Question

I have a file with about 500 devices which I need to ping to check if they're currently accessible or not from various subnets around my network. It's essentially a basic test to check for routing/accessibility issue.

Not sure where to start really. I have a basic file in which I have put each individual IP in a file line-by-line.

For example, the file looks like this:

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4

I'd need it to return something like the following, if the third in line didn't get a reply:

192.168.1.1 Accessible
192.168.1.2 Accessible
192.168.1.3 Inaccessible
192.168.1.4 Accessible

I'm running Ubuntu here. Apologies for not having any idea where to start !

Cheers Steve.

Was it helpful?

Solution

You should use nmap in ping scan mode with:

nmap -sn -oG - -v -iL hosts_to_scan.txt

This will try to ping all hosts contained in the hosts_to_scan.txt file.

By the way, you can also specifify a subnet, if that is the case:

nmap -sn -oG - -v 192.168.1.0/24

And/or save the result to file:

nmap -sn -oG status.txt -v 192.168.1.0/24
nmap -sn -oG status.txt -v -iL hosts_to_scan.txt

OTHER TIPS

I would use nmap probably for a long list, but if you are in a command line and need a quick one-liner, this will do also:

$ for i in `cat file.txt `;do ping -c 1 $i;done

PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=50 time=16.271 ms

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 16.271/16.271/16.271/0.000 ms
PING 8.8.4.4 (8.8.4.4): 56 data bytes
64 bytes from 8.8.4.4: icmp_seq=0 ttl=50 time=16.030 ms

--- 8.8.4.4 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 16.030/16.030/16.030/0.000 ms

On a positive note, this method it's quick and easy to remember. Works (probably) with all major shells (bash, zsh, *sh?).

On the other hand it's fairly verbose and you don't want that in say 200 IP's, even 10 might be hard to monitor.

I would write a script in ruby, or pytho or whatever language you like if nmap can't cut it.

EDIT: This one is cleaner and also has some additional stats:

for i in `cat file.txt `;do ping -c 1 $i|grep 64;done  
64 bytes from 8.8.8.8: icmp_seq=0 ttl=50 time=15.397 ms
64 bytes from 8.8.4.4: icmp_seq=0 ttl=50 time=13.170 ms

There's virtually nothing that can't be done with gnu-tools.

Basic schema would be to ping each one of the servers and print the result.

If you store the IPs in a ips.txt file, you could do:

while read my_ip
do
   test_ping $my_ip
done < ips.txt

And then create a function such as test_ping, that pings once to each IP:

test_ping () {
    if ping -c 1 $1 &> /dev/null
    then
        echo "$1 Accessible"
    else
        echo "$1 Inaccessible"
    fi

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top