Last active
November 18, 2025 08:53
-
-
Save KarimAziev/90c5e60dd59cf96a170c953a175b822f to your computer and use it in GitHub Desktop.
Detects the default interface and its IPv4/netmask, computes the subnet in CIDR form, then runs sudo nmap -sn on that subnet to discover live hosts - producing a list of reachable IPs (with hostnames and MAC addresses when available). #sh #macos #linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Detect default interface and run sudo nmap -sn on the local IPv4 subnet (CIDR) | |
| sudo nmap -sn $(ip -4 route list match $(ip -o -4 addr show dev $(ip route get 8.8.8.8 2> /dev/null | awk '{for(i=1;i<NF;i++) if($i=="dev"){print $(i+1); exit}}') | awk '{print $4}') | awk '{print $1}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Detect default interface and run sudo nmap -sn on the local IPv4 subnet (CIDR) | |
| sudo nmap -sn $( | |
| iface=$(route get default | awk '/interface:/ {print $2}') | |
| ip=$(ipconfig getifaddr "$iface") | |
| maskhex=$(ifconfig "$iface" | awk '/inet /{for(i=1;i<=NF;i++) if($i=="netmask"){print $(i+1); exit}}') | |
| masknum=$((maskhex)) | |
| IFS=. read -r a b c d <<< "$ip" | |
| n0=$((a & ((masknum >> 24) & 255))) | |
| n1=$((b & ((masknum >> 16) & 255))) | |
| n2=$((c & ((masknum >> 8) & 255))) | |
| n3=$((d & (masknum & 255))) | |
| prefix=0 | |
| m=$masknum | |
| while [ $m -ne 0 ]; do | |
| prefix=$((prefix + (m & 1))) | |
| m=$((m >> 1)) | |
| done | |
| printf "%d.%d.%d.%d/%d" $n0 $n1 $n2 $n3 $prefix | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment