脚本首先是从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()