Looping with until and grep:
I am confused about the reason why the following script is reaching the "red" printf for some IPs in the file. This happens although the string "packet loss" that grep looks for ALWAYS exists in with all IPS! In otherwords, ALL hosts should stop from the first until loop (i.e., TRIES = 2).
Code:
#!/bin/sh |
My question would be ..what the???
I am not understanding how you ever enter the loop? As you are using 'until' and the string "packet loss" is always in the data it will always return true hence you should never enter the loop? Ahhhh ... a little testing has revealed a light bulb. When ping finds an ip that it cannot reach it will error, however the grep will always find "packet loss" in the resulting file as it always exists, hence, the anded (&&) statement is false so until will process the loop again. As this ip will never change until the next is read from the file after the completion of the until loop, the TRIES will eventually be greater than 3 and so the if is launched. Firstly I would think it better to simply pipe the ping output to grep as a temporary file serves no purpose here, however, it may be prudent to think about what it is you are actually trying to test for?? |
grail: The ping if you are not aware enough, even with unreachable hosts it reports 100% packet loss so the statement is there. In contrary, the RTT value will not be there.
Regarding using until and grep, this is working with reachable hosts (i.e., less than 100% packet loss), BUT not working with unreachable hosts! |
Try using packet\ loss instead of "packet loss" in you grep command.
And, per grail's comment, here's a somewhat simpler bash version of your script: Code:
And here's the output for a segment of my local network Code:
]$ bash ali2011.sh |
Here's another look, with an unreachable host in the list. (Note the redirection of the ping error output.)
Code:
#!/bin/bash Code:
$ bash ali2011.sh |
Thanks PTrenholme:
1- I didn't see the point of piping the error message? 2- I need to pipe the ping's output to a ${IP}.loss whether it succeeded or not. 3- Does the [ $? -eq 0 ] && break act as if statement? |
Also the string "packet loss" should always appear no matter ping reached the host or not as I mentioned above! Because of that I can't understand why you have in your output "[8] IP = www.AReallyStrangeIS.nonsense (1 failed) (2 failed)"!
If you pipe the output of that host, you should find the string "packet loss"; however if you replace this string by "rtt" your current output makes sense. |
First off, I'd like to point out that you can probably replace your script with a one line call to nmap. It will not only be simpler, but faster too.
Quote:
Code:
~$ ping -c 2 -i 0.025 www.AReallyStrangeIS.nonsense Quote:
Quote:
|
ntubski
Regarding the statement I stated "Also the string "packet loss" should always appear no matter ping reached the host or not as I mentioned above!" You said that the string "packet loss may not appear" as in: Quote:
In my experiment, however, all hosts in the form of public and numeric IPs (i.e., no DNS task is required on them) under my network so this sort of error will not exist. |
Quote:
|
Quote:
1. If you read my post again you might see that I agreed with your first point that "packet loss" will always be there (barring ntubski's example) Quote:
However, in the even that the ping is unsuccessful, ie IP not reachable, the grep is never executed as the AND is false if the first expression is false, hence you will receive an infinite loop and therefore pass TRIES >= 3 so the 'if' is executed I hope this helps along with the other suggestions of how to fix ;) btw. In PTrenholme's code, you do not need to separately test the exit code as until (or if in his example) can test it for you :) Code:
until sudo ping -i 0.025 -c 2 ${IP} | grep -q 'packet loss' |
All times are GMT -5. The time now is 03:19 AM. |