[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

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...
0
Xephyr
Asked:
Xephyr
  • 4
  • 3
1 Solution
 
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
 
avizitCommented:
also yuo should delete the temp file after you are done

rm -f /tmp/test.txt
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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
 
avizitCommented:
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
 
XephyrAuthor Commented:
Very well..
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now