windows下python-nmap运行过程中出现的问题及解…

2018-06-17 23:54:00来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:用python写简易的端口扫描

下一篇:python string类型 bytes类型 bytearray类型