LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   ping script (https://www.linuxquestions.org/questions/linux-general-1/ping-script-450536/)

mcrosby 06-01-2006 08:04 AM

ping script
 
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?

homey 06-01-2006 08:44 AM

Something like this may get you started....
Code:

#!/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


mcrosby 06-01-2006 09:32 AM

I also forgot to mention that i am new to shell scripting so I am going to need you to explain that script a bit for me i am not sure were you have those variables going etc..

homey 06-01-2006 11:08 AM

Probably, your best bet would be to check out the Advanced Bash-Scripting Guide at http://www.tldp.org/LDP/abs/html/ .

If you access the manual pages with a command like: man set, it will list a bunch of goodies called ....
BASH_BUILTINS(1)

In that script, I made a variable called host so you can easily type in different locations to ping.

The while : is same as saying while true so it just keeps going until ended by the exit statement or control c .

I just created a variable called info which has the output of ping -q ( quiet ) -c3 ( give me 3 pings ). grep packets is used because I only want the line which has packets in it. cut -d" " ( uses the space as a delimiter or separator of data on that line ) -f1,4 ( only shows data in field 1 and 4 which are the numeric values of packets transmitted and packets received ) .

set -- $info ( check the man page ) is just used to put the values of $info from cut -f1,4 into variables which I can compare to see if they're equal or not.

The if statement says if those values are not equal, then do something. If they are equal, do something else.

mcrosby 06-01-2006 01:29 PM

ok i understand pretty much all of that now that you explained it thanks..the one thing i dont understand is how does the latter option know to repeat the loop?..other than that i havent tried this out yet but by the looks of it..it looks to be exactly what i want..that script should work as is correct?

homey 06-01-2006 02:14 PM

while : ; do

The loop is always true and will continue to loop until there is something to make it quit. Like this....
if [ $1 != $2 ]; then
echo "transmitted and received packets don't match, exiting now"
exit

Normally, you would have a built in exit like this...
Code:

while ((n < 4)) ; do
  let n=$n+1
or

for ((i=1; i<=$1; ++i)) ; do
  let n=$n+1

That script does work as is but, I would put an exit in there so it will quit after a few rounds.

mcrosby 06-01-2006 02:36 PM

ahh yes my fault i forgot about the loop...thank you for your help i will let you know if I have any further issues

mcrosby 06-02-2006 06:11 AM

Is there anyway i can have it show the pinging information as well as the echo'd text?

timmeke 06-02-2006 07:03 AM

You could add a "tee" command between "ping" and "grep" to print the ping info to a file as well...
ie
Code:

info=$(ping -qc3 $host |tee -a some_file |grep packets| cut -d" " -f1,4)
Of course "some_file" should be replaced by a more suitable filename (and path).

If you then launch your script into the background, you can track the pings by doing:
Code:

tail -f some_file
Tail lists the last lines in a text file. -f makes tail "follow" and print any newly appended lines.

mcrosby 06-02-2006 07:24 AM

how do I get the program to display on the screen 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 is that possible?

timmeke 06-02-2006 10:13 AM

Sure. But personnally I'd start using Perl (or at least awk) for such kinds of text interpretation.
It's possible in Bash (or other shells) too.

Basically, you'll need to capture all of the output of ping into a variable and then
use grep (or array indices) to verify each value.

The info= line should thus look something like:
Code:

info=$(ping $@ $host); #$@ passes the variables that you gave to your script on to "ping"
The tricky bit here is that the shell normally splits up based on words (space delimiter). You'll need to change some environment var to make it split up on lines only. Otherwise, you can't use multiple grep's & cut's on $info
to get each value (ie echo $info|grep packets|cut -d" " -f1,4; followed by a similar command to extract another value from the line).
An alternative would be to use indexing on $info.

mcrosby 06-02-2006 10:42 AM

hmm im not really sure what you mean

mcrosby 06-05-2006 06:58 AM

ok so right now I have what homey gave me with a few changes 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?

mcrosby 06-05-2006 01:13 PM

ok so lets say I stripped down the script a little more and came to this

Quote:


#!/bin/bash

host=db.macom.com

ping $host $1 $2 $3 $4

Now how would i implement the other script to check if the packets are equal or not.. I mean i get how homey did it with the grep command but I can't implement that for my script because i wanted all of the information displayed right? So how do I reference that packets part without the grep command up there... and then once i can access the packets line can I use that delimeter to reference the transmitted and received again?

homey 06-05-2006 02:14 PM

Not entirely sure what you are trying to do. Maybe something like this....
Code:

#!/bin/bash

host=asus

while (( n < 4)) ; do
  let n=$n+1

(ping -c3 $host 2>&1) | tee file.txt

cat file.txt |grep packets | cut -d" " -f1,4 |\
while read info ; do

set -- $info
#echo $1 $2

if [ $1 != $2 ]; then
  echo "transmitted and received packets don't match, exiting now"
  exit
else
  if [ $n -lt 4 ]; then
      echo "transmitted and received packets do match, repeating loop"
  fi
fi

done
done



All times are GMT -5. The time now is 03:15 AM.