-
Notifications
You must be signed in to change notification settings - Fork 1
/
netstat-whois.py
53 lines (41 loc) · 1.37 KB
/
netstat-whois.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
import subprocess
import tempfile
from ipwhois import IPWhois
import json
# netstat_output = sp.run(['netstat', '-a'], stdout=sp.PIPE).stdout.decode('utf-8').split("\n")
with tempfile.TemporaryFile() as tempf:
proc = subprocess.Popen(['netstat', '-a'], stdout=tempf)
proc.wait()
tempf.seek(0)
out = tempf.read().decode('utf-8').split('\n')
# with open('tmp.txt', 'r') as f:
# out = f.read().split('\n')
cells = []
for line in out:
res = ""
for c in line:
if not res.endswith(c):
res += c
cells.append(res.split(' '))
#ips = [r[4] for r in cells if r[4] and ':' in r[4]]
ips = []
for r in cells:
if len(r) < 4:
break
if ':' in r[4]:
ips.append(r[4])
ips = set(ips)
with open("result.txt", "w") as of:
for ip in ips:
ip = ip.split(':')[0]
of.write("%s IP: %s" %("\n" * 3 + "#"*5, ip))
try:
res = IPWhois(ip)
res = res.lookup_whois()
city = res['nets'][0]['city']
corp = res['nets'][0]['name']
email = res['nets'][0]['emails'][0]
of.write("\n\tCorp:\t%s\n\tCity:\t%s\n\tEmail:\t%s" %(corp, city, email))
except:
of.write("\n--Not a valid IP")
pass