【发布时间】:2020-02-11 09:10:23
【问题描述】:
我有一个文件,其中包含具有不同子网的不同 IP 范围。我现在想从每个范围内获取所有主机。所以我使用了ipadress library 和hosts() 函数:
import subprocess
import ipaddress
if __name__ == "__main__":
#host='8.8.8.8'
#subprocess.run(["host", host])
f=open('ip.txt', 'r')
for line in f:
#subprocess.run(["host", line])
newLine=line+''
newLine=newLine[:-1]#remove EOL
#print(newLine)
myList=ipaddress.ip_network(u''+newLine, False)#create the object
list(myList.hosts())
print(list)
for i in list:
subprocess.run(["host", i])
目前我的列表是空的
adriano@K62606:~/findRoute$ python3 workingWithMask.py
<class 'list'>
<class 'list'>
因此我得到了错误:
<class 'list'>
Traceback (most recent call last):
File "workingWithMask.py", line 16, in <module>
for i in list:
TypeError: 'type' object is not iterable
我准确地说,文件被正确读取了
【问题讨论】:
标签: python-3.x ip-address