FreeBSD and Linux: How to find live hosts on a directly connected subnet with ping
How to find mac address of all hosts in a connected subnet using "ping" / how to find live hosts on a directly connected subnetSome systems do not have advanced tools for network scanning discovery like nmap, just the basic "ping". Under FreeBSD and Linux a ping while loop can be used to send requests to all hosts in a specific subnet and then to check the arp table entries:
To acomplish this ping while loop in the most efficient way, I'm interested in two ping options: "-W 1"and "-c 1":
Quote:
usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]
[-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]
[-P policy] [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]
[-W waittime] [-z tos] host
ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]
[-M mask | time] [-m ttl] [-P policy] [-p pattern] [-S src_addr]
[-s packetsize] [-T ttl] [-t timeout] [-W waittime]
[-z tos] mcast-group
So send only one ping request to each host and wait one second for the reply, instead of the default 10-11 seconds timeout in Linux and FreeBSD.
Ping while loop to find out alive hosts in Linux and FreeBSD:Code:
# i=1; while [ $i -lt "255" ] ; do ping -c 1 -W 1 192.168.0.${i} >/dev/null; i=`expr $i + 1`; done
This command will take at most 254-255 seconds
Now, we're not interested in the ping replies as some hosts could block ICMP packets, but we're interested in the mac entries in the arp table of the host (remember the target subnet is directly connected):
Code:
# arp -an | grep -v incomplete
? (10.1.10.2) at 00:1c:39:2a:32:ba on vmx1 permanent [ethernet]
? (192.168.0.65) at bc:8e:a4:ea:1a:e3 on vmx0 expires in 818 seconds [ethernet]
? (192.168.0.1) at 3c:21:72:c6:c1:88 on vmx0 expires in 1195 seconds [ethernet]
? (192.168.0.154) at 00:0c:39:61:39:17 on vmx0 expires in 123 seconds [ethernet]
? (192.168.0.152) at 00:1c:39:16:b8:b9 on vmx0 expires in 849 seconds [ethernet]
? (192.168.0.153) at 00:1c:39:52:07:bb on vmx0 expires in 490 seconds [ethernet]