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?
|
Something like this may get you started....
Code:
#!/bin/bash |
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..
|
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. |
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?
|
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 |
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
|
Is there anyway i can have it show the pinging information as well as the echo'd text?
|
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) If you then launch your script into the background, you can track the pings by doing: Code:
tail -f some_file |
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?
|
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" 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. |
hmm im not really sure what you mean
|
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?
|
ok so lets say I stripped down the script a little more and came to this
Quote:
|
Not entirely sure what you are trying to do. Maybe something like this....
Code:
#!/bin/bash |
All times are GMT -5. The time now is 03:15 AM. |