0

I'm playing with the nmap parser for python located at (http://xael.org/norman/python/python-nmap/). It provides a sample code snippet that I used in a .py script in order to do routine checks, automating some tasks. However I get a error on "Line 25". Can someone please help me..?

import nmap

nm = nmap.PortScanner()

nm.scan('127.0.0.1', '22-2223')
nm.command_line()
nm.scaninfo()

for host in nm.all_hosts():
    print('----------------------------------------------------')
    print('Host : %s (%s)' % (host, nm[host].
    print('State : %s' % nm[host].state())
    for proto in nm[host].all_protocols():
         print('----------')
         print('Protocol : %s' % proto)
         lport = nm[host][proto].keys()
         lport.sort()
         for port in lport:
             print ('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
             print('----------------------------------------------------')

ERROR Below:

    root@server:~/python/python# python MyApp.py 
    ----------------------------------------------------
    Host : 127.0.0.1 (localhost)
    State : up
    ----------
    Protocol : addresses
    Traceback (most recent call last):
      File "MyApp.py", line 25, in <module>
        print ('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
    TypeError: string indices must be integers
    root@damnation:~/python/python# 

Line 25, is the second last print line from the bottom. 'port : %s\tstate : %s' % (port, nm[host][proto][port]'.

any advice would be great. thank you .

9
  • Port is an integer, so either use str(port) to convert it to a string in line 25 or change your print statement's format to '%d' for integer. Commented Feb 10, 2014 at 7:54
  • Thnx, I tried both your suggestions, but I still get the same problem. Commented Feb 10, 2014 at 8:27
  • if I remove 'state' completely I get this output ---> port : 80 state : {'product': u'lighttpd', 'state': u'open', 'version': u'1.4.28', 'name': u'http', 'conf': u'10', 'extrainfo': '', 'reason': u'syn-ack', 'cpe': ''} Commented Feb 10, 2014 at 10:28
  • 1
    How about print('port : '+port+'\tstate : '+ nm[host][proto][port]['state'])? What's that print out? I'm guessing that your state object is not a string. Commented Feb 10, 2014 at 22:01
  • hey hd1, it returns the following "File "MyApp.py", line 22, in <module> print('port : '+port+'\tstate : '+ nm[host][proto][port]['state']) TypeError: string indices must be integers". The same error. Commented Feb 11, 2014 at 6:28

1 Answer 1

1

I have found that specifying the proto in the lport param enabled the for bundled loop to correctly see the strings within the dict. Below is the correct script that allows for the python-nmap parser to work correctly. Obviously the for bundle only caters for TCP, however another params with for bundle would suffice for the UDP requirement.

    import nmap                         # import nmap.py module

    nm = nmap.PortScanner()
    host = '127.0.0.1'
    nm.scan(host, '1-1024')
    nm.command_line()
    nm.scaninfo()

    for host in nm.all_hosts():
        print('----------------------------------------------------')
        print('Host : %s (%s)' % (host, nm[host].hostname()))
        print('State : %s' % nm[host].state())
        print('----------------------------------------------------')

    for proto in nm[host].all_protocols():
            print('----------')
            print('Protocol : %s' % proto)

    lport = nm[host]['tcp'].keys()   #<------ This 'proto' was changed from the [proto] to the ['tcp'].
    lport.sort()
    for port in lport:
                    print('----------------------------------------------------')
                    print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
                    print('----------------------------------------------------')

I'm not a python expert(yet) and had some help from a friend (Tx AdriaanDL :)). However it does sort out the problem I have been having with this sample that the nmap.py developers have on their website.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.