Thursday 16 August 2007

Mass pinging hosts with the help of standard binutils

To quickly perform mass pinging of hosts on a local network, if you don't have any special tools, the following scripts may be of help:

---- SCRIPT 1 : pingnet ----------
#!/bin/sh

for(( i=0; i <= 255; i++ ))
do
  ping -w 1 "192.168.0."$i
done | `dirname $0`/hostalive
----------------------------------

Give that file rights for execution. You may want to correct "for" condition and address beginning, depending on your network configuration. dirname is standard utility. hostalive is another script, which parses the output of ping and makes a conclusion whether the host is alive or not. For that task, sed is used:

---- SCRIPT 2 : hostalive ----------
#!/bin/sed -nf

# get IP address
/^--- / {
  s/^--- //
  s/ ping.*/ /
  h
}

# is it alive?
/^rtt / {
  g
  s/$/OK/
  p
}

# is it down?
/ 100% packet loss/ {
  g
  s/$/DOWN/
  p
}
----------------------------------

Give this file an executable permissions as well, and put it into the same folder as pingnet. Now you may run pingnet, and with a bit of luck you'll see something like that:

192.168.0.0 OK
192.168.0.1 DOWN
192.168.0.2 OK
192.168.0.3 DOWN
192.168.0.4 DOWN
192.168.0.5 DOWN
192.168.0.6 OK
192.168.0.7 OK
192.168.0.8 DOWN
192.168.0.9 OK

No comments: