LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-19-2004, 01:30 AM   #1
bdp
Member
 
Registered: Apr 2002
Distribution: RH 9
Posts: 230

Rep: Reputation: 30
bash script prob: pinging boxes


hi all,
using the following script:

<code>
#!/bin/bash
BOXLIST="10.0.2.101 10.0.2.102 10.0.2.103 10.0.2.104"
for i in $BOXLIST
do
PINGOUT=`ping -w1 $i |grep '100% packet loss'`
if [ "$PINGOUT" = "" ]; then
echo $i is (at least somewhat) up
else
echo $i is down
fi
done
</code>

the output seems fine except that the -w1 option doesn't get applied, i.e. when a box is down it hangs for ~5 seconds instead of the specified 1 second. however, when i type " ping -w1 10.0.2.101" into a command line, ping only waits 1 second before returning control when 101 is down.

this seems like a minor point but a box sometimes doesn't reply at all, hanging the script completely

hopefully i'm missing something, shouldn't ping do the same thing at the CL as it does in a bash script? any suggestions would be greatly appreciated.

thx, - bp
 
Old 02-19-2004, 01:50 AM   #2
PSIplus
Member
 
Registered: Feb 2004
Location: Vienna/Austria
Distribution: Debian Unstable
Posts: 36

Rep: Reputation: 15
Yep, it seems that ping has a small problem with timeouts, but I made an alternative solution at our last lanparty... Find out wich port is open on all these boxes (for example echo, netbios, etc.) and open a socket to it. Socket timeouts are much better timeable!

Example how to solve this in PHP:
Code:
<?
...
	error_reporting(0);
	$fp=fsockopen($temp[data],139,&$errno,&$errstr,0.2);
	if($fp) $stat="Online";
	else $stat="Offline";
	if($fp) fclose($fp);
...
?>
(The error-reporting-line is required if you don't want the php-socket-warnings ;-) )

It is as easy to solve it in C or via BASH-Script. In C Just create a socket and use "connect", basically the same as in PHP. In BASH-Scripts you could use netcat (nc).
 
Old 02-19-2004, 01:58 AM   #3
bdp
Member
 
Registered: Apr 2002
Distribution: RH 9
Posts: 230

Original Poster
Rep: Reputation: 30
thx for the speedy reply and workaround, i was wondering if i just ran into another redhat problem.

am stoked, thanks.
 
Old 02-19-2004, 02:11 AM   #4
PSIplus
Member
 
Registered: Feb 2004
Location: Vienna/Austria
Distribution: Debian Unstable
Posts: 36

Rep: Reputation: 15
What kind of RedHat Problem? (I use Debian, no problems ;-) )
 
Old 02-19-2004, 02:48 AM   #5
codeape
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 62

Rep: Reputation: 15
Hmm.. my ping doesn't even have the -w option.

I know you're already helped, but I had some spare time and I like shell scripting

You could speed things up by pinging the list of IP's simultaneously (without waiting for the answers). I believe fping does this, but in a script it could look like this:

-------------------------
#!/bin/bash

BOXLIST="10.0.2.101 10.0.2.102 10.0.2.103 10.0.2.104"
for i in $BOXLIST
do
/tmp/pingcheck.sh $i &
done
wait
-------------------------

This script calls the second script for each IP in the list and puts them all in the background.

-------------------------
#!/bin/bash

PINGOUT=`ping -c 2 $1 | grep "100% packet loss"`
if [ "$PINGOUT" = "" ]; then
echo "$1 is (at least somewhat) up"
else
echo "$1 is down"
fi
-------------------------

I had to change the -w option to -c since my ping doesn't support it. The "-c 2" limits the ping to two ping requests.
 
Old 02-19-2004, 11:08 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Why not use nmap's ping-scanning?
Code:
#!/bin/bash
nmap -sP 10.0.2.101-104
(with -sP it just pings the hosts, and will not do any port scanning)
 
Old 02-19-2004, 12:00 PM   #7
codeape
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 62

Rep: Reputation: 15
I don't know. Why not indeed.
 
Old 02-19-2004, 02:21 PM   #8
BaerRS
Member
 
Registered: Oct 2001
Location: Columbus, Ohio
Distribution: all.. but mainly SuSe--- looks like it changing to Red Hat
Posts: 119

Rep: Reputation: 15
I'm not sure what your trying to do here..
I've done something similar, so I wanted to share.

I wanted to check the status of my
ISP's router(I've had some connectivity problems)...
This script will check two internal IP address, and if good. Will go cycle threw a list of IP's and check them.
If it fails, you can have it log, e-mail, and send an sms(text) message to a phone/alpapager.

I have this running on a cron job every 5 mins.
From the log files, I was able to prove I was down more than up.. and I got a credit from RR. This bash script was more for monitoring some sites I was responsible for.

Code:
#!/bin/bash
# reads sites from a file
# ping a site if no response then email a message
logfile="/var/log/pink.log"
smsnotify="1234564985@vtext.com"
mailnotify="my@example.com"

chk1="192.168.1.1"      // Its my default route to insure my side is up. 
chk2="192.168.1.2"      // I"m also checking my dns

ping -c 2 -w 10 -Q 0x04 $chk1 2>&1 > /dev/null; chk1=$?
ping -c 2 -w 10 -Q 0x04 $chk2 2>&1 > /dev/null; chk2=$?

if [ $chk1 -gt 0 -a $chk2 -gt 0 ]
       then
       echo "reliable hosts are down. no sites checks performed." `date` >>$logfile
       else
{


#cat /usr/lib/pink/pink.sites|while read pingsite pingname
cat /root/status/iplist|while read pingsite pingname
do
       ping -c 2 -w 10 -Q 0x04 $pingsite 2>&1 > /dev/null
       
       #if 100% packet loss - a bad ping
       if [ $? -gt 0 ]
       then
         echo $pinname $pingsite "Failed on" `date` >>$logfile
	#echo "no replay form $pingsite"
        # echo $pingname $pingsite "Alert" `date`| mail -s "$pingname" $mailnotify
        #echo no reply from $pingname $pingsite | mail -s "$pingname" $smsnotify
     
       else {
       touch "$logfile"
       echo $pingname $pingsite "Passed on " `date` >>$logfile
       }
       fi
done
}
Heres the iplist file.
Code:
24.208.128.1 router
65.24.192.171 dns-1 
65.24.192.172 dns-2
65.24.000.163 dns-3

Last edited by BaerRS; 02-19-2004 at 02:23 PM.
 
Old 02-19-2004, 06:58 PM   #9
bdp
Member
 
Registered: Apr 2002
Distribution: RH 9
Posts: 230

Original Poster
Rep: Reputation: 30
cool, i'm fairly new to scripting bash and am stoked to see diff solns. thx for the replies as i'm going to go through it all this wknd to build some more familiarity with structure/syntax.

BaerRS: "I'm not sure what your trying to do here ... If it fails, you can have it log, e-mail, and send an sms(text) message to a phone/alpapager. "

well, you're right on with what i want to do: manage 40 addresses and have it email me when one doesn't respond, as that means my job stopped on _all_ the boxes, which sucks. thanks a ton for the info, the replies on this site are quite a stoke.

PSIplus: as for my redhat comment, i've just noticed they like to release things before they're ready for primetime but fortunately that's slowly changing.

cheers, -bp
 
Old 02-20-2004, 01:50 AM   #10
codeape
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 62

Rep: Reputation: 15
Most of the feedback you got could can be combined to improve on BaerRS' script. Instead of ping you could use nmap and you could execute the while loop in the background (with & ) so all IP's in the iplist file are polled (virtually) simultaneously.
Ought to speed things up nicely.

Last edited by codeape; 02-20-2004 at 01:52 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script for pinging servers steste Linux - Networking 12 11-16-2004 09:38 AM
Help writing a pinging bash script dehuszar Linux - Software 3 06-18-2004 02:03 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
win2000 boxes cannot map but winnt boxes can - same user! starbeetlechick Linux - Networking 0 07-24-2003 07:24 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration