1)问题描述
在windows下会抛出以下异常。
Traceback (most recent call last): File "ftplogin_file.py", line 111, in <module> main() File "ftplogin_file.py", line 109, in main ftpLogin(filepath) File "ftplogin_file.py", line 77, in ftpLogin if nmapScan(ip,"21") == True: File "ftplogin_file.py", line 8, in nmapScan nmScan = nmap.PortScanner() File "C:\Python27\lib\site-packages\nmap\nmap.py", line 144, in __init__ raise PortScannerError(‘nmap program was not found in path. PATH is : {0}‘.f ormat(os.getenv(‘PATH‘))) nmap.nmap.PortScannerError: ‘nmap program was not found in path. PATH is : C:\\W INDOWS\\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‘
抛出异常的地方
nmScan = nmap.PortScanner()
2)解决方案
抛出异常的原因是因为没有指定nmap的执行路径所致,在windows下需要手动指定nmap的执行路径。
修改后
nmScan = nmap.PortScanner(nmap_search_path=(‘nmap‘,r"H:\Nmap\nmap.exe"))
3)修改后的完整代码
import nmap import optparse def nmapScan(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 + ‘ ‘ + state def main(): 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): print parser.usage exit(0) for tgtPort in tgtPorts: nmapScan(tgtHost,tgtPort) if __name__ == ‘__main__‘: main()
4)再次运行,运行结果如下
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
时间: 2024-11-05 06:28:01