sockets - Python script to read through a list and grab reverse DNS entry -


background: have wanted try hand @ scripting here goes!

problem: when gethostbyaddr gets ip no dns entry errors , script doesn't continue on.

here have far:

import socket  file = 'serverlist'  f = open(file, 'r')   lines = f.readlines()  f.close()  in lines:     host = i.strip()     if socket.gethostbyaddr(host) return(true):         val1 = socket.gethostbyaddr(host)         print("%s - %s" % (host, val1))     else:         print ("%s - no entry" % (host)) 

but errors because return(true) not proper syntax.

can help?

thanks, j

as basic syntax, should remove return(true) mentioned itzmeontv.

however, if method fails throw kind of exception (i tried servers , got socket.gaierror), you'll want catch , handle cases try ... except:

import socket  file = 'serverlist'  f = open(file, 'r')  lines = f.readlines() f.close() in lines:     host = i.strip()     try:         val1 = socket.gethostbyaddr(host)         print("%s - %s" % (host, val1))     except socket.error, exc:         print ("%s - no entry, socket error: %s" % (host, exc)) 

i recommend reading through handling exceptions.


Comments