Solved

Simple ping script

Posted on 2004-08-23
7
762 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 155 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

695 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