Ping inside FOR loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ping inside FOR loop
# 1  
Old 12-05-2010
Question Ping inside FOR loop

Hello,

I'm trying to write (my own primitive) traceroute command that would write out all IPs that packets are hoping through. Smilie

So I've put the ping command inside a for loop

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

for ((i=1i<=30i++)) # max. 30 hops
do 
    
ping -t $i $domain # I would later add "grep" to extract IPs
done

exit 0
What is wrong with this script? All I get is 30 "Usage: ping [...]" replies. Smilie

EDIT: I see it: I've written $1 instead of $i. Smilie But don't you worry, I will most surely come back for help with grep! Smilie

Last edited by courteous; 12-05-2010 at 11:23 AM.. Reason: novice-like error
# 2  
Old 12-05-2010
Hi.

I think that should be $i, not $1 in your ping command. But -t is used to set a ping timeout.

According to the man page on my system -R is used to record the route. You might also want to use -c?
This User Gave Thanks to Scott For This Post:
# 3  
Old 12-05-2010
Quote:
Originally Posted by scottn
You might also want to use -c?
First, thank you for the reply.
  1. I've seen other scripts that used, say, -c 2, but I don't (really) understand why (yes, I looked at man ping).

    In ICMP's first 8 bits I'll be getting #11 (TTL expired) and not #0 (ECHO_REPLY), which is what switch -c is referring to. Well, what is wrong with my "logic"?
    ADD: Should I use -c 1 for each hop on the way, is that right?
  2. How to find the right maximum TTL number? Smilie for example, pinging google.com it takes 14 hops. So, if I do the for loop 30 times, the last 16 IPs are all (almost) the same.

Last edited by courteous; 12-05-2010 at 11:46 AM..
# 4  
Old 12-05-2010
You miss a colon :

Code:
domain=${1:-www.google.com}

By the way you should check the ping option available for your OS version
(look at the -c option that let you ping a <count> times so the ping isn't "endless")
# 5  
Old 12-05-2010
Hi ctsgnb,
the "default option" works also without the colon. Smilie

I'd be most grateful for any help on 2. question (on choosing max. TTL) ...
and of course any correction on my "thinking" in 1. point (on -c switch). Smilie

ADD: I think I should let ping run inside the for loop, until I'd get ECHO_REPLY, and then break the for loop.
Code:
PING www.l.google.com (209.85.135.99) 56(84) bytes of data.
From 209.85.253.26 icmp_seq=1 Time to live exceeded

--- www.l.google.com ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms
Here should be the break-like statement: above is TTL_EXPIRED, below ECHO_REPLY. Is that right?
PING www.l.google.com (209.85.135.99) 56(84) bytes of data.
64 bytes from mu-in-f99.1e100.net (209.85.135.99): icmp_req=1 ttl=250 time=45.0 ms

--- www.l.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 45.052/45.052/45.052/0.000 ms


Last edited by courteous; 12-05-2010 at 11:55 AM..
# 6  
Old 12-05-2010
The colon isn't really a requirement in this case:

Code:
/ $ set -- www.bbc.co.uk    
               
/ $ domain=${1-www.google.com}
/ $ echo $domain
www.bbc.co.uk

/ $ domain=${2-www.google.com}
/ $ echo $domain              
www.google.com

O/P: -c has nothing to do with hops, but how many times you want to attempt a ping before stopping.

There is no "right TTL" number. Quote from the ping man page:
Quote:
In normal operation ping prints the ttl value from the packet it receives.
# 7  
Old 12-05-2010
Quote:
Originally Posted by courteous
Hi ctsgnb,
the "default option" works also without the colon. Smilie
I'll sleep a little bit less ignorant tonight Smilie
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