Cross-platform way to retrieve the current (Operative system) DNS server IP address in python

Hans Mulder hansmu at xs4all.nl
Fri Apr 30 12:01:59 CEST 2010


joamag wrote:

> It's not my ip address that I want to discover... I want to discover
> my default dns server ip address.
> This ip is stored in an operative system basis.
> 
> Dos anyone know how to get it ?

You asked for a cross-platform solution; there probably isn't one.

The code below works on Unix and MacOS X.
If you can find code that works on Windows, you can cobble together
somthing that will work on most platforms.

dns_ips = []

for line in file('/etc/resolv.conf', 'r'):
     columns = line.split()
     if columns[0] == 'nameserver':
         dns_ips.extend(columns[1:])

print dns_ips


Hope this help,

-- HansM




More information about the Python-list mailing list