我正在使用暴力Python書,並且有一個錯誤阻止我移過第一個nmap構建(第40-41頁)。在VM上使用Ubuntu 14.04。我輸入'python nmapScan.py -H 10.50.60.125 -p 21,1720'到終端。「KeyError:」(插入IP)'「是什麼意思?
import nmap
import optparse
def nmapScan(tgtHost,tgtPort):
nmScan = nmap.PortScanner()
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()
以下是錯誤:
File "nmapScan.py", line 24, in <module>
main()
File "nmapScan.py", line 21, in main
nmapScan(tgtHost, tgtPort)
File "nmapScan.py", line 7, in nmapScan
state = nmScan[tgtHost]['tcp'][int(tgtPort)]['state']
File "build/bdist.linux-x86_64/egg/nmap/nmap.py", line 567, in __getitem__
KeyError:'10.50.60.125'
對不起獲取信息,但它爲我工作得很好。嘗試添加一些調試打印語句,例如'print nmap.all_hosts()'。有些nmap信息位於http://xael.org/norman/python/python-nmap/。 – matsjoyce 2015-03-18 22:14:27
你確實在問'KeyError是什麼意思?如果是這樣,請參閱[python文檔](https://wiki.python.org/moin/KeyError)。如果您的意思是「爲什麼我的PortScanner對象不包含我試圖掃描的主機」,請更新問題。 – 2015-03-18 22:20:02