-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathNmapScanner.py
68 lines (51 loc) · 2.27 KB
/
NmapScanner.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import optparse, nmap
import json
class NmapScanner:
def __init__(self):
self.nmsc = nmap.PortScanner()
def nmapScan(self, host, port):
try:
print "Checking port "+ port +" .........."
self.nmsc.scan(host, port)
# Command info
print "[*] Execuing command: %s" % self.nmsc.command_line()
self.state = self.nmsc[host]['tcp'][int(port)]['state']
print " [+] "+ host + " tcp/" + port + " " + self.state
print self.nmsc[host].tcp(int(port))
self.server = self.nmsc[host].tcp(int(port))['product']
self.version = self.nmsc[host].tcp(int(port))['version']
print " [+] "+ self.server + " " + self.version + " tcp/" + port
except Exception,e:
print "Error to connect with " + host + " for port scanning"
pass
def nmapScanJSONGenerate(self, host, ports):
try:
print "Checking ports "+ str(ports) +" .........."
self.nmsc.scan(host, ports)
# Command info
print "[*] Execuing command: %s" % self.nmsc.command_line()
print self.nmsc.csv()
results = {}
for x in self.nmsc.csv().split("\n")[1:-1]:
splited_line = x.split(";")
host = splited_line[0]
proto = splited_line[1]
port = splited_line[2]
state = splited_line[4]
try:
if state == "open":
results[host].append({proto: port})
except KeyError:
results[host] = []
results[host].append({proto: port})
# Store info
file_info = "scan_%s.json" % host
with open(file_info, "w") as file_json:
json.dump(results, file_json)
print "[*] File '%s' was generated with scan results" % file_info
except Exception,e:
print e
print "Error to connect with " + host + " for port scanning"
pass