Solved

Simple ping script

Posted on 2004-08-23
7
706 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
  • 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now