1 #Coding=utf-82 3 ImportNmap4 ImportOptparse5 ImportThreading6 ImportSYS7 ImportRe8 " "9 need to install PYTHON_NMAP package, support 2.x and 3.xTen The Python_nmap package provides a series of interfaces that Python calls Nmap One A (i) Important categories and methods: - 1. Create an NMAP scanner - class Portscanner () the __init__ (Self, nmap_search_path= (' Nmap ', '/usr/bin/nmap ', '/usr/local/bin/nmap ', '/sw/bin/nmap ', '/opt/local/ Bin/nmap ')) - Initialize Portscanner Module - - * Detects nmap on the system and NMAP version + * May raise Portscannererror exception if Nmap isn't found in the path - + :p Aram Nmap_search_path:tupple of string where to search for Nmap executable. Change the If you want to use a specific version of Nmap. A : returns:nothing at 2. Scanner Method - Scan (self, hosts= ' 127.0.0.1 ', Ports=none, arguments= '-sv ', Sudo=false) - Scan Given hosts - - May raise Portscannererror exception if Nmap output is not XML - in Test existance of the following key to know if something went wrong: [' nmap '] [' scaninfo '] [' ERROR '] - If not present, everything is OK. to + :p Aram Hosts:string for hosts as nmap use it ' scanme.nmap.org ' or ' 198.116.0-255.1-127 ' or ' 216.163.128.20/20 ' - :p Aram Ports:string for ports as nmap use it ' 22,53,110,143-4564 ' the :p Aram Arguments:string of arguments for Nmap '-SU-SX-SC ' * :p Aram Sudo:launch nmap with sudo if True $ Panax Notoginseng : Returns:scan_result as Dictionnary - the (ii) examples + Import Nmap A scanner = Nmap. Portscanner () #nmap_search_path已包含了nmap所在路径, if there is no nmap in the default path, you need to indicate the results = Scanner.scan (hosts= ' 192.168.2.1 ', ports= ') + Pprint.pprint (Results) - {' Nmap ': {' command_line ': ' Nmap-ox-P 80-sv 192.168.2.1 ', $ ' Scaninfo ': {' tcp ': {' method ': ' syn ', ' Services ': ' + '}, $ ' scanstats ': {' downhosts ': ' 0 ', - ' elapsed ': ' 11.59 ', - ' timestr ': ' Thu Jul 10:08:34 ', the ' totalhosts ': ' 1 ', - ' uphosts ': ' 1 '},Wuyi ' Scan ': {' 192.168.2.1 ': {' addresses ': {' IPv4 ': ' 192.168.2.1 ', the ' mac ': ' D0:c7:c0:6a:f6:a0 '}, - ' hostnames ': [], Wu ' status ': {' reason ': ' Arp-response ', - ' state ': ' Up '}, About ' TCP ': {: {' conf ': ' 3 ', $ ' CPE ': ', - ' extrainfo ': ', - ' name ': ' http ', - ' product ': ', A ' reason ': ' No-response ', + ' State ': ' Filtered ', the ' version ': '}, - ' Vendor ': {' d0:c7:c0:6a:f6:a0 ': ' Tp-link ' $ ' Technologies ' }}} the the " " the defAnlyze_port (target_port): the #resolves the value passed in to the-p parameter and returns a list of ports - Try: inPattern = Re.compile (r'(\d+)-(\d+)')#Parse connector-pattern theMatch =Pattern.match (Target_port) the ifmatch: Aboutstart_port = Int (Match.group (1)) theend_port = Int (Match.group (2)) the return([x forXinchRange (Start_port,end_port + 1)]) the Else: + return([Int (x) forXinchTarget_port.split (',')]) - exceptException as err: the Print('Please note that error 1:', Sys.exc_info () [0],err]Bayi Print(parser.usage) the exit (0) the - defPortscanner (target_host,target_port): -Scanner =Nmap. Portscanner () theResults = Scanner.scan (hosts=target_host,ports=target_port,arguments='-t4-a-V-PN')#fast scan for forbidden pings the Print('The scan statements are:', results['Nmap']['Command_line']) the Print('[*] Host'+ Target_host +'of the'+ STR (target_port) +'The port status is:'+ results['Scan'][target_host]['TCP'][int (Target_port) [' State']) the - defMain (): theUsage ='Usage:%prog--host <target_host>--port <target_port>' theParser = Optparse. Optionparser (usage,version='v1.0') theParser.add_option ('--host', dest='Target_host', type='string',94help='host, domain, or IP that needs to be scanned') theParser.add_option ('--port', dest='Target_port', type='string', thehelp='host ports that need to be scanned, support 1-100 or 21,53,80 two forms') the(Options,args) =Parser.parse_args ()98 ifOptions.target_host = = NoneorOptions.target_port = =None: About Print(parser.usage) - exit (0)101 Else:102Target_host =Options.target_host103Target_port =Options.target_port104 theTarget_port =Anlyze_port (Target_port)106 forPortinchTarget_port:107t = Threading. Thread (target=portscanner,args=(TARGET_HOST,STR (port)))108 T.start ()109 the if __name__=='__main__':111Main ()
The result after the run is:
1 c:\python34\python.exe nmapscanner.py--host 192.168.1.1--port2 scan statement is: Nmap-ox-P 80-t4-a-V -PN 192.168.1.13 [*] Host 192.168.1.1 80 port status: filtered
Python calls Nmap for scanning