Bash script to test IP range on server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to test IP range on server
# 1  
Old 10-24-2010
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.
# 2  
Old 10-24-2010
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?
Code:
ping -c1 -W1 $IP >/dev/null  ||  record_bad_ip $IP

If some nodes won't ping, you could use a command something like this
Code:
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.
Code:
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

# 3  
Old 10-24-2010
# 4  
Old 10-25-2010
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.
# 5  
Old 10-25-2010
You might be looking for something really simple like this:
(more of a learning experience that useful, but easy to understand)
Code:
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:
Code:
#!/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.

Moderator's Comments:
Mod Comment Use code tags, please...

Last edited by Scott; 10-25-2010 at 09:01 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Bash script - printing range of lines from text file

I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would... (8 Replies)
Discussion started by: ksmarine1980
8 Replies

3. Homework & Coursework Questions

Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have written a script for a dice game that: (1) tells user that each of the 2 die are 6 sided (Spots=6); (2)... (3 Replies)
Discussion started by: LaurenRose
3 Replies

4. Shell Programming and Scripting

bash script range calculations

Hi, I have data in the following form: AB001 10 AB002 9 AB003 9 etc AB200 5 What I need to do is sum up the second value according to groups of the first, i.e. AB001 to AB030 the total being X, AB031 to AB050 the total being Y etc (there are 5 AB ranges of different sizes). I'm sure... (3 Replies)
Discussion started by: chrissycc
3 Replies

5. Shell Programming and Scripting

Help: Bash backup script (includes copy, test-

Basically it's for a work assignment. Have to make a menu with the following choices ***************menu********************* 1) Show Current Directory 2) Dispaly Current Time and Date 3) Copy 4) Change Password 5) write directory to file 6) Edit File Directory 7) Make backup from... (1 Reply)
Discussion started by: Covax
1 Replies

6. UNIX for Dummies Questions & Answers

How do i tell my bash shell script to test the output of a command against something

How do i tell my bash shell script to test the output of the command i'm using?? I want this script to look for lines not equal to 1 then let me know.. $ cat blah ; echo ---- ; cat blah.sh 1 fe 1 fi 1 fo 0 fum 1 blahda 1 blah 0 blahh 1 bla 1 bl 1 blahhh ---- #!/bin/bash while... (1 Reply)
Discussion started by: phpfreak
1 Replies

7. Shell Programming and Scripting

bash script to test network connection - please help

I want to test if my host can connect to any of the following 10 hosts (192.168.1.0 to 192.168.1.9) I didnt know which command i should use, so i chose ping. (i wonder if i should use tracepath or sth else) my code is the following: #!/bin/bash clear firstPart="192.168.1" maxNum="9" ... (2 Replies)
Discussion started by: shadow_boi
2 Replies

8. Shell Programming and Scripting

Bash Script w/ IP Range

Is there a basic IP range script or a site that has basic script written? I am making a script that is looking for a device on my network threw HTTP://. I and using a for loop with wget to download the page and using grep to search the code for the serial number. My range is 172.16.x.x/16 I... (3 Replies)
Discussion started by: captaindoogles
3 Replies

9. UNIX for Advanced & Expert Users

execute a script on test server from dev server

I need to execute a script from dev server which is located on Test server.I can use ftp to connect to dev server and from there how can i execute a command on test server. Thanks (5 Replies)
Discussion started by: ukatru
5 Replies

10. UNIX for Dummies Questions & Answers

Script to Test Application Server is running

Hi, I'm a complete novice at Unix and need to create a script that does the following... checks to see if an application server is running. If the app is running then print 'Available' Else print 'Unavaliable' exit from scriopt I have no idea where to start. I'd be very grateful... (0 Replies)
Discussion started by: duglover
0 Replies
Login or Register to Ask a Question