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

Discussion in 'Python' started by joamag, Apr 22, 2010.

  1. joamag

    joamag Guest

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

    Thanks !
    João
     
    joamag, Apr 22, 2010
    #1
    1. Advertisements

  2. joamag

    DarkBlue Guest

    On Apr 22, 4:55 pm, joamag <> wrote:
    > 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.
     
    DarkBlue, Apr 24, 2010
    #2
    1. Advertisements

  3. joamag

    joamag Guest

    On 24 Abr, 14:50, DarkBlue <> wrote:
    > On Apr 22, 4:55 pm, joamag <> wrote:
    >
    > > 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 ?
     
    joamag, Apr 27, 2010
    #3
  4. joamag

    Hans Mulder Guest

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

    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
     
    Hans Mulder, Apr 30, 2010
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Martelli
    Replies:
    25
    Views:
    1,801
    MrJean1
    Dec 1, 2005
  2. techtonik
    Replies:
    3
    Views:
    521
    Gabriel Genellina
    Feb 11, 2007
  3. =?iso-8859-1?B?QW5kcuk=?=
    Replies:
    8
    Views:
    582
    =?iso-8859-1?B?QW5kcuk=?=
    May 1, 2007
  4. John
    Replies:
    11
    Views:
    5,990
    Lawrence Oluyede
    Oct 8, 2007
  5. howa
    Replies:
    5
    Views:
    422
    James Kanze
    Jan 20, 2008
  6. chewie54
    Replies:
    2
    Views:
    378
  7. Roedy Green
    Replies:
    4
    Views:
    696
    Tim Slattery
    Mar 18, 2010
  8. Metallicow
    Replies:
    10
    Views:
    452
    Chris Angelico
    Jul 12, 2013
Loading...