Bash script to test IP range on server

Hello,
We have to configure servers with a range of IPs which is in itself a subject for another script assistance request -but- we have run into quite a few IP ranges with routing problems lately.

I've been trying to figure out the best way to test a range of IPs, I mean, manually it's not much fun typing "telnet ip 22" then "quit" a few dozen times for each setup. (I log into another linux box and try to telnet to SSH on each IP on the new server, this makes sure the IP is at least functioning and reachable).

I'm open to suggestions for a better method, of course.

A bash script I can upload to my work box and then just input a start and stop IP for the range w/ a result would be great.

Actually making a SSH connection on each IP and transferring a file or running MTR or some other diagnostic over each connection would probably be more "flash" in terms of something I can copy and paste into the customer's deployment report.

I'm just starting off with writing my own bash scripts and could really use the help on this.

This seems to be precisely the problem that the ping command was invented to test.

Can you loop through the IP addresses with a command something like this?

ping -c1 -W1 $IP >/dev/null  ||  record_bad_ip $IP

If some nodes won't ping, you could use a command something like this

ssh invalid@$IP 2>&1 | grep -q "No route to host"  &&  record_bad_ip $IP

---------- Post updated at 11:41 AM ---------- Previous update was at 11:09 AM ----------

A quick check of the Fedora repositories revealed three packages that might be helpful. I've never used any of them.

Name        : fping
Summary     : Scriptable, parallelized ping-like utility
URL         : http://fping.sourceforge.net/
Description : fping is a ping-like program which can determine the accessibility
            : of multiple hosts using ICMP echo requests. fping is designed for
            : parallelized monitoring of large numbers of systems, and is
            : developed with ease of use in scripting in mind.

Name        : tcping
Summary     : Check of TCP connection to a given IP/Port
URL         : http://www.linuxco.de/tcping/tcping.html
Description : tcping does a TCP connect to the given ip/port combination. The
            : user can specify a timeout in seconds. This is useful in shell
            : scripts running in firewalled environments. Often SYNs are just
            : being dropped by firewalls, thus connection establishment will be
            : retried several times (for minutes) until a TCP timeout is
            : reached. With tcping it is possible to check first if the desired
            : port is reachable and then start connection establishment.

Name        : hping3
Summary     : TCP/IP stack auditing and much more
URL         : http://www.hping.org/
Description : hping3 is a network tool able to send custom TCP/IP packets and to
            : display target replies like ping do with ICMP replies. hping3 can
            : handle fragmentation, and almost arbitrary packet size and
            : content, using the command line interface.
            : Since version 3, hping implements scripting capabilties

try Nmap - Free Security Scanner For Network Exploration & Security Audits.

Thanks for the replies, tcping looks really interesting but as I said, I'm just getting started w/ writing shell scripts and wouldn't know where to start. (I have an e-book but not much time, unfortunately). I do have a background in programming using a couple of languages that are dead now so I'm hoping some of the base concepts will still translate.

You might be looking for something really simple like this:
(more of a learning experience that useful, but easy to understand)

export subnet=1
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14     17 18 19 20 21 22 23 24 25 26 27 28 29 30
do
echo  "      192.168.$subnet.$i "
/sbin/ping -c 1 -w 1   192.168.$subnet.$i
done

save the above in a file named pingall.sh
then chmod +x pingall.sh this sets the permissions allowing you to eXecute the scipt
then ./pingall.sh

You might find my version of the documentation of chmod helpful
real-world-systems com/docs/chmod.1.html
also
real-world-systems com/docs/ping.1.html

A more complicated and much faster version:

#!/bin/bash
#PING smacker.local (192.168.0.7): 56 data bytes
#64 bytes from 192.168.0.7: icmp_seq=0 ttl=64 time=0.120 ms
#
#--- smacker.local ping statistics ---
#1 packets transmitted, 1 packets received, 0% packet loss
#round-trip min/avg/max/stddev = 0.120/0.120/0.120/0.000 ms
# just get sudo to ask for password sooner
sudo echo -n "$HOSTNAME  "

export subnet=`sudo /sbin/ping -l 3 -c 1  $HOSTNAME  | egrep "ms" | grep -v 100% |sed "s/64 bytes//" | sed "s/icmp_//" | sed "s/\.... ms//"  | sed "s/round.*//"  |sed "s/from//"| grep -v " 0% packet loss" | sed s"/: seq=. ttl=.4 time=.//" |cut -f3 -d\.`

ping $HOSTNAME
echo "++subnet $subnet"

for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 17 18 19 20 21 22 23 24 25 26 27 28 29 30
do
echo  "      192.168.$subnet.$i " 
#       alert    need at least 3 to get min/max average stdDev preload  -l 3  -c 3  uniq only sees first one!
sudo /sbin/ping -A -c 3 -l 3   192.168.$subnet.$i  | egrep "ms" | grep -v 100% |\
sed "N; s/\n/   /;s/64 bytes from/   from/g; s/ttl=.. /& /g; s/icmp_seq=.//g ; s/\/.....\/.....\/+0.00. ms//; s/\/.....\/.....\/nan//; s/\/stddev/ stddev/;s/\/0./ ./g; s/. ms/ms/g ; s/ .\.... \.... \.... .0[01]ms/ms/; s/ \.00ms/ms/; s/   round/ round/g" | grep -v " 0% packet loss" |grep -v "^$" |uniq &

host 192.168.$subnet.$i | grep -v NXDOMAIN | \
      sed "s/in-addr.arpa//; s/ domain name pointer// ; s/^./                &/" &
sleep 0

#sudo /sbin/ping -A -l 3 -c 3    192.168.1.$i  | egrep "ms" | grep -v 100% |sed "N; s/\n/   /;s/64 bytes from/   from/; s/ttl=.. /& /; s/icmp_seq=.// ; s/0.000/ 0/; s/\/.....\/.....\/nan//" | grep -v " 0% packet loss" |grep -v "^$" &

done

#sleep 1
for i in  6   4  2  
do
sleep 2
echo -n $i " processes still running: " ; /bin/ps | wc -l
done
sleep 2
 echo $PS1

Please let us know how you do.