[Tutor] Re: How to find out DNS ?

Derrick 'dman' Hudson dman@dman.ddts.net
Thu, 27 Jun 2002 12:55:20 -0500


--8t9RHnE3ZwKMSgU+
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Thu, Jun 27, 2002 at 07:03:34PM +0200, A wrote:

| Is there a way how to find out, from Python , what primary or=20
| secondary DNS I use when connecting to internet?

f =3D open( "/etc/resolv.conf" , "r" )
nameservers =3D []
for line in f.xreadlines() :
    line =3D line.strip()
    if line.startswith( "nameserver" ) :
        _ , ip =3D line.split()
        nameservers.append( ip )
f.close()
for ns in nameserver :
    print "nameserver" , ns


Of course, this only works on a UNIX-like system where
/etc/resolv.conf is used to list the nameservers.  A common setup (on
unix systems) is for some entries to be in /etc/hosts which takes
precedence over DNS lookups.  Any given application can also do its
own thing too, if it wants.  For example, the 'host' command can be
told to contact any arbitrary host and submit a DNS query to it.  Some
setups will have LDAP (or maybe NIS) also serving host information, in
which case DNS isn't involved for those hosts.

Why do you want to know what the nameserver(s) are?  Usually it is
better to not know that and just let the underlying libresolv handle
name resolution for you.  (program to the interface, not the
implementation ...)

-D

--=20

Microsoft is to operating systems & security ....
                                     .... what McDonald's is to gourmet coo=
king
=20
http://dman.ddts.net/~dman/


--8t9RHnE3ZwKMSgU+
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj0bUYcACgkQO8l8XBKTpRR4cgCdHiTyTTOJrHT/0Da5+TbhUJUE
cBoAoKi8bnhEQkRUOdTwi8BQ2bW48/7r
=e7GP
-----END PGP SIGNATURE-----

--8t9RHnE3ZwKMSgU+--