- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmap_python.py
More file actions
Latest commit
43 lines (36 loc) · 1.17 KB
/
Copy pathnmap_python.py
File metadata and controls
43 lines (36 loc) · 1.17 KB
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
importnmap, csv, re
nm=nmap.PortScanner()
cidr_list=[]
cidr_regex='(?:\d{1,3}\.){3}\d{1,3}(?:/\d\d?)?'
host_count=0
up_count=0
defcheck_cidr(cidr):
if(re.search(cidr_regex, cidr)):
return"Valid CIDR"
else:
pass
defget_ip_list():
withopen('ip_ranges.csv', newline='') ascsvfile:
csvreader=csv.reader(csvfile, delimiter=',')
forrowincsvreader:
forcidr_rangeinrow:
ifcheck_cidr(cidr_range) =="Valid CIDR":
cidr_list.append(cidr_range)
else:
pass
defdiscovery_scan(cidr):
globalhost_count, up_count
nm.scan(hosts=cidr, arguments='-PE -PU')
hosts_list= [(x, nm[x]['status']['state']) forxinnm.all_hosts()]
host_count+=len(hosts_list)
forhost, statusinhosts_list:
ifstatus=='up':
up_count+=1
withopen('output.csv', 'w', newline='') asoutput:
output.write(nm.csv())
if__name__=="__main__":
get_ip_list()
forcidrincidr_list:
print(f"Scanning range {cidr}")
discovery_scan(cidr)
print(f"Scanned {host_count} IPs, there are {up_count} up")