the codingmerc llc

application programming expert for hire

Find All Hosts On A Network

I recently came across the need to quickly see all hosts connected on my VPN network. I have some hosts on the VPN advertising services and some that don’t and I wanted to make sure that they are still connected. The easy way would be to ping them but that becomes tedious as soon as it is becoming more than a couple of hosts in question.

I tried a simple bash loop with ping but for a /24 subnetwork, it takes roughly 5 minutes to check all hosts…

for ip in $(seq 1 254); 
do 
	ping -c 1 192.169.1.$ip>/dev/null
	[ $? -eq 0 ] && echo "192.169.1.$ip UP" || :
done

It works and the only dependency is your shell but It certainly is not nice and far away from the best solution to solve the problem.

nmap to the rescue!

I installed nmap and ran the following command:

nmap -sP 192.169.1.0/24

For newer versions of nmap you can also use sn instead

nmap -sn 192.169.1.0/24

It took only a couple of seconds for all hosts to respond compared to a couple of minutes looping ping instead!

Beware the firewall

If your firewall does not allow ICMP echo requests your network participants will not respond to a ping request even though they are up. You might want to consider opening your firewall to ICMP requests for local networks only if your host is also available publicly. 

© 2017 the codingmerc llc