1) Description of the problem
The following exception is thrown under Windows.
Traceback (most recent): File"ftplogin_file.py", line 111,inch<module>Main () File"ftplogin_file.py", line 109,inchmain FTPLogin (filepath) File"ftplogin_file.py", Line 77,inchFTPLoginifNmapscan (IP," +") ==True:file"ftplogin_file.py", line 8,inchNmapscan Nmscan=Nmap. Portscanner () File"C:\Python27\lib\site-packages\nmap\nmap.py", line 144,inch __init__ RaisePortscannererror ('Nmap program is not a found in path. PATH is: {0}'. F ormat (Os.getenv ('PATH')) Nmap.nmap.PortScannerError:'Nmap program is not a found in path. PATH is:c:\\wIndows\\system32; C:\\Windows; C:\\windows\\system32\\wbem; C:\\python27;d:\\wps of fice\\9.1.0.4468\\office6; C:\\Program Files\\Microsoft Visual Studio\\common\\to Ols\\winnt; C:\\Program Files\\Microsoft Visual Studio\\common\\msdev98\\bin; C:\\ Program Files\\Microsoft Visual Studio\\common\\tools; C:\\Program Files\\microso ft Visual Studio\\vc98\\bin'
Where the exception is thrown
Nmscan = Nmap. Portscanner ()
2) Solutions
The exception is thrown because the execution path of NMAP is not specified, and the execution path of NMAP needs to be specified manually under Windows.
After modification
Nmscan = Nmap. Portscanner (nmap_search_path= (' Nmap ', r "H:\Nmap\nmap.exe"))
3) The complete code after the modification
ImportNmapImportOptparsedefNmapscan (tgthost,tgtport): Nmscan= Nmap. Portscanner (Nmap_search_path= ('Nmap'R"H:\Nmap\nmap.exe")) Nmscan.scan (tgthost,tgtport) state= nmscan[tgthost]['TCP'][int (Tgtport) [' State'] Print '[*]'+ Tgthost +'tcp/'+ Tgtport +' '+ StatedefMain (): Parser= Optparse. Optionparser ('Usage%prog'+'- H <target host>-p <target port>') parser.add_option ('- H', dest='Tgthost', type='string', help='Specify target host') parser.add_option ('- P', dest='Tgtport', type='string', help='specify target Port[s] separated by comma') (Options,args)=Parser.parse_args () tgthost=options.tgthost tgtports= str (options.tgtport). Split (',') if(Tgthost = = None) | (Tgtports[0] = =None):PrintParser.usage exit (0) forTgtportinchTgtports:nmapscan (Tgthost,tgtport)if __name__=='__main__': Main ()
4) run again and the results are as follows
D:\python-nmap>python2 test.py-h 192.168.1.44-p 21,22,1720[*]192.168.1.44 tcp/21 closed[*] 192.168.1.44 tcp/22 closed[*]192.168.1.44 tcp/1720 closed
Problems and solutions in the operation of Windows Python-nmap