Determine the IP address of an eth interface

B

billie

Hi all. I'm searching for a module that permits me to low-level interact
with ethernet interfaces of my system.
I would like to determine at least the first of the followings values:

1 - IP address assigned to the interface
2 - subnet mask
3 - default gateway
4 - primary and secondary dns
5 - default wins server
6 - mac address
7 - broadcast address

On python cookbook I found this usefuls script that returns the IP address
of a specified interface but it only works on unix platforms. I was
wondering if exist a specific module that could solve this kind of
problems.
Best regards.

import socket
import fcntl
import struct

def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
'127.0.0.1'
'38.113.228.130'
 
M

Michael Amrhein

billie said:
Hi all. I'm searching for a module that permits me to low-level interact
with ethernet interfaces of my system.
I would like to determine at least the first of the followings values:

1 - IP address assigned to the interface
2 - subnet mask
3 - default gateway
4 - primary and secondary dns
5 - default wins server
6 - mac address
7 - broadcast address

On python cookbook I found this usefuls script that returns the IP address
of a specified interface but it only works on unix platforms. I was
wondering if exist a specific module that could solve this kind of
problems.
Best regards.

import socket
import fcntl
import struct

def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
'127.0.0.1'
'38.113.228.130'

On win... you may call ipconfig: os.popen('ipconfig /all','r')
- if ipconfig on your default path, otherwise specify full path -
and retrieve the information you want from the output.

Have fun
Michael
 
T

Tim Golden

[billie]

| Hi all. I'm searching for a module that permits me to
| low-level interact
| with ethernet interfaces of my system.
| I would like to determine at least the first of the followings
values:
|
| 1 - IP address assigned to the interface
| 2 - subnet mask
| 3 - default gateway
| 4 - primary and secondary dns
| 5 - default wins server
| 6 - mac address
| 7 - broadcast address

Not a cross-platform solution (I doubt if there is one, strictly)
but you can use WMI on win32 to get most of what you want. I'm
not sure about the broadcast address, but some Googling around
might help to solve that:

(wmi module from http://timgolden.me.uk/python/wmi.html)

<code>
import wmi
c = wmi.WMI ()
for eth in c.Win32_NetworkAdapterConfiguration (IPEnabled=True):
print eth

</code>

TJG
 

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,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top