網上有很多方法能夠過去到IP地址歸屬地的腳本,但是我發現淘寶IP地址庫的信息更詳細些,所以用shell寫個腳本來處理日常工作中一些IP地址分析工作。
腳本首先是從http://ip.taobao.com/的數據接口獲取IP地址的JSON格式的數據信息,在使用一個python腳本來把Unicode字符轉換成UTF-8編碼。
Shell腳本內容:
ipInfo() {
for i in `cat list`
do
TransCoding="/usr/bin/python TransCoding.py"
JsonDate="curl -s http://ip.taobao.com/service/getIpInfo.php?ip=$i"
country=`$JsonDate | sed 's/,//n/g' | $TransCoding | tr -d "{}/"" | awk -F ":" 'NR==2{print $3}'
area=`$JsonDate | sed 's/,//n/g' | $TransCoding | tr -d "{}/"" | awk -F ":" 'NR==4{print $2}'
region=`$JsonDate | sed 's/,//n/g' | $TransCoding | tr -d "{}/"" | awk -F ":" 'NR==6{print $2}'
city=`$JsonDate | sed 's/,//n/g' | $TransCoding | tr -d "{}/"" | awk -F ":" 'NR==8{print $2}'
county=`$JsonDate | sed 's/,//n/g' | $TransCoding | tr -d "{}/"" | awk -F ":" 'NR==10{print $2}'
isp=`$JsonDate | sed 's/,//n/g' | $TransCoding | tr -d "{}/"" | awk -F ":" 'NR==12{print $2}'
printf "%-18s/t%-8s/t%-8s/t%-8s/t%-8s/t%-8s/t%-8s/n" $i $country $isp $area $region $city $county
done
}
printf "%-18s/t%-8s/t%-8s/t%-8s/t%-8s/t%-8s/t%-8s/n" IP地址 國家 運營商 區域 省份 城市 縣/區
echo -e "/e[1;33m======================================================================/e[0m"
ipInfo;
Python腳本內容:
import re
import sys
def main():
for line in sys.stdin:
sys.stdout.write(re.sub(r'//u/w{4}',
lambda e: unichr(int(e.group(0)[2:], 16)).encode('utf-8'),
line))
if __name__ == '__main__':
main()
將兩個腳本放在一個目錄下,再將需要分析的IP地址一行一個寫入在list文件中,執行 shell腳本即可。
實例演示(分析最近暴力破解服務器密碼的IP歸屬地):
新聞熱點
疑難解答