Objective is to read a list of domains from a file and perform lookup to confirm reachability and resolution from my end.
This is what I have written:
#!/usr/bin/python
import os
import socket
f = open('file1.lst', 'r')
s = f.readlines()
for i in s:
print i
socket.gethostbyname(i.strip())
f.close()
socket.gethostbyname()
line throws an exception.
for i in s:
print i
try:
socket.gethostbyname(i.strip())
except socket.gaierror:
print "unable to get address for", i
If an address could not be found, then gethostbyname
will raise an exception (not throw). This is the way error handling is done in Python. If you know how to properly deal with the error, the you should catch it with an except
clause.
Note that you will need some more code to also check for connectivity.