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
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
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.
#!/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.
Jim, I don't want to start a long discussion about personal likes/dislikes, but let me state I would write it like this
#!/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.
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:
[ $! -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:
[ $? -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:
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:
ping -c 1 -w 5 AIXservername
produces output similar to:
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:
#!/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:
#!/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:
now=`$date +%s`
and MadeInGermany's script also contains:
while now=`$date +%s`; [ $now -le $stop ]
both of which are likely to produce a diagnostic similar to:
-ksh: +%s: not found
In bash, we can do like this
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