help to ping a host, is it alive or not ...

hello to everyone, i was wondering if you could help me with a script im working on, it's kind of simple but i dont have a lot experience on unix comands: well, here it is:

you might apreciate the infinite while loop :D, it is supossed to be running on the server all day scaning it every 5 minutes and cheking if a host is alive or not, if it isnt it will send an email to the sistems administrator.

the problem here is that when it gets to the if [ $? -ne 0 ], it executes both the if and else commands, i have been reading in several pages and i found a solution to use ping exit commands, however i already read in the unix manual that this exact aplication im trying to make is impossible to do with exit commands, here the page where i read it: http://www.rt.com/man/ping.8.html
extract from the page

"If ping does not receive any reply packets at all it will exit with code
1. On error it exits with code 2. Otherwise it exits with code 0. This
makes it possible to use the exit code to see if a host is alive or not"

the code:

#sx3v1l_1n51de
#Sistema de Monitoreo de Servidores
#CENCAR

a=1
while (test "$a"!="0")
do
for ipadr in `cat ipadress`
do
ping $ipadr
if [ $? -ne 0 ]
then
echo $ipadr" is alive"
else
echo $ipadr" is dead"
ipadr >> noresponseip

#./strike1

/strike1 is another script that waits 1 minute and then tries to ping again if it still doesnt answer then sends the email to the admin, strike1 uses the noresponseip file to read the ip that didnt answered
i havent actually tested this part yet, what i want to do exactly is that strike1 checks the bad ip's while the other keeps scanning, im not sure about my impementation, the way i put it, does it executes and ends the ./strike1 script or it just starts it and continues scanning ip'? because i dont want it to stop for a minute an send an email evry time it founds host is not answering..
/

fi
done
done

thank you for your help :smiley:

Here is a ping node script. Look it over, create a mail.list file and set the path to this file in the script. Then add a cron entry to run the script at any interval you choose. The script will ping any host you have listed in the ping file (which you need to define and set its path to.). The script was written by the author of Mastering UNIX Shell Scripting by Wiley books (he provides his scripts for free on his website). I use the script but I have modified it so that I ping the IPs in the host file instead of a ping list. Note that I simply changed the extension on the script to preserve formatting when you open it online. Change extension back to ksh after saving.

a=1

while (test "$a"!="0")
  do
    for ipadr in $(cat ipadress)
      do
        ping $ipadr
        
        if [ $? -ne 0 ]
         then
            echo "$ipadr is alive"
         else
           echo $ipadr" is dead"
           $ipadr >> noresponseip
        fi
     done
  done
#./strike

Be careful about where you run this script if its not in the same dir as strike then it wont work. Also, you should use fully qualified path to the file called ipadress (and maybe spell it correctly for completeness :wink: ) Finally, you should set a=0 at some point to exit the loop! Maybe trap a signal or an event like when you hit CNTRL-C

thanks for the tips, yep it is address not adress, my mistake :D, its better the way you wrote the script, but i still have a problem with the if conditional.

when it compares the ping results it executes both sentences and writes:

148.202.15.70 is alive
148.202.15.70 is dead

there has to be something wrong with that if, i dont know what it is exaclty...
i have also tried comparing strings with the same mistake, i dont know why it executes both commands...

pong=$ipadr" is alive"
alive=`ping $ipadr`
if (test "$pong"="$alive")

any idea of what's wrong this if?

Which shell are you using? If Kornshell run the script as

ksh -x <scriptname>

then post the output.

it's a bourne shell (sh), the system is a solaris 5:

$ sh -xv servscript
#Sistema de Monitoreo de Servidores
#CENCAR

a=1
a=1
while (test "$a"!="0")
do
for ipadr in `cat ipadress`
do

ping $ipadr

if ("$?"="0")

then
echo $ipadr" is alive"
else
echo $ipadr" esta muerto"
#ipadr >> deadhosts
#./strike1

fi
done
done
+ test 1!=0
+ cat ipadress
+ ping 148.202.1.29
148.202.1.29 is alive
+ 0=0
servscript: 0=0: not found
+ echo 148.202.1.29 esta muerto
148.202.1.29 esta muerto
+ ping 148.202.3.5
148.202.3.5 is alive
+ 0=0
servscript: 0=0: not found
+ echo 148.202.3.5 esta muerto
148.202.3.5 esta muerto
+ ping 148.201.3.2
no answer from 148.201.3.2
+ 1=0
servscript: 1=0: not found
+ echo 148.201.3.2 esta muerto
148.201.3.2 esta muerto
+ ping 148.202.202.25
148.202.202.25 is alive
+ 0=0
servscript: 0=0: not found
+ echo 148.202.202.25 esta muerto
148.202.202.25 esta muerto
+ ping 128.12.65.202
no answer from 128.12.65.202
+ 1=0
servscript: 1=0: not found
+ echo 128.12.65.202 esta muerto
128.12.65.202 esta muerto
+ ping 148.148.148.148
ICMP Host Unreachable from gateway host-200-76-4-89.block.alestra.net.mx (200.76.4.89)
for icmp from filgc.class.udg.mx (148.202.15.70) to 148.148.148.148
ICMP Host Unreachable from gateway host-200-76-4-89.block.alestra.net.mx (200.76.4.89)
for icmp from filgc.class.udg.mx (148.202.15.70) to 148.148.148.148
^C$

Is above not giving any problem ?
Giving continuously echos ???

If you want to limit number of echos to 5,

do

echo -c 5 $ipaddr

looks like that is working ok, the problem is here:

+ ping 148.202.1.29
148.202.1.29 is alive
+ 0=0
servscript: 0=0: not found
+ echo 148.202.1.29 esta muerto (<-- is dead, in spanish)
148.202.1.29 esta muerto

as you can see the ping works all right, and it does send a 0 as an indication that it all went fine and the host did respond, and echoes host is alive, but then it's that error that says: servscript: 0=0 not found... and it also echoes host is dead, even if it had already checked that it was alive...

im not sure what it is, i guess is some kind of syntax error but i dont know, it looks fine to me...

Try integer comparison ...

if  test $? -eq 0  then 
  echo " Alive"
fi

Thanks, i already fixed it, seem that teh sintax was:

if ping $ipadr
then
echo "is alive"
else
echo "is dead"
fi

it seems thet the if statement just goes like if 0 do something... i didnt knew that... but thank you all for helping me :slight_smile:

only one more thing: when it's pinging it shows the results, 148.202.15.70 is alive, and i dont want that to be shown, it only needs to run and do nothing if the host is alive, if a host doesnt answer send an email...

is it anyway to tell it that i dont need the output on the screen?


echo "dead " > tmp

ping yahoo.com
if test $? -eq 0 
then
  sleep 0 ;
else
sendmail X@Y.com  < tmp
fi

One more way .....

echo "dead " > tmp

ping yahoo.com
if test $? -eq 0
then
  stty -echo ;
  echo "Alive" ;
  stty echo ;
else
sendmail X@Y.com  < tmp
fi

Thank you all for Helping me with my script, here it is finished, it's not fancy, it's actually very simple, but for monitoring less than 25 servers is ok.
I'll leave it Here if someone else wants to use it.

there are 2 files, servscript and strike1, servscript will be running in the background all day and pinging the hosts every 5 minutes, if the host is alive then it continues scanning a does nothing, if the host is dead it writes it's IP address to the file deadip, when it finish scanning all of the ip's in the ipaddresslist file, then it will start the striek1 script, whose only function is to wait a minute, just to make sure that the host didnt answered because it was busy, then it pings all of the ip's in the deadip file, if they are still dead it sends an email to the sytems administrator, strike1 finishes and control goes back to servscript where it sleeps for 5 minutes to start the scanning all over again...

$ cat servscript

#!/usr/bin/sh
#Sistema de Monitoreo de Servidores
#CENCAR

a=1
while (test "$a"!="0")
do
for ipaddress in `cat ipaddresslist`
do
if ping $ipaddress >> pingout
then
continue
else
echo $ipaddress >> deadip
fi
done
./strike1
sleep 300
rm deadip
rm pingout
done

$ cat strike1

sleep 60
for a in `cat deadip`
do
if ping $a >> pingout
then
continue
else
echo $a" No Respondio al Segundo Intento" | mailx -s "ADVERTENCIA DEL SISTEMA DE MONITOREO" xxxx@cencar.udg.mx
echo $a" No Respondio al Segundo Intento" | mailx -s "ADVERTENCIA DEL SISTEMA DE MONITOREO" xxxx@cencar.udg.mx
fi
done