Script for pinging continuously

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Script for pinging continuously
# 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 03: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:
# 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 04:42 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script for continuously monitoring log file

Hi I have written below log monitoring script to egrep multiple words and redirect the output to a text file and its working fine but I want to add some more below given functionality to it, which is very advance and im not very good in it, so please help if you can :) I am egrepping all the... (1 Reply)
Discussion started by: scazed
1 Replies

2. UNIX Desktop Questions & Answers

Pinging Host

I need to ping websites and I need to see which one has the highest delay. My problem is I need to extract the name Facebook and the time=74.0 ms using awk. I need help doing this please... PING facebook.com (173.252.90.36) 56(84) bytes of data. 64 bytes from... (5 Replies)
Discussion started by: 5sku5
5 Replies

3. Shell Programming and Scripting

Shell Script for continuously checking status of a another script running in background, and immedia

Hi, I want to write a script which continuously checking status of a script running in background by nohup command. And if same script is not running then immediately start the script...please help.. i am using below command to run script nohup system_traps.sh & but in some... (9 Replies)
Discussion started by: ketanraut
9 Replies

4. Shell Programming and Scripting

Need help in running a script continuously non stop

Hi, I am running a schedular script which will check for a specific time and do the job. I wanted to run this continuously. Meaning even after the if condition is true and it executes the job, it should start running again non stop. I am using below script #!/bin/sh start: while true do... (10 Replies)
Discussion started by: sandeepcm
10 Replies

5. Shell Programming and Scripting

Run a script continuously for 10 minutes

Hi all!! Im using ksh and my OS is Linux. I want to run a script for ten minutes, starting from my current system time. How to acheive this? Any help appreciated. Thanks in advance (5 Replies)
Discussion started by: Jayaraman
5 Replies

6. Shell Programming and Scripting

Pinging a domain

how can you ping a domain and store the ip? like given a url in a variable $url how can i ping it? also how can i find the local server's ip address on a cpanel server? (i have multiple servers and didnt want to hard code it in) (basically i want to check the domain accounts on the server,... (11 Replies)
Discussion started by: vanessafan99
11 Replies

7. Shell Programming and Scripting

Forking and Pinging

Keep in mind that I haven't done Perl scripting for a LONG time, so I'm quite rusty. This is what I would like to do: - using fork, create 3 or 4 processes to read 3 or 4 different text documents containing server names or IP addresses - in each of those processes, Perl will ping each of those... (7 Replies)
Discussion started by: kooshi
7 Replies

8. Shell Programming and Scripting

Shell Script to continuously scan a log file

Hello members, I have some doubts on how to write a script that can reports success / failure of a batch job ? 1. Run a batch job: 2. Wait and search for a particular string in the Log file: tail -f log01*.txt | egrep -v "^SUCCESSFUL" echo "continue with the other tasks" ... (1 Reply)
Discussion started by: novice82
1 Replies

9. Shell Programming and Scripting

pinging an ip

Hi, How to ping an ip from an unix machine. Can you please let me know the exact command. I used and i got the below error ping 171.18.17.2 bash: ping: command not found Thanks n regards Ammu (1 Reply)
Discussion started by: ammu
1 Replies

10. AIX

pinging IP's in a file

Hey all. I have a long list of IP addresses I want to ping. The IP's are located in a flat file "ping_info.dat". I was wondering what the best way to go about this would be. Can someone help me out? (2 Replies)
Discussion started by: jalge2
2 Replies
Login or Register to Ask a Question