@@ -21,6 +21,73 @@ libnmap is a python toolkit for manipulating nmap. It currently offers the follo
2121 - elastic search: todo
2222 - csv: todo
2323
24- How
25- ===
26- Will be added very soon but I have to go get some sushis first :)
24+ ## How
25+
26+ ### Launch a nmap scan
27+ Here a consequent example on how to use libnmap:
28+ ``` python
29+ # !/usr/bin/env python
30+ from libnmap import NmapProcess, NmapParser, NmapParserException
31+
32+
33+ # start a new nmap scan on localhost with some specific options
34+ def do_scan (targets , options ):
35+ nm = NmapProcess(targets, options)
36+ rc = nm.run()
37+ if rc != 0 :
38+ print " nmap scan failed: %s " % (nm.stderr)
39+
40+ try :
41+ parsed = NmapParser.parse(nm.stdout)
42+ except NmapParserException as e:
43+ print " Exception raised while parsing scan: %s " % (e.msg)
44+
45+ return parsed
46+
47+
48+ # print scan results from a nmap report
49+ def print_scan (nmap_report ):
50+ print " Starting Nmap {0} ( http://nmap.org ) at {1} " .format(
51+ nmap_report._nmaprun[' version' ],
52+ nmap_report._nmaprun[' startstr' ])
53+
54+ for host in nmap_report.scanned_hosts:
55+ print " Nmap scan report for {0} ({1} )" .format(
56+ host.hostname,
57+ host.address)
58+ print " Host is {0} ." .format(host.status)
59+ print " PORT STATE SERVICE"
60+
61+ for serv in host.services:
62+ pserv = " {0:>5s } /{1:3s } {2:12s } {3} " .format(
63+ str (serv.port),
64+ serv.protocol,
65+ serv.state,
66+ serv.service)
67+ if len (serv.banner):
68+ pserv += " ({0} )" .format(serv.banner)
69+ print pserv
70+ print nmap_report.summary
71+
72+
73+ if __name__ == " __main__" :
74+ report = do_scan(" 127.0.0.1" , " -sV" )
75+ print_scan(report)
76+ ```
77+
78+ ### De/Serialize NmapReport
79+ Easy:
80+ ``` python
81+ from libnmap import NmapParser, NmapReport
82+ from libnmap import ReportDecoder, ReportEncoder
83+ import json
84+
85+ r = NmapParser.parse_fromfile(' /root/dev/python-nmap-lib/libnmap/test/files/1_hosts.xml' )
86+
87+ # create a json object from an NmapReport instance
88+ j = json.dumps(r, cls = ReportEncoder)
89+
90+ # create a NmapReport instance from a json object
91+ nmapreport = json.loads(j, cls = ReportDecoder)
92+ nmapreport.name
93+ ```
0 commit comments