查询netstat中的源ip所在地

来源:互联网 发布:手机视频剪辑合并软件 编辑:程序博客网 时间:2024/06/05 00:33
#!/usr/sbin/env pythonimport urllib2,jsonimport os,threadingdef 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

原创粉丝点击