-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipscan
79 lines (57 loc) · 2.13 KB
/
ipscan
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python3
try:
import libnmap.process
import libnmap.parser
import tableprint
import numpy
import ipaddress
from typing import List
except ModuleNotFoundError:
print("Required modules not found. This program requires Python 3 and required dependicies installed. Run 'pip install python-libnmap tableprint numpy' to install dependicies and try again.")
exit(1)
HOSTS_TO_SCAN = '192.168.1.0/24'
DNS_SERVER = '192.168.1.1'
def hasHostname(hostnames: List[str]) -> bool:
return len(hostnames) != 0
def isValidIPAddress(IP_ADDR: str) -> bool:
try:
ipaddress.ip_address(IP_ADDR)
except ValueError:
return False
return True
def removeDuplicatesFromList(array: List):
b = list()
[b.append(sublist) for sublist in array if sublist not in b]
return b
def runNmapScan(nmapOptions: str, dnsServer: str):
if DNS_SERVER is not None:
if(isValidIPAddress(dnsServer) is True):
nmapOptions += ' --dns-servers '
nmapOptions += dnsServer
else:
raise TypeError("Invalid DNS server")
nm = libnmap.process.NmapProcess(HOSTS_TO_SCAN, options=nmapOptions)
rc = nm.run()
nmap_report = libnmap.parser.NmapParser.parse(nm.stdout)
return nmap_report
def Convert2DArrayToNumpyArray(arr: List):
return numpy.array(arr)
def print2DArrayAsTable(
arr: List[list],
alignment: str,
tableHeaders: List[str]):
tableprint.table(Convert2DArrayToNumpyArray(arr),
tableHeaders, align=alignment)
hosts = []
print("Running standard ping scan...")
nmap_report = runNmapScan("-sn", DNS_SERVER)
for host in nmap_report.hosts:
if hasHostname(host.hostnames):
hosts.append([host.address, ' '.join(host.hostnames)])
print("Running deeper ping scan to find hidden devices...")
nmap_report = runNmapScan("-sn -Pn", DNS_SERVER)
for host in nmap_report.hosts:
if hasHostname(host.hostnames):
hosts.append([host.address, ' '.join(host.hostnames)])
hostsWithoutDuplicates = removeDuplicatesFromList(hosts)
print2DArrayAsTable(hostsWithoutDuplicates, 'left', ['IP Address', 'Hostname'])