Skip to content

Instantly share code, notes, and snippets.

@Gardelll
Last active September 16, 2021 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gardelll/e60024af65b15b7203798217371ee448 to your computer and use it in GitHub Desktop.
Save Gardelll/e60024af65b15b7203798217371ee448 to your computer and use it in GitHub Desktop.
Bash脚本: 根据TraceRoute获取IP地址的物理位置
#!/bin/bash
if [[ "$1" == "" ]]; then
echo "未指定域名" 1>&2
echo "$0 Domain" 1>&2
exit 1
fi
traceroute -n > /dev/null 2>&1
if [[ "$?" == "64" ]]; then
cmdArg="-I"
else
cmdArg="-In"
fi
traceroute $cmdArg $1 | tail -n +2 | sed 's/[*]//g;s/[ ][ ]*/,/g;s/^,//' | cut -d ',' -f 2 | while read line
do
if [[ "$line" == "" || $line == 10.* || $line == 192.168.* || $line == 127.* ]]; then continue; fi
if [[ $line == 172.* ]]; then
two=$(echo -n $line | cut -d '.' -f 2)
if [[ $two -ge 16 && $two -le 31 ]]; then continue; fi
fi
data=$(curl -s 'http://ip.tool.chinaz.com/ajaxsync.aspx?at=ipbatch&callback=q' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0' --compressed -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Origin: http://ip.tool.chinaz.com' -H 'Referer: http://ip.tool.chinaz.com/ipbatch' --data-raw "ip=$line" | grep -Eo 'location.+?[,}]' | cut -d "'" -f 2)
echo "$line $data"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment