[英]Python KeyError scanning list of hosts for open ports
I have a list of my ip's in my python script, and I'm trying to run a nmap scan on each of them to find open ports. 我的python脚本中有我的IP列表,并且我试图对它们中的每一个运行nmap扫描以查找开放端口。 I keep getting this error:
我不断收到此错误:
Traceback (most recent call last):
File "rscan.py", line 33, in <module>
main()
File "rscan.py", line 30, in main
vulnscan(nm, L)
File "rscan.py", line 6, in vulnscan
for port in nm[item].all_tcp():
File "build/bdist.linux-x86_64/egg/nmap/nmap.py", line 567, in __getitem__
KeyError: u'IP ADDRESS HERE'
(There is an actual ip address in the "IP ADDRESS HERE" part, though.) (不过,“ IP ADDRESS HERE”部分中有一个实际的IP地址。)
The scanning part of my code that I tried is is: 我尝试的代码的扫描部分是:
for item in L:
for port in nm[item].all_tcp():
state= nm[item]['tcp'][port]['state']
if state== 'open':
print state
'L' is the list that contains my ip addresses. “ L”是包含我的IP地址的列表。
What is the proper way to scan a small list of ip addresses for open ports using nmap? 使用nmap扫描一小部分IP地址以查找开放端口的正确方法是什么?
Like this, based on your code: 这样,根据您的代码:
for item in L:
if item in nm.all_hosts():
for port in nm[item].all_tcp():
state = nm[item]['tcp'][port]['state']
if state == 'open':
print state
Checking if specified ip
address is in nm.all_hosts()
(which returns a list of ips
) allows you to safely query nm[item]
afterwards. 检查指定的
ip
地址是否在nm.all_hosts()
(返回ips
列表)中,之后可以安全地查询nm[item]
。
Note that you could make this code a bit cleaner by simply intersecting your L list with the list of ip addresses returned by nm.all_hosts(): 请注意,您可以通过将L列表与nm.all_hosts()返回的IP地址列表相交来使此代码更简洁:
items = list(set(L) & set(nm.all_hosts()))
for item in items:
for port in nm[item].all_tcp():
state = nm[item]['tcp'][port]['state']
if state == 'open':
print state
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.