-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaged_nmap.py
41 lines (29 loc) · 1.08 KB
/
staged_nmap.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
#!/bin/python3
import os
import argparse
import subprocess
import xml.etree.ElementTree as ET
# sort out command line args
parser = argparse.ArgumentParser()
parser.add_argument("host", help="The IP address or hostname of the scan target")
args = parser.parse_args()
host = args.host
wd = os.getcwd()
filename = wd + "/stagednmap.txt"
# run initial nmap scan and get the output
print("Running initial scan on {}".format(host))
initialscan = subprocess.run(["nmap","-T4","-p-","-oX","-",host], stdout=subprocess.PIPE)
initialresult = initialscan.stdout.decode("utf-8")
# parse the output and grab open ports
root = ET.fromstring(initialresult)
openports = list()
for port in root.findall(".//port"):
openports.append(port.attrib["portid"])
portparam = "-p{}".format(",".join(openports))
# Run deep scan
print("Running final scan on {}".format(host))
finalscan = subprocess.run(["nmap","-A",portparam,"-oN","-",host], stdout=subprocess.PIPE)
result = finalscan.stdout.decode("utf-8")
with open(filename, "w") as textout:
textout.write(result)
print("Output saved to {}".format(filename))