-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnslookup.py
53 lines (43 loc) · 1.3 KB
/
nslookup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
import sys
import argparse
import ipaddress
import subprocess
def nslookup(q,ip,ipfile):
resolved_ip = None
process = subprocess.Popen(["nslookup", q], stdout=subprocess.PIPE)
output, error = process.communicate()
output = output.decode().split('\n')
if ip == True:
for data in output:
if 'name' in data:
hostname = data.split("name = ")[-1].rstrip(".")
print(hostname)
return
else:
resolved_ip = output[-3].split("Address: ")[-1]
if ipfile:
if resolved_ip in ipfile:
print(q)
else:
print(resolved_ip)
def main(args):
query = args.query
if args.ipfile:
with open(args.ipfile,'r') as readhandle:
ipfile = readhandle.read()
else:
ipfile = None
for q in query:
ip = True
try:
ipaddress.IPv4Address(q)
except:
ip = False
nslookup(q,ip,ipfile)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="nslookup wrapper")
parser.add_argument(action="store",dest="query",nargs='+',help="what to lookup")
parser.add_argument("-f",action="store",dest="ipfile",help="file to reference")
args = parser.parse_args()
main(args)