如何在 python 中向 nmap 指定 CIDR IP 范围?

如何在 python 中向 nmap 指定 CIDR IP 范围?

我有这个功能:

if subnet==16:
       nm = nmap.PortScanner() 
       a=nm.scan(hosts='172.16.2.0-256', arguments='-sn') 
       for k,v in a['scan'].iteritems(): 
               if str(v['status']['state']) == 'up':
                         print str(v)
                 try:    print str(v['addresses']['ipv4']) + ' => ' + str(v['addresses']['mac'])
                 except: print str(v['addresses']['ipv4'])

但结果是:

Traceback (most recent call last):
  File "APINMAP.py", line 101, in <module>
    main()
  File "APINMAP.py", line 19, in main
    a=nm.scan(hosts='172.16.2.0-256', arguments='-sn') 
  File "/usr/local/lib/python2.7/dist-packages/nmap/nmap.py", line 302, in scan
    nmap_err_keep_trace = nmap_err_keep_trace)
  File "/usr/local/lib/python2.7/dist-packages/nmap/nmap.py", line 360, in 
analyse_nmap_xml_scan
    raise PortScannerError(nmap_err)    
nmap.nmap.PortScannerError: u'Your host specifications are illegal!\nQUITTING!\n'

答案1

编辑!IP 最多只能达到 255

每个子网有 256 个元素,但其索引为零。您需要:0-255


使用标准子网掩码作品:

nm.scan(hosts='10.10.0.0/24', arguments='-sn')

相关内容