LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-01-2006, 08:04 AM   #1
mcrosby
Member
 
Registered: May 2006
Posts: 39

Rep: Reputation: 15
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?
 
Old 06-01-2006, 08:44 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
 
Old 06-01-2006, 09:32 AM   #3
mcrosby
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
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..
 
Old 06-01-2006, 11:08 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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.
 
Old 06-01-2006, 01:29 PM   #5
mcrosby
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
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?
 
Old 06-01-2006, 02:14 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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.
 
Old 06-01-2006, 02:36 PM   #7
mcrosby
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
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
 
Old 06-02-2006, 06:11 AM   #8
mcrosby
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
Is there anyway i can have it show the pinging information as well as the echo'd text?
 
Old 06-02-2006, 07:03 AM   #9
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 06-02-2006, 07:24 AM   #10
mcrosby
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
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?
 
Old 06-02-2006, 10:13 AM   #11
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 06-02-2006, 10:42 AM   #12
mcrosby
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
hmm im not really sure what you mean
 
Old 06-05-2006, 06:58 AM   #13
mcrosby
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
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?
 
Old 06-05-2006, 01:13 PM   #14
mcrosby
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
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?
 
Old 06-05-2006, 02:14 PM   #15
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Keep Alive Ping Script Apollo77 Linux - Networking 8 05-19-2006 12:29 PM
How to create a ping-tracking script..? daleo Programming 5 11-17-2005 03:03 AM
Linux ping script MPowers Linux - Networking 6 07-02-2005 04:18 PM
ping script richiehawtin Programming 7 08-28-2004 02:39 AM
Ping Random IPs using a tcsh script.. zeppelin Programming 7 08-07-2003 11:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration