View Full Version : ping script
mcrosby
1st June 2006, 16:07
hello, I am looking to make a script that will ping a remote ip address. Upon completion of the ping I want the program to either ping again if the # of packets transmitted is equal to the number of packets received or exit if the two values are unequal and information was lost. I am not sure if I should go about this program with a shell script or a C script or any other alternative. In either case would someone be able to point me in the correct direction?
falko
1st June 2006, 17:50
If you use a shell script, you might have to parse the ping output with awk and/or sed.
But it might be easier to use a PHP script for it. Have a look at the functions exec(), system(), shell_exec, and passthru().
mcrosby
1st June 2006, 18:24
I am very new to scripting so to put it bluntly I have no idea what you are talking about lol..I am familiar with PHP but i do not know how to code it. Are those functions I should take a look at in PHP or shell script
The commands that falko posted are php native functions :)
http://ch2.php.net/manual/en/ref.exec.php
mcrosby
5th June 2006, 15:02
ok so i wanted to use bash scripting and i received a bit of help thus far.
#!/bin/bash
host=yahoo.com
echo "Pinging $host ; Use ctl c to end the loop"
while : ; do
info=$(ping -qc3 $host |grep packets| cut -d" " -f1,4)
set -- $info
#echo $1 $2
if [ $1 != $2 ]; then
echo "transmitted and received packets don't match, exiting now"
exit
else
echo "transmitted and received packets do match, repeating loop"
fi
done
but i am looking to get the program to display the results in the terminal window. I would like it to display the normal ping information the size as well as the interval. I would also like it so that when the user runs the program they would do "nameofprogram -c3 -i0.05 -s800 ... so they take those commands from the command line and then the output is the size, the interval, and the packet size and then it gives the message if they are equal or not can anyone help?
falko
5th June 2006, 15:51
I would also like it so that when the user runs the program they would do "nameofprogram -c3 -i0.05 -s800 ... so they take those commands from the command line and then the output is the size, the interval, and the packet size and then it gives the message if they are equal or not can anyone help?Have a look here: http://www.tldp.org/LDP/abs/html/internalvariables.html#ARGLIST
mcrosby
5th June 2006, 16:31
hmm that page would help more if there were discriptions of what they were doing
falko
5th June 2006, 19:47
You can refer to arguments you pass to your script with $1, $2, ... $0 is the script itself.
mcrosby
5th June 2006, 20:56
thanks Falco that did in fact prove to help
mcrosby
6th June 2006, 15:04
ok so now for example my output is:
64 bytes from host: icmp_seq=0 ttl=64 time=2.22 ms
64 bytes from host: icmp_seq=1 ttl=64 time=0.537ms
64 bytes from host: icmp_seq=2 ttl=64 time=0.575 ms
64 bytes from host: icmp_seq=3 ttl=64 time=0.585 ms
64 bytes from host: icmp_seq=4 ttl=64 time=0.578 ms
And then once the ping either times out or the user presses cntrl C you get
5 packets transmitted, 5 received, 0% packet loss, time 12009ms
etc...
My question is how do I reference that number of packets transmitted and received so that I can calculate if they are equal or not. I know i would put those values into variables but what do I set each variable equal too?
falko
7th June 2006, 15:23
I think you must parse this somehow:
5 packets transmitted, 5 received, 0% packet loss, time 12009ms
Maybe there's a regular expression for it, but unfortunately I'm not a regular expresions expert... :(
msfz751
7th January 2007, 23:03
I got the idea with the posts above and came up with this function:
#!/bin/bash
# 401_ping_function.sh
# 07.JAN.07 Original version modified to use it as a function
host=zeus
#================================================= ========================
function is_host_alive() # Returns success or failure as boolean
#================================================= ========================
# Called as: is_host_alive my_host_name
# Arg_1 = remote_host_to_test
{
count=3
loopit=5
qhost=$1 # saving contents in $1 before is used
echo "Pinging $qhost ; Use ctl c to end the loop"
i=0
while [ $i -lt $loopit ]
do
info=$(ping -qc$count $qhost |grep packets| cut -d" " -f1,4)
set -- $info
echo -e "$i: $1 $2 \t"
if [ $1 != $2 ]; then
echo "transmitted and received packets do not match"
# exit
exitcode=1
else
echo "transmitted and received packets do match"
exitcode=0
fi
i=$(expr $i + 1)
done
return $exitcode
}
##################
# main starts here
##################
if is_host_alive $host
then
echo "host alive"
else
echo "host "
fi
falko
9th January 2007, 00:27
Instead of specifiying the hostname in the script (host=zeus) You could use host=$1
You would then call the script like this:
/path/to/401_ping_function.sh server1.example.com
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.