network-programmingshellpingarp

Send a ping to each IP on a subnet


Is there a command line based way to send pings to each computer in a subnet? Like

for(int i = 1; i < 254; i++)
    ping(192.168.1.i);

to enforce arp resolution?


Solution

  • I would suggest the use of fping with the mask option, since you are not restricting yourself in ping.

    fping -g 192.168.1.0/24
    

    The response will be easy to parse in a script:

    192.168.1.1 is alive
    192.168.1.2 is alive
    192.168.1.3 is alive
    192.168.1.5 is alive
    ...
    192.168.1.4 is unreachable
    192.168.1.6 is unreachable
    192.168.1.7 is unreachable
    ...
    

    Note: Using the argument -a will restrict the output to reachable ip addresses, you may want to use it otherwise fping will also print unreachable addresses:

    fping -a -g 192.168.1.0/24
    

    From man:

    fping differs from ping in that you can specify any number of targets on the command line, or specify a file containing the lists of targets to ping. Instead of sending to one target until it times out or replies, fping will send out a ping packet and move on to the next target in a round-robin fashion.

    More info: http://fping.org/