腳本首先是從http://ip.taobao.com/的數據接口獲取IP地址的JSON格式的數據信息,在使用一個python腳本來把Unicode字符轉換成UTF-8編碼。
#!/bin/bash
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;
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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()