py-ipchecker/main.py

77 lines
1.4 KiB
Python

import os
import threading
from ipwhois.net import Net
from ipwhois.asn import IPASN
from pprint import pprint
tmpips = [] #NOTE Need to reset this variable between runs
ips = []
ipdict = {}
currValue = ""
class threadedLookup(threading.Thread):
def run(self):
global currValue
lookup(currValue)
def openFile(filename):
try:
ipList = open(filename)
except Exception as e:
print(e)
print("Please create an iplist.txt file")
return ipList
def splitLine(fileline):
tmpline = fileline.split('\n')
retval = []
return tmpline
def lookup(ip):
global ipdict
try:
net = Net(ip)
obj = IPASN(net)
results = obj.lookup()
#pprint(results)
for key,value in results.iteritems():
if key == "asn_country_code":
#print(value)
ipdict[ip] = value
except Exception as e:
print(e)
ipListFile = openFile("iplist.txt")
ipList = ipListFile.read()
test = splitLine(ipList)
#print test
for a in test:
b = a.split(' ')
tmpips.append(b)
for x in tmpips:
for y in x:
if y != '':
ips.append(y)
for value in ips:
if "." in value:
#lookup(value)
currValue = value
print currValue
lookupThread = threadedLookup(name = "lookup" + value)
lookupThread.start()
#pprint(ipdict)
for key, value in sorted(ipdict.iteritems(), key=lambda (k,v): (v,k)):
if "AU" not in value:
if "NZ" not in value:
print key + '\t' + '\t' + value
# if x != "''":
# print(x)
#print(test)
#print ipList
#for a in ipList:
# print(a)