Your function does not return a value so it returns None as all python functions that don't specify a return value do:
def whois(k, i):
k = str(k[i])
print (k)
whois = subprocess.Popen(['whois', k], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ou, err = whois.communicate()
who = str(ou)
print (who.find('NetName:'))
who = re.split('NetName:', who)[1]
who = re.split('NetHandle', who)[0]
who = who.replace(r'
', '')
return who # return who
You can also use check_output
if you just want the output, you also don't need re to split:
from subprocess import check_output
def whois(k, i):
k = str(k[i])
who = check_output(['whois', k], universal_newlines=True)
who = who.split('NetName:')[1]
who = who.split('NetHandle')[0]
return who.replace(r'
', '')
There are also multiple python libraries that will do what you want a couple of which are ipwhois, python-whois
If you just want the NetHandle
you can use re to find that:
def whois(k, i):
k = k[i]
who = check_output(['whois', k], universal_newlines=True)
return dict(map(str.strip,ele.split(":",1)) for ele in re.findall(r'^w+:s+.*', who, re.M))
Demo:
In [28]: whois([1,1,1,1, "216.58.208.228"],4)
Out[28]: 'NET-216-58-192-0-1'
Or create a dict and get all the info in key/value pairings:
def whois(k, i):
k = k[i]
who = check_output(['whois', k], universal_newlines=True)
print(who)
return dict(map(str.strip,ele.split(":",1)) for ele in re.findall('w+:s+.*', who))
d = whois([1,1,1,1, "216.58.208.228"],4)
print(d["NetHandle"])
from pprint import pprint as pp
pp(d)
Output:
NET-216-58-192-0-1
{'Address': '1600 Amphitheatre Parkway',
'CIDR': '216.58.192.0/19',
'City': 'Mountain View',
'Country': 'US',
'NetHandle': 'NET-216-58-192-0-1',
'NetName': 'GOOGLE',
'NetRange': '216.58.192.0 - 216.58.223.255',
'NetType': 'Direct Allocation',
'OrgAbuseEmail': '[email protected]',
'OrgAbuseHandle': 'ZG39-ARIN',
'OrgAbuseName': 'Google Inc',
'OrgAbusePhone': '+1-650-253-0000',
'OrgAbuseRef': 'http://whois.arin.net/rest/poc/ZG39-ARIN',
'OrgId': 'GOGL',
'OrgName': 'Google Inc.',
'OrgTechEmail': '[email protected]',
'OrgTechHandle': 'ZG39-ARIN',
'OrgTechName': 'Google Inc',
'OrgTechPhone': '+1-650-253-0000',
'OrgTechRef': 'http://whois.arin.net/rest/poc/ZG39-ARIN',
'Organization': 'Google Inc. (GOGL)',
'OriginAS': 'AS15169',
'Parent': 'NET216 (NET-216-0-0-0-0)',
'PostalCode': '94043',
'Ref': 'http://whois.arin.net/rest/org/GOGL',
'RegDate': '2000-03-30',
'StateProv': 'CA',
'Updated': '2013-08-07'}