Created
September 21, 2012 14:53
-
-
Save hydra35/3761943 to your computer and use it in GitHub Desktop.
lookup traceroute result using bgp.he.net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
lookup_single_ip() { | |
local ip=$1; shift | |
# 从bgp.he.net获取AS信息. e.g., <td>CHINANET Shanghai province network</td> | |
local data=$(curl -L http://bgp.he.net/ip/$ip 2>/dev/null | grep -A 30 'Announced By' | egrep '^\s*<td>.*</td>\s*$' | egrep -v 'AS[0-9]*</a>' | uniq) | |
# 从<td>标签中取值. e.g., CHINANET Shanghai province network | |
Lookup_single_ip=$(echo $data | awk -F '<' '{print $2}' | awk -F '>' '{print $2}') | |
} | |
do_traceroute() { | |
local ip=$1; shift | |
local src_ip=$1; shift | |
if [[ -n $src_ip ]]; then | |
Do_traceroute=$(traceroute -s $src_ip -w 2 -n $ip 2>/dev/null | grep -v '* * *' | grep -v '^traceroute to' | awk '{print $2}' | egrep -v '^192.168' | egrep -v '^10.' | egrep -v '172.1[6789]' | egrep -v '172.2[0-9]' | egrep -v '172.3[12]') | |
else | |
Do_traceroute=$(traceroute -w 2 -n $ip 2>/dev/null | grep -v '* * *' | grep -v '^traceroute to' | awk '{print $2}' | egrep -v '^192.168' | egrep -v '^10.' | egrep -v '172.1[6789]' | egrep -v '172.2[0-9]' | egrep -v '172.3[12]') | |
fi | |
} | |
do_showpath() { | |
local ip=$1; shift | |
local src_ip=$1; shift | |
do_traceroute $ip $src_ip | |
i=1 | |
white_space= | |
for l in $Do_traceroute | |
do | |
lookup_single_ip $l | |
echo "$i: $Lookup_single_ip($l)" | |
(( i++ )) | |
done | |
} | |
if [[ ! -n $1 ]]; then | |
echo "Usage: $0 ip srcip" | |
echo " $0 ip_list_file srcip" | |
else | |
if [[ -f $1 ]]; then | |
for ip in `cat $1` | |
do | |
echo "showpath to: $ip" | |
do_showpath $ip | |
done | |
else | |
echo "showpath to: $1" | |
if [[ -n $2 ]]; then | |
do_showpath $1 $2 | |
else | |
do_showpath $1 | |
fi | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment