I have a file containing different ip ranges with different subnets. I want now to get all the host from every ranges. So I used the ipadress library with the hosts()
function:
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])
Currently my list is empty
adriano@K62606:~/findRoute$ python3 workingWithMask.py
<class 'list'>
<class 'list'>
and therefore I get the error:
<class 'list'>
Traceback (most recent call last):
File "workingWithMask.py", line 16, in <module>
for i in list:
TypeError: 'type' object is not iterable
I precise, that the file is readed correctly
myList = ipaddress.ip_network(u''+newLine, False)
list(myList.hosts())
print(list)
for i in list:
You convert myList.hosts()
to a list but throw it away, then printing the built-in type list
then trying to iterate over it which makes no sense at all.
You have to keep the result of list(...)
somewhere, then iterate over that.
Consider:
myList = list(ipaddress.ip_network(u''+newLine, False).hosts())
print(myList)
for i in myList:
subprocess.run(["host", i])