ifconfig in python

bruce bedouglas at earthlink.net
Tue Jan 20 16:31:59 CET 2009


hi...

in general, i've found that using "route" to find the iface for the default
gets me the interface in use... i then parse either ifconfig/iwconfig, to
get the address of the nic for that interface.. it's worked ok so far on
most machines i've dealt with...

thoughts/comments are of course welcome!


-----Original Message-----
From: python-list-bounces+bedouglas=earthlink.net at python.org
[mailto:python-list-bounces+bedouglas=earthlink.net at python.org]On Behalf
Of Ned Deily
Sent: Monday, January 19, 2009 6:49 PM
To: python-list at python.org
Subject: Re: ifconfig in python


In article
<e1a84d570901191634u27e6b5d7h2f05a698213f6173 at mail.gmail.com>,
 "James Mills" <prologic at shortcircuit.net.au> wrote:

> On Tue, Jan 20, 2009 at 10:28 AM, Nehemiah Dacres <vivacarlie at gmail.com>
> wrote:
> > Is ther an easy way to get the resolved ip address of the machine a
script
> > is running on? socket.gethostbyname(socket.gethostname) has only
returned
> > the ip address of my loop back interface ... not very usefull.
>
> That's because your /etc/hosts resolves
> your hostname to 127.0.0.1 :)
>
> And no I know of no "easy" way cross
> platform way. Perhaps parsing the output
> of ifconfig itself ?

Also, since the subject is on my brain at the moment, how to find "the
address" is not the right question to ask.  These days most systems have
multiple network interfaces (Ethernet, WiFi, dialup, et al) running
multiple protocols, like IPv4 and IPv6.  In general, there is no *one*
IP address of a machine; often there are many.  Ignoring that fact can
lead to subtle bugs, like the one causing a urllib2 regression test
failure that I've been squashing today!

--
 Ned Deily,
 nad at acm.org

--
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list