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

J

joamag

Does anybody know a cross platform way to retrieve the default DNS
server IP address in python ?

Thanks !
João
 
D

DarkBlue

Does anybody know a cross platform way to retrieve the default DNS
server IP address in python ?

Thanks !
João


import os,urllib2,re


def getIpAddr():
"""
Function for parsing external ip adress by pinging dyndns.com
"""
External_IP=urllib2.urlopen('http://checkip.dyndns.com/').read()
m = re.search(r"(([0-9]+\.){3}[0-9]+)", External_IP)
my_IP= m.group(1)
return my_IP




print('Current Ip from DynDns : %s ') % getIpAddr()



this gets you your ip address

hope it helps.
 
J

joamag

Does anybody know a cross platform way to retrieve the default DNS
server IP address in python ?
Thanks !
João

import os,urllib2,re

def getIpAddr():
    """
    Function for parsing external ip adress by pinging dyndns.com
    """
    External_IP=urllib2.urlopen('http://checkip.dyndns.com/').read()
    m = re.search(r"(([0-9]+\.){3}[0-9]+)", External_IP)
    my_IP= m.group(1)
    return my_IP

print('Current Ip from DynDns :  %s ') % getIpAddr()

this gets you your ip address

hope it helps.

Hi,

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 ?
 
H

Hans Mulder

joamag said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top