1

I have a simple Python program which uses nmap library to do port scanning.

from optparse import OptionParser
import nmap 
from threading import * 
screenLock=Semaphore(value=1)
def nmapScan(thost,tport):
 x=nmap.PortScanner()
 x.scan(thost,tport)
 state=x[thost]['tcp'][int(tport)]['state']
 print "[*]" + thost + "tcp/"+tport+" "+state
def main():
 parser=OptionParser('usage %prog -H <target host> -p <target port>')
 parser.add_option('-H',dest='thost',type='string',help='specify target host')
 parser.add_option('-p',dest='tports',type='string',help='specify target port[s] seperated by comma')
 (options,args)=parser.parse_args()
 thost=options.thost
 tports=options.tports
 tports=tports.split(',')
 if (thost==None)|(tports==None):
 print parser.usage
 exit(0)
 for i in tports:
 nmapScan(thost,i)
main()

When i run the program, i get the following error.

akshayrajmacbookpro$ python nmapScanner.py -H 192.168.1.60 -p 80,443
Traceback (most recent call last):
 File "nmapScanner.py", line 28, in <module>
main()
File "nmapScanner.py", line 26, in main
nmapScan(thost,i)
File "nmapScanner.py", line 10, in nmapScan
state=x[thost]['tcp'][int(tport)]['state']
File "build/bdist.macosx-10.11-intel/egg/nmap/nmap.py", line 555, in __getitem__
KeyError: '192.168.1.60'

I tried using url instead of ip in the command line. But I get the same error. Being new to Python, I am not able to understand and resolve this.

halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked May 20, 2016 at 12:12
1
  • What do you expect state to have? Commented May 20, 2016 at 12:24

2 Answers 2

2

x (instance of nmap.PortScanner) does not contain those keys. To be able to iterate the scan results you can do this:

for host, result in x._scan_result['scan'].items():
 print "[*]" + thost + "tcp/" + tport + " " + result['status']['state']

It's best if you looked at the docs or source code of python-nmap to see what other useful info is available e.g. Service name and version that is listening on that port.

More info here: https://bitbucket.org/xael/python-nmap/src/f368486a2cf12ce2bf3d5978614586e89c49c417/nmap/nmap.py?at=default&fileviewer=file-view-default#nmap.py-381

answered May 20, 2016 at 12:40
Sign up to request clarification or add additional context in comments.

2 Comments

But according to the documentation at pypi.python.org/pypi/python-nmap, my syntax is correct @fips
You are right, it seems both options should work. Actually when you do x[host] it accesses _scan_result['scan'] internally which is essentially the same thing. So you were getting a key error because that host did not exist in the result. Here is the line that attempts to get the specified host results: bitbucket.org/xael/python-nmap/src/… Maybe you can check before accessing it using if host in x.all_hosts().
0

The host does not exist in the result of the scan as a "Key" for the Dictionary that forms a part of the data thrown up by scan data. That is probably, in my opinion, the reason for the error. Thanks

answered Nov 21, 2016 at 18:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.