-2

I have this function:

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'])

But the result is :

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
  • Thank you for clarifying your question - can I suggest you edit the title as well? Something like "How do I specify a CIDR IP range to nmap in python?" would make it clearer and more useful for future readers. Mar 11, 2015 at 11:53

1 Answer 1

1

Edit! IPs only go up to 255!

There are 256 elements per subnet but it's zero-indexed. You want: 0-255


Using standard subnet masks works:

nm.scan(hosts='10.10.0.0/24', arguments='-sn')
1
  • Hmm, saying that hyphenated ranges work for me too.
    – Oli
    Mar 11, 2015 at 11:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .