July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Wolfgang Strobl
You
need to find out which DNS name servers are being used, but
you’re on Microsoft Windows, so you
can’t just parse the
resolv.conf file, as you might do on Unix and
Unix-like platforms.
On Windows, DNS servers (like much other information) can be found in
the registry, which can be accessed with the standard module
_winreg:
import string import _winreg def binipdisplay(s): "convert a binary array of ip addresses to a python list" if len(s)%4!= 0: raise EnvironmentError # well ... ol=[] for i in range(len(s)/4): s1=s[:4] s=s[4:] ip=[] for j in s1: ip.append(str(ord(j))) ol.append(string.join(ip,'.')) return ol def stringdisplay(s): 'convert "d.d.d.d,d.d.d.d" to ["d.d.d.d","d.d.d.d"]' return string.split(s,",") def RegistryResolve( ): """ Return the list of dotted-quads addresses of name servers found in the registry -- tested on NT4 Server SP6a, Win/2000 Pro SP2, XP, ME (each of which has a different registry layout for nameservers!) """ nameservers=[] x=_winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE) try: y= _winreg.OpenKey(x, r"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters") except EnvironmentError: # so it isn't NT/2000/XP # Windows ME, perhaps? try: # for Windows ME y = _winreg.OpenKey(x, r"SYSTEM\CurrentControlSet\Services\VxD\MSTCP") nameserver, dummytype = _winreg.QueryValueEx(y,'NameServer') if nameserver and not (nameserver in nameservers): nameservers.extend(stringdisplay(nameserver)) ...
Read now
Unlock full access