Enumerating Network Adapters

O

Olivier Parisy

Hi all,

I'd like to get, in a portable way, a list of all
network adapters on a local machine running some
python code.

socket.gethostname() does not seem enough, as it
only returns one name ; I'd like to get all
available IPs.

I could process ipconfig or ifconfig outputs, but
this is OS-dependant and not very future-proof...

Best regards,
Olivier Parisy.
 
D

Diez B. Roggisch

I could process ipconfig or ifconfig outputs, but
this is OS-dependant and not very future-proof...

This has been discussed quite a few times on this NG, search google groups.
However IMHO there is no general system built-in way (AFAIK not even posix)
for doing this, you'll end up parsing tool output anyway...
 
O

Olivier Parisy

Diez said:
This has been discussed quite a few times on this NG, search google groups.
However IMHO there is no general system built-in way (AFAIK not even posix)
for doing this, you'll end up parsing tool output anyway...

I should have done this first, my mistake...

But actually, a previously posted message suggests a portable way to get
the platform IPs. Citing John Abel :
> socket.gethostbyaddr( socket.gethostname() ) will return all the
> details that you need to know. For example, the output of that
> command on my machine is :
>
> ('hallows', [], ['101.101.101.102', '10.253.1.118'])

Are there some pitfalls I'm not aware of ?

Thanks,
Olivier Parisy.
 
D

Dave Brueck

Olivier Parisy wrote:
[snip]
But actually, a previously posted message suggests a portable way to get
the platform IPs. Citing John Abel :
socket.gethostbyaddr( socket.gethostname() ) will return all the
details that you need to know. For example, the output of that
command on my machine is :

('hallows', [], ['101.101.101.102', '10.253.1.118'])

Are there some pitfalls I'm not aware of ?

Doesn't seem to work right on Windows, for me at least:
('<my machine name>', [], ['169.254.0.100'])

It returned the least useful of my 3 IP addresses - my loopback adapter
- but didn't return my LAN and wireless IPs.

-Dave
 
T

Thomas Heller

Dave Brueck said:
Olivier Parisy wrote:
[snip]
But actually, a previously posted message suggests a portable way to get
the platform IPs. Citing John Abel :
socket.gethostbyaddr( socket.gethostname() ) will return all the
details that you need to know. For example, the output of that
command on my machine is :

('hallows', [], ['101.101.101.102', '10.253.1.118'])
Are there some pitfalls I'm not aware of ?

Doesn't seem to work right on Windows, for me at least:
('<my machine name>', [], ['169.254.0.100'])

It returned the least useful of my 3 IP addresses - my loopback
adapter - but didn't return my LAN and wireless IPs.

If anyone really needs it, I can post code for windows using ctypes to
collect ip-addresses and netmasks together with the name and description
of all network adapters.

Thomas
 
E

Elbert Lev

It returned the least useful of my 3 IP addresses - my loopback
Interesting! I have Python installed on 10 W2K servers and I'm using
the same construct to get addresses. Works fine for me.
 
D

Dave Brueck

Elbert said:
Interesting! I have Python installed on 10 W2K servers and I'm using
the same construct to get addresses. Works fine for me.

WinXP Pro here - I wonder if that's the difference.

-Dave
 
O

Olivier Parisy

Dave said:
WinXP Pro here - I wonder if that's the difference.

Interestingly, I have the same problem with XP Pro.

But socket.gethostbyname_ex seems to do the trick for me !

Regards,
Olivier.
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top