ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
<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.
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!
(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).
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.
Distribution: all.. but mainly SuSe--- looks like it changing to Red Hat
Posts: 119
Rep:
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
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.