Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- corrected missing incomplete parameter on parse_fromfile and parse_fromstring - added support to run scan in background with sudo support added NmapProcess.sudo_run_background() - fixed issue with run() blocking when an error triggered during the scan
- Loading branch information
1 parent
12e9ad3
commit 3c639ea
Showing
10 changed files
with
224 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env python | ||
|
||
from libnmap.process import NmapProcess | ||
from time import sleep | ||
|
||
def make_nmproc_obj(targets, options): | ||
return NmapProcess(targets=targets, options=options) | ||
|
||
def start_all(nmprocs): | ||
for nmp in nmprocs: | ||
print("Starting scan for host {0}".format(nmp.targets)) | ||
nmp.run() | ||
|
||
def summarize(nmprocs): | ||
for nmp in nmprocs: | ||
print "rc: {0} output: {1}".format(nmp.rc, nmp.summary) | ||
|
||
nm_targets = ["localhost", "localhost", "localhost"] | ||
nm_opts = "-sT" | ||
|
||
nm_procs = [make_nmproc_obj(t, nm_opts) for t in nm_targets] | ||
start_all(nm_procs) | ||
|
||
summarize(nm_procs) |
29 changes: 29 additions & 0 deletions
29
libnmap/test/process-stressbox/multi_nmap_process_background.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python | ||
|
||
from libnmap.process import NmapProcess | ||
from time import sleep | ||
|
||
def make_nmproc_obj(targets, options): | ||
return NmapProcess(targets=targets, options=options) | ||
|
||
def start_all_bg(nmprocs): | ||
for nmp in nmprocs: nmp.run_background() | ||
|
||
def any_running(nmprocs): | ||
return any([nmp.is_alive() for nmp in nmprocs]) | ||
|
||
def summarize(nmprocs): | ||
for nmp in nmprocs: | ||
print "rc: {0} output: {1}".format(nmp.rc, nmp.summary) | ||
|
||
nm_targets = ["localhost", "localhost", "localhost"] | ||
nm_opts = "-sT" | ||
|
||
nm_procs = [make_nmproc_obj(t, nm_opts) for t in nm_targets] | ||
start_all_bg(nm_procs) | ||
|
||
while any_running(nm_procs): | ||
print "Nmap Scan running..." | ||
sleep(2) | ||
|
||
summarize(nm_procs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python | ||
|
||
from libnmap.process import NmapProcess | ||
from time import sleep | ||
|
||
|
||
nmap_proc = NmapProcess(targets="scanme.nmap.org", options="-sT") | ||
nmap_proc.run_background() | ||
while nmap_proc.is_alive(): | ||
print "Nmap Scan running: ETC: {0} DONE: {1}%".format(nmap_proc.etc, | ||
nmap_proc.progress) | ||
sleep(2) | ||
|
||
print "rc: {0} output: {1}".format(nmap_proc.rc, nmap_proc.summary) | ||
print nmap_proc.stdout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/env python | ||
from libnmap.process import NmapProcess | ||
from libnmap.parser import NmapParser, NmapParserException | ||
|
||
|
||
# start a new nmap scan on localhost with some specific options | ||
def do_scan(targets, options): | ||
nm = NmapProcess(targets, options) | ||
rc = nm.run() | ||
if rc != 0: | ||
print "nmap scan failed: %s" % (nm.stderr) | ||
|
||
try: | ||
parsed = NmapParser.parse(nm.stdout) | ||
except NmapParserException as e: | ||
print "Exception raised while parsing scan: %s" % (e.msg) | ||
|
||
return parsed | ||
|
||
|
||
# print scan results from a nmap report | ||
def print_scan(nmap_report): | ||
print "Starting Nmap {0} ( http://nmap.org ) at {1}".format( | ||
nmap_report._nmaprun['version'], | ||
nmap_report._nmaprun['startstr']) | ||
|
||
for host in nmap_report.hosts: | ||
if len(host.hostnames): | ||
tmp_host = host.hostnames.pop() | ||
else: | ||
tmp_host = host.address | ||
|
||
print "Nmap scan report for {0} ({1})".format( | ||
tmp_host, | ||
host.address) | ||
print "Host is {0}.".format(host.status) | ||
print " PORT STATE SERVICE" | ||
|
||
for serv in host.services: | ||
pserv = "{0:>5s}/{1:3s} {2:12s} {3}".format( | ||
str(serv.port), | ||
serv.protocol, | ||
serv.state, | ||
serv.service) | ||
if len(serv.banner): | ||
pserv += " ({0})".format(serv.banner) | ||
print pserv | ||
print nmap_report.summary | ||
|
||
|
||
if __name__ == "__main__": | ||
report = do_scan("127.0.0.1", "-sV") | ||
print_scan(report) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env python | ||
|
||
from libnmap.process import NmapProcess | ||
from time import sleep | ||
|
||
def make_nmproc_obj(targets, options): | ||
return NmapProcess(targets=targets, options=options) | ||
|
||
def start_all_bg(nmprocs): | ||
for nmp in nmprocs: nmp.run_background() | ||
|
||
def any_running(nmprocs): | ||
return any([nmp.is_alive() for nmp in nmprocs]) | ||
|
||
def summarize(nmprocs): | ||
for nmp in nmprocs: | ||
print "rc: {0} output: {1}".format(nmp.rc, len(nmp.stdout)) | ||
|
||
nb_targets = 1000 | ||
nm_target = "localhost" | ||
nm_opts = "-sP" | ||
|
||
nm_targets = [nm_target for i in range(nb_targets)] | ||
nm_procs = [make_nmproc_obj(t, nm_opts) for t in nm_targets] | ||
start_all_bg(nm_procs) | ||
|
||
while any_running(nm_procs): | ||
sleep(5) | ||
|
||
summarize(nm_procs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python | ||
from libnmap.process import NmapProcess | ||
from libnmap.parser import NmapParser, NmapParserException | ||
|
||
nm = NmapProcess('127.0.0.1', '-sP') | ||
rc = nm.run() | ||
if rc != 0: | ||
print "nmap scan failed: %s" % (nm.stderr) | ||
|
||
try: | ||
report = NmapParser.parse(nm.stdout) | ||
except NmapParserException as e: | ||
print "Exception raised while parsing scan: %s" % (e.msg) | ||
|
||
print len(nm.stdout) |