Last active
September 16, 2021 13:10
-
-
Save Gardelll/e60024af65b15b7203798217371ee448 to your computer and use it in GitHub Desktop.
Bash脚本: 根据TraceRoute获取IP地址的物理位置
This file contains 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 | |
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