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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help to ping a host, is it alive or not ...
# 1  
Old 01-26-2005
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 Smilie, 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 Smilie
# 2  
Old 01-26-2005
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.

Last edited by google; 01-26-2005 at 09:19 PM..
# 3  
Old 01-26-2005
Code:
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 Smilie ) 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

Last edited by google; 01-28-2005 at 09:07 AM..
# 4  
Old 01-27-2005
Bug thank you guys!!

thanks for the tips, yep it is address not adress, my mistake Smilie, 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?
# 5  
Old 01-27-2005
Which shell are you using? If Kornshell run the script as
Code:
ksh -x <scriptname>

then post the output.
# 6  
Old 01-27-2005
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$
# 7  
Old 01-27-2005
Quote:
ping $ipadr
Is above not giving any problem ?
Giving continuously echos ???

If you want to limit number of echos to 5,

do

Code:
echo -c 5 $ipaddr

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Ping to remote host failed

Actually. I was getting a ping to remote host failed for one of my etherchannel. When I checked it was in backup adapter and again I use to faileover and brought to primary channel. But it was again going to backup channel and giving me the alert ping to remotehost failed. When I checked the load... (3 Replies)
Discussion started by: Mohamed Thamim
3 Replies

2. Shell Programming and Scripting

Ping Response from the host name

Hi All, I have the requirement where am pinging the server and matching the IP address with the existing IP address. Below code is returning me the IP address and my requirement is i have to see that also whether it is pinging or not PING useipapd01 (172.22.32.87) 56(84) bytes of data. 64... (1 Reply)
Discussion started by: sharsour
1 Replies

3. Shell Programming and Scripting

Ping Host Until it is up and email

Hi I am trying to write a script which runs until the host is up. i got it figured out that it needs to be in loop till it return $? = 0. Not getting it through though. I am not sure about the 6th line !!! #!/bin/sh HOSTS="host.txt" ping(){ for myhost in "$HOSTS" do ping -c -1 "$myhost"... (8 Replies)
Discussion started by: Antergen
8 Replies

4. Solaris

able to ping all hosts but not able to traceroute any host

i am using solaris 10 and i am able to ping all the hosts but i am not able to traceroute any of them. how to fix this? (9 Replies)
Discussion started by: chidori
9 Replies

5. IP Networking

ping can not recognize host but host command can

Hi, I have a weird problem. when ever I do ping command like for example ping unix.comI get the following message: # ping unix.com ping: unknown host unix.com but when I use host the computer is able to know the host. # host unix.com unix.com has address 81.17.242.186 unix.com mail is... (2 Replies)
Discussion started by: programAngel
2 Replies

6. Solaris

Unable to ping Solaris VM from Xp host

Hi All, I am using Vmware Workstation 6.0.3 build-80004. Guest OS: Solaris 10 Host OS : Win XP I am getting request time out when i am trying to ping from XP ( cmd line) to Solaris VM - I have assigned IP 192.168.50.5 in Solaris VM ( Hostname: Tower1) and it is in UP status. ... (4 Replies)
Discussion started by: saurabh84g
4 Replies

7. Solaris

unable to ping a host in another domain

Hello I have a server in it.siroe.com I added it.siroe.com in /etc/resolv.conf. I still can't ping the server. any service to restart here? any other file to edit? thx (4 Replies)
Discussion started by: melanie_pfefer
4 Replies

8. Solaris

PING - Unknown host 127.0.0.1, Unknown host localhost - Solaris 10

Hello, I have a problem - I created a chrooted jail for one user. When I'm logged in as root, everything work fine, but when I'm logged in as a chrooted user - I have many problems: 1. When I execute the command ping, I get weird results: bash-3.00$ usr/sbin/ping localhost ... (4 Replies)
Discussion started by: Przemek
4 Replies

9. IP Networking

QNX host cannot ping SCO host, vice versa

The problem I am facing now is that the QNX host could not ping the SCO host and vice versa. They are in the same domain, ie, 172.20.3.xx. As I am very new to Unix, I guess I must have missed out some important steps. Pls help... Thanx alot (2 Replies)
Discussion started by: gavon
2 Replies

10. UNIX for Dummies Questions & Answers

Unable to ping host

Hi, dear all, I am rather new to Unix and have this problem where I cant seem to ping from 1 host to another. The scenerio is as follows: - 1 QNX host->Eth->1 SCO host the SCO host is configured with it's IP the QNX host is configured with another IP both in the same domain, ie, 172.20.3.XX... (3 Replies)
Discussion started by: gavon
3 Replies
Login or Register to Ask a Question