views:

570

answers:

4

I am trying to parse the "number of hops" value from the traceroute command and output it with a bash script.

Any hints? Very new so thanks.

My script looks like this so far:

#!/bin/bash
#parse traceroute for hops and output to stdout.
echo -n "Enter a host name(like www.google.com):  "
read hostname
traceroute 2>&1 $hostname|grep "hops"
echo "number of hops are "
+1  A: 

The only occurrence of "hops" in traceroute output (stderr, actually) is towards the top, in the stderr "header" line:

traceroute to foo.bar.com (123.12.1.23), 30 hops max, 40 byte packets

or the like -- if that's the one line you want to grab, you might grep for "hops max," or the like in order to reduce the risk of undesired hits (should some intermediate host just happen to have "hops" in their DNS).

Is this what you mean? Is your problem grabbing this 30, the maximum number of hops, into a bash variable? If that's so, then

maxhops=`traceroute www.yahoo.com 2>&1 >/dev/null | grep "hops max" | cut -d' ' -f5`

should help -- you can use $maxhops after this (and it will be 30 in the above example).

But I suspect you mean something completely different -- clarify, maybe...?

Edit: the OP has clarified they want, not maxhops, but the actual number of hops as measured by traceroute; for that purpose,

numhops=`traceroute www.yahoo.com 2>/dev/null | tail -1 | cut -d' ' -f2`

or a simpler

numhops=`traceroute www.yahoo.com | wc -l`

should work fine!

If you do want anything fancier (or more than one result, etc) then awk as suggested in other answers is perfectly fine, but a pipeline mix of grep, tail, and cut, is quite a traditional unix-y approach, and still quite workable for simple cases (and some complex ones, but awk or perl may be more appropriate for those;-).

Alex Martelli
i like the idea of setting a variable of maxhops. Your right, i wnat the final output of the script to display to the screen the following:"The host www.google.com is --- hops away". The ---- would be the hop value parsed from the traceroute command. It may be different depending on the site entered by the user. so i am trying to grab just the number of hops parsed from traceroute and only that data. what do u think?
note that using wc -l is not accurate as the header should not be counted. that said, some times traceroute will show * * * do you want to count that as a hop?
ghostdog74
Alex Martelli
`* * *` hops should be counted, yes. The hop is there, it just doesn't respond to `traceroute`.
John Kugelman
+1  A: 

Are you looking for the number of hops it took, or the "64 hops max" value? If it's the former, use tail to get the last line of output, then awk to print the first column:

traceroute "$hostname" 2>/dev/null | tail -1 | awk '{print $1}'
John Kugelman
A: 
traceroute www.google.com 2>/dev/null | awk 'NR==1{print $5;exit}'
ghostdog74
A: 
another way, where those hops not reachable (for lack of better word) are not counted

# traceroute www.yahoo.com 2>/dev/null | awk 'NR>1 && !/* * */{c++}END{print c}'
  16
ghostdog74