查询netstat中的源ip所在地

xiaoxiao2021-02-27  216

#!/usr/sbin/env python import urllib2,json import os,threading def action(ip): try: a=urllib2.urlopen("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ip).read() a=a.split("=")[1].strip().split(";")[0] a=json.loads(a) if a.has_key("province"): if not a['province'].encode('utf-8')=='\xe5\xb9\xbf\xe4\xb8\x9c\xe7\x9c\x81': print ip+":"+a['country'].encode('utf-8')+a['province'].encode('utf-8') else: if not a['country'].encode('utf-8')=='\xe4\xb8\xad\xe5\x9b\xbd': print ip+":"+a['country'].encode('utf-8') except Exception,e: print ip+ ":" + str(e) net_res = os.popen("netstat -anlpt |sed '1,2d' | awk '{print $5}' | grep -v '^:' | cut -d ':' -f 1 | sort | uniq | grep -v '0.0.0.0' | grep -v '127.0.0.1'").read().strip() net_res = net_res.split('\n') iplength = len(net_res) for i in xrange(iplength): t=threading.Thread(target=action,args=(net_res[i],)) t.start()

脚本是以查找非广东省ip所在地为例,调用的是新浪的查询api

转载请注明原文地址: https://www.6miu.com/read-10912.html

最新回复(0)