Script for pinging continuously

Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 04-04-2016
Script for pinging continuously

Hi,

I need to set up a script that would write the results of the ping command from one AIX server to another file may be every minute. Like this I need to gather the data for a period of 24 hours.
Can someone please help me with this?

G
# 2  
Old 04-04-2016
Okay. Since ping -c 1 -w 5 (where -c is the number of ping attempts and -w is the timeout in seconds) seems to be the AIX way to ping and limit the time it takes to complete the operation:

One day is 86400 seconds.

Code:
#!/bin/ksh    
now=`date +%s`
stop=$(( $now +  86400 ))
while true
do
   now=`$date +%s`
   if [ $now  -gt $stop ] 
   then
       break
   fi
   ping -c 1 -w 5 AIXservername 
   [ $! -eq 0 ] && OK='pass' || OK='fail'
   echo "$OK at `$date`" >>mylogfile
done

See if that meets your needs.
# 3  
Old 04-04-2016
Jim, I don't want to start a long discussion about personal likes/dislikes, but let me state I would write it like this
Code:
#!/bin/ksh
now=`$date +%s`
stop=$(( $now +  86400 ))
while now=`$date +%s`;  [ $now  -le $stop ]
do
   if ping -c 1 -w 5 AIXservername
   then
      OK='pass'
   else
      OK='fail'
   fi
   echo "$OK at `$date`" >>mylogfile
done

The cryptic [ $! -eq 0 ] && OK='pass' || OK='fail' does unnecessary evaluation of OK='pass', and would fail if such an expression would have a non-zero exit status. In comparison, if-then-else-fi is easy to understand and risk-free.
# 4  
Old 04-06-2016
Guys, one out off topic question and maybe rather for forum administrators, but why are these questions accepted in "Emergency UNIX and Linux Support" thread?

I fully understand and accepting need of emergency consultancy with other professionals (at all it is why we are all here), but what I see is that ggayathri has nice habit to raise all his questions directly here (and personally I don't see questions related to tcpdump, netstat and Gmail marking mails coming from his server as spam as something really emergent).

Don't want to be picky, but this is degrading quality and main goal of this thread.
# 5  
Old 04-06-2016
Quote:
Originally Posted by brusell
Guys, one out off topic question and maybe rather for forum administrators, but why are these questions accepted in "Emergency UNIX and Linux Support" thread?

I fully understand and accepting need of emergency consultancy with other professionals (at all it is why we are all here), but what I see is that ggayathri has nice habit to raise all his questions directly here (and personally I don't see questions related to tcpdump, netstat and Gmail marking mails coming from his server as spam as something really emergent).

Don't want to be picky, but this is degrading quality and main goal of this thread.
These questions are allowed in the Emergency UNIX and Linux Support forum because the question is important to the submitter and the submitter is willing to pay for the privilege of posting in this forum.

Back on topic...

I don't have any objection to Jim's use of:
Code:
   [ $! -eq 0 ] && OK='pass' || OK='fail'

except that it is using the wrong shell variable. $! expands to the process ID of the last asynchronously started command (and there are no asynchronously started commands in this script). The exit code from the last executed command is $?, so I believe the intent was:
Code:
   [ $? -eq 0 ] && OK='pass' || OK='fail'

and, the request was for a report about once a minute. Both suggested scripts will produce a report about 12 times per minute (when the target system is not responding) to a few hundred times per minute (when the target system is responding). And, since the shell variable date is not set in MadeInGermany's script or in jim mcnamara's script, the use of:
Code:
   echo "$OK at `$date`" >>mylogfile

just adds a line containing "pass at " or "fail at " (while I assume the intent in both cases was to include the status and the current date and time) in the log file.

Furthermore, the command:
Code:
ping -c 1 -w 5 AIXservername

produces output similar to:
Code:
PING google.com (172.217.2.46): 56 data bytes

--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss, 1 packets out of wait time
round-trip min/avg/max/stddev = 29.182/29.182/29.182/0.000 ms

(using google.com instead of AIXservername since AIXservername is not the name of a host known on my network) which is written to standard output from this script. Since I assume this script will be started in the background, spewing output like this to standard output seems undesirable.

I would be tempted to use something more like:
Code:
#!/bin/ksh    
now=$(date +%s)
stop=$(( now + 86400 ))
while [ $now -le $stop ]
do
   ping -c 1 -w 5 AIXservername > /dev/null && OK='pass' || OK='fail'
   echo "$OK at $(date)" >>mylogfile
   sleep 60
   now=$(date +%s)
done

or, if detailed ping output is desired in the log instead of just one word of status:
Code:
#!/bin/ksh    
now=$(date +%s)
stop=$(( now + 86400 ))
while [ $now -le $stop ]
do
   echo "*** $(date)" >> mylogfile
   ping -c 1 -w 5 AIXservername >> mylogfile
   sleep 60
   now=$(date +%s)
done

PS
Note that Jim's script also contains:
Code:
   now=`$date +%s`

and MadeInGermany's script also contains:
Code:
while now=`$date +%s`;  [ $now  -le $stop ]

both of which are likely to produce a diagnostic similar to:
Code:
-ksh: +%s: not found


Last edited by Don Cragun; 04-07-2016 at 02:24 AM.. Reason: Fix typos: s/pass " or "fail /pass at" or "fail at / and s/'*** $(date)/"*** $(date)"/
This User Gave Thanks to Don Cragun For This Post:
MadeInGermany (04-06-2016)
# 6  
Old 08-12-2016
In bash, we can do like this

Code:
for i in {1..3600}; do ping -c1 my_host_name >/dev/null 2>&1 && echo "$(date) : pass" >> output.txt || echo "$(date) : fail" >>output.txt; sleep 60; done


Last edited by itkamaraj; 08-26-2016 at 03:42 AM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Pinging 5 hosts once every hour Katkota Shell Programming and Scripting 1 02-17-2017 03:19 AM
Pinging Host 5sku5 UNIX Desktop Questions & Answers 5 12-07-2015 01:04 PM
Shell Script for continuously checking status of a another script running in background, and immedia ketanraut Shell Programming and Scripting 9 11-13-2013 12:54 AM
Need help in running a script continuously non stop sandeepcm Shell Programming and Scripting 10 02-21-2013 07:52 AM
Run a script continuously for 10 minutes Jayaraman Shell Programming and Scripting 5 07-23-2012 01:14 PM
Pinging IP located on another interface NH2 IP Networking 3 07-06-2012 06:56 AM
Pinging a domain vanessafan99 Shell Programming and Scripting 11 12-30-2011 05:47 AM
Server Pinging Problem sudhansu Solaris 1 02-22-2011 03:30 AM
Forking and Pinging kooshi Shell Programming and Scripting 7 09-08-2010 06:12 PM
Shell Script to continuously scan a log file novice82 Shell Programming and Scripting 1 10-09-2009 01:03 AM
External NIC cards not pinging with each other in RHEL 5.1 durgaprasadr13 Red Hat 3 11-05-2008 09:03 AM
Alternate to pinging boxes benefactr UNIX for Advanced & Expert Users 5 12-13-2007 03:40 AM
pinging an ip ammu Shell Programming and Scripting 1 07-19-2007 02:58 PM
pinging IP's in a file jalge2 AIX 2 06-21-2005 03:47 PM
Server stopped pinging (in and out) skotapal UNIX for Advanced & Expert Users 2 11-14-2002 09:22 AM