腳本首先是從http://ip.taobao.com/的數據接口獲取IP地址的JSON格式的數據信息,在使用一個python腳本來把Unicode字符轉換成UTF-8編碼。
shell腳本內容:
#!/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;
Python腳本內容:
#!/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()
將兩個腳本放在一個目錄下,再將需要分析的IP地址一行一個寫入在list
文件中,執行 shell腳本即可。
實例演示(分析最近暴力破解服務器密碼的IP歸屬地):
cat /var/log/secure | awk '/Failed/ {print $(NF-3)}' | sort -u > list
[root@MyVPS4407 ip]# ./ip.sh IP地址 國家 運營商 區域 省份 城市 縣/區 ====================================================================== 114.112.69.50 中國 華南 廣東省 118.244.14.49 中國 華北 北京市 北京市 122.72.120.109 中國 鐵通 西北 甘肅省 1.25.202.50 中國 聯通 華北 內蒙古自治區 包頭市 133.242.17.113 日本 134.255.243.11 德國 145.253.72.3 德國 188.116.55.211 波蘭 202.103.36.43 中國 電信 華中 湖北省 武漢市 202.97.194.167 中國 聯通 東北 黑龍江省 哈爾濱市 203.122.59.88 印度 210.44.159.49 中國 教育網 華東 山東省 濟南市 211.232.30.253 韓國 218.248.42.131 印度 223.5.3.200 中國 阿里巴巴 華東 浙江省 杭州市 37.55.227.103 烏克蘭 38.69.193.39 美國 50.97.246.147 美國 66.161.209.154 美國 66.248.201.2 加拿大