Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
#!/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
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..
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?
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.
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"
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.
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:
#!/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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.