I am trying to do a reverse lookup for all the internal IP addresses, to validate the inventory that I have. I am looking to do this via Python. I am thinking of generating a csv file with all the internal IP addresses using the following code-
import ipaddress as ip
import pandas as pd
file_name='10Dot.csv'
a = ip.ip_network('10.0.0.0/8')
ip_list = []
for x in a.hosts():
ip_list.append(x.compressed)
df=pd.DataFrame({'IP_Address':ip_list})
df.to_csv(file_name, encoding='utf-8', index=False)
end = time.time()
print(end - start)
Similarly, I want to generate files for other internal networks. Then using the following function I am trying to go through each of the lines in the generated file to do a reverse lookup-
def reverse_lookup(host):
try:
lookup=socket.gethostbyaddr(str(host))[0]
except:
lookup="NA"
return lookup
If I read the csv file line by line it is very slow to get through all the IP addresses. I am trying to use multi-threads to pick chunks of the CSV file and execute the above function line by line. So with the 10.0.0.0/8 network, I have 16,777,214 rows in the file, I am thinking of diving this in 100 parts and generate a final file with host and the looked up value. How do I read the csv file in parallel for the threads and then combine them into a single file?
Also if you have a better approach to solving this problem please do let me know.