Determine the IP address of an eth interface

billie mlist at
Wed Jan 18 13:14:44 EST 2006

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
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(
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])

>>> get_ip_address('lo')

>>> get_ip_address('eth0')

More information about the Python-list mailing list