Python使用淘寶API查詢IP歸屬地功能分享
網上有很多方法能夠過去到IP地址歸屬地的腳本,但是我發(fā)現(xiàn)淘寶IP地址庫的信息更詳細些,所以用shell寫個腳本來處理日常工作中一些IP地址分析工作。
腳本首先是從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地址 國家 運營商 區(qū)域 省份 城市 縣/區(qū)
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文件中,執(zhí)行 shell腳本即可。
實例演示(分析最近暴力破解服務器密碼的IP歸屬地):
cat /var/log/secure | awk '/Failed/ {print $(NF-3)}' | sort -u > list
[root@MyVPS4407 ip]# ./ip.sh
IP地址 國家 運營商 區(qū)域 省份 城市 縣/區(qū)
======================================================================
114.112.69.50 中國 華南 廣東省
118.244.14.49 中國 華北 北京市 北京市
122.72.120.109 中國 鐵通 西北 甘肅省
1.25.202.50 中國 聯(lián)通 華北 內蒙古自治區(qū) 包頭市
133.242.17.113 日本
134.255.243.11 德國
145.253.72.3 德國
188.116.55.211 波蘭
202.103.36.43 中國 電信 華中 湖北省 武漢市
202.97.194.167 中國 聯(lián)通 東北 黑龍江省 哈爾濱市
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 加拿大
相關文章
linux shell在while中用read從鍵盤輸入的實現(xiàn)
下面小編就為大家?guī)硪黄猯inux shell在while中用read從鍵盤輸入的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01Linux下/var/run/目錄下的pid文件詳解及pid文件作用
linux系統(tǒng)中/var/run/目錄下的*.pid文件是一個文本文件,其內容只有一行,即某個進程的PID。這篇文章主要介紹了Linux下/var/run/目錄下的pid文件詳解及pid文件作用,需要的朋友可以參考下2018-04-04