Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simple ping script

Posted on 2004-08-23
7
Medium Priority
?
780 Views
Last Modified: 2008-02-26
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
Comment
Question by:Xephyr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11877747
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
 
LVL 11

Expert Comment

by:avizit
ID: 11877790
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
 
LVL 11

Expert Comment

by:avizit
ID: 11877798
also yuo should delete the temp file after you are done

rm -f /tmp/test.txt
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:Xephyr
ID: 11877887
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
 

Author Comment

by:Xephyr
ID: 11877920
Also you can see my question I posted at Linux Security named as packet inspecting firewall with IPTables...
0
 
LVL 11

Accepted Solution

by:
avizit earned 620 total points
ID: 11878001
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
 

Author Comment

by:Xephyr
ID: 11878214
Very well..
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question