Ping inside FOR loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ping inside FOR loop
# 8  
Old 12-05-2010
Quote:
Originally Posted by scottn
O/P: -c has nothing to do with hops, but how many times you want to attempt a ping before stopping.
So, for emulating traceroute it should be -c 1, right?

Can you help me with my 2nd question: how could I "break" from the for loop when above quoted change (from TTL_EXPIRED to ECHO_REPLY) occurred? Perhaps with a help from grep? How? Smilie
# 9  
Old 12-05-2010
You could (probably!) say:

Code:
ping .... | grep ECHO_REPLY && break

But I'm not really understanding why you have a for-loop in the first place to ping the same thing 30 times...
This User Gave Thanks to Scott For This Post:
# 10  
Old 12-05-2010
Help with grep

Quote:
Originally Posted by scottn
You could (probably!) say:
Code:
ping .... | grep ECHO_REPLY && break

There isn't actual word "ECHO_REPLY" but a line that starts with "64 bytes from [...]" when the script reaches the target computer (eg. google.com). Now, how do you write an if statement like:
PHP Code:
if (line starts with "64 bytes from [...]")
do
    break;
fi 
Smilie

Quote:
Originally Posted by scottn
But I'm not really understanding why you have a for-loop in the first place to ping the same thing 30 times...
You're right, I should use while or until, but I don't know how to express the terminating if statement.
But I'm not pinging "the same thing", because I increase TTL in each iteration and get new servers where the packet "expires".

This is (almost) it:
PHP Code:
#!/bin/bash
domain=${1-www.google.com}; #If no argument, then do www.google.com. 

for ((i=1i<=30i++))
do 
    
ping -c 1 -t $i $domain grep -'[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
done

exit 0
Above script outputs this:
Code:
209.85.135.147 # target: google.com
192.168.1.1      # default gateway
209.85.135.147 # wrong
85.10.0.254
209.85.135.105 # wrong
95.176.241.10
209.85.135.106 # wrong
209.85.135.106 # wrong: twice in a row because the 4th hop returns all stars (* * *) as in built-in traceroute
85.10.0.73
209.85.135.105 # wrong
85.10.0.74
209.85.135.103 # wrong
212.18.32.97
209.85.135.99 # wrong
212.18.39.214
209.85.135.147 # target reached: all below are unnecessary PINGs
72.14.219.148
209.85.135.99 
etc.

EDIT: Wait, I'm doing it wrong: I also pickup google's IP from each line "PING www.l.google.com (209.85.135.103) 56(84) bytes of data.". How do I exclude these IPs with grep?

Last edited by courteous; 12-05-2010 at 04:01 PM.. Reason: found error(s)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Loop in /24 Subnet, No ping beyond .1 and .2

Running 3650 switch. I have this odd issue where I cannot get 4 new Centos 7 boxes pinging out on public IPs (nor pinging in), only gateway .1 and first public IP .2 This is what I see, which doesn't look normal. How do I fix this? The server itself is configured fine (Centos 7) # cat... (0 Replies)
Discussion started by: Bashed
0 Replies

2. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

3. Shell Programming and Scripting

If inside If loop

Hi All, Below is the very simple code snippet but it si giving me syntax error #!/bin/bash #To ensure If JMS directory exists or not ServerName=$(hostname) #To ensure If JMS directory exists or not echo $ServerName if ; then echo "Inside First If" if ; then echo 'JMS... (4 Replies)
Discussion started by: sharsour
4 Replies

4. Programming

Perl Ping Loop

Hi All i have an issue with ping, we are using dhcp and so if the machine has been offline and i ping it, i get " ping: unknown host <hostname> is there a way i can stick a loop somewhere so it would keep trying when it got the unknown host error and then when the machine came back online... (2 Replies)
Discussion started by: ab52
2 Replies

5. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

6. Shell Programming and Scripting

Using 'su' inside a loop

Hi, I am using su within a for loop. As you might expect, it prompts for password during each loop execution. Here is my piece of code: for i in $LIST do if then DATABASE=`echo $i | awk -F "|" '{ print $1 }'` USER_ID=`echo $i | awk -F "|" '{ print $2 }'` su - apstage -c... (1 Reply)
Discussion started by: sugan
1 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

9. Shell Programming and Scripting

help: infinant loop script - host ping test

need to check on some hosts and send an email if there status changes I wanna put together a script in bash that will allow me to check the up/down state of a single host via ping i want it to run in a continuous loop so I can just fire the script and forget about it(dont want cron to drive... (2 Replies)
Discussion started by: zeekblack
2 Replies

10. IP Networking

ping loop

i need to write a script that can loop through ej.192.162.0.0 to 192.162.0.256 find the ones that are connected and the ones not connected write the name , ip address and connection status of each one in a file to later use it in perl to create a web page with the information thanks guys (1 Reply)
Discussion started by: djbilly
1 Replies
Login or Register to Ask a Question