Simple ping script

I want to do the following operation with a simple bash script:

- ping an host [with count 4]
- when ping returns total time passed [such as 515 ms for all 4 packets]
- get this result and compare it with a number, if eg. this total time is higher than 750ms
- send an alarm to syslog [that syslog part is not important, just echoing something to screen would be enough]

I think it should be possible without referring to C language.

Any "working" source code (in this case a bash script) shall resolve...
XephyrAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
avizitConnect With a Mentor Commented:
try the following csh script
there might be problems with the print $10 . for that you have to decide for yourself what to put $10 or $11 whatever depending on the output of your  "ping -c 4 ip" command ( the format of output might vary from system to system) .
alternatively you can paste the putput of the command here so i can modify the script to suit you


#!/usr/bin/tcsh
ping -c 4 localhost > /tmp/test.txt
set TOTALTIME=`grep transmitted /tmp/test.txt | awk '{print $10}' |  sed s/ms//g`
echo "totaltime:$TOTALTIME"
if( $TOTALTIME > 750 ) then
        echo "warning"
endif
0
 
avizitCommented:
You can essily write a script with

ping -c 4  hostname > /tmp/tmp.file


thefile shold have something like

PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.040 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.039 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.036 ms

--- localhost ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2997ms
rtt min/avg/max/mdev = 0.036/0.037/0.040/0.007 ms



and then extract the secopnd last line by grepping for "transmitted"

and then using awk to get the time
and then compare it with the known value
and echo


0
 
avizitCommented:
roughly your script should look like

#!/usr/bin/bash

ping -c 4 host > /tmp/test.txt
TOTATLTIME= `grep transmitted /tmp/test.txt | awk '{print $10}' |  sed s/ms//g`
if $TOTALTIME   -gt   750
then
    echo "warning"
fi


===
the if command syntax change to whatever is correct . i am not sure bout bash
also thet print $10 has to be changed according to the output of theoping command on your system


   

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
avizitCommented:
also yuo should delete the temp file after you are done

rm -f /tmp/test.txt
0
 
XephyrAuthor Commented:
That syntax for that script seems incorrect, it could also be in any other syntax including csh, zsh or ash. It will be better if you post an exactly correct and tested script so that the solution archived will not be wrong... (I thought about using these utils but the important point is syntax)

I would grade and close this question soonafter...
0
 
XephyrAuthor Commented:
Also you can see my question I posted at Linux Security named as packet inspecting firewall with IPTables...
0
 
XephyrAuthor Commented:
Very well..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.