Skip to content

Provide NICs information a-la ifconfig #376

Description

@giampaolo

From g.rodola on May 10, 2013 03:16:52

This is somewhat related to issue 250 and aims to provide a replacement for 
ifconfig command on UNIX.
Whereas issue 250 aims to provide something like this:

    >>> psutil.network_ifaces()
    {'lo': nic(up=True, duplex=0, speed=0), 'eth0': nic(up=True, duplex=2, 
speed=100)}

...here we want to provide the IP address(es) and netmask associated with a 
network interface similarly to ifconfig.

It seems that on most (all?) POSIX system we can use getifaddrs(3).
Here's a Linux example using ctypes: 
http://carnivore.it/2010/07/22/python_-_getifaddrs It prints:

    {'eth0': {2: [{'addr': '192.168.1.2', 'netmask': '255.255.255.0'}],
              10: [{'addr': 'fe80::92e6:baff:fe80:e90d',
                    'netmask': 'ffff:ffff:ffff:ffff::',
                    'scope': 2L}],
              17: [{'addr': '90:e6:ba:80:e9:0d'}]},
     'lo': {2: [{'addr': '127.0.0.1', 'netmask': '255.0.0.0'}],
            10: [{'addr': '::1',
                  'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'}],
            17: [{'addr': '00:00:00:00:00:00'}]}}


Here's a similar implementation for OSX / BSD: 
http://carnivore.it/2010/07/22/python_-_getifaddrs On FreeBSD it prints:

    [ifaddrs(name='usbus0', flags=65537L, family=18, address='', netmask=None),
     ifaddrs(name='em0', flags=34883L, family=18, address='00:50:56:28:ec:d8', 
netmask=None),
     ifaddrs(name='em0', flags=34883L, family=2, address='10.31.8.132', 
netmask='255.255.255.0'),
     ifaddrs(name='usbus1', flags=65537L, family=18, address='', netmask=None),
     ifaddrs(name='plip0', flags=34832L, family=18, address='', netmask=None),
     ifaddrs(name='lo0', flags=32841L, family=18, address='', netmask=None),
     ifaddrs(name='lo0', flags=32841L, family=28, address='::1', 
netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'),
     ifaddrs(name='lo0', flags=32841L, family=28, address='fe80:5::1', 
netmask='ffff:ffff:ffff:ffff::'),
     ifaddrs(name='lo0', flags=32841L, family=2, address='127.0.0.1', 
netmask='255.0.0.0')]


In terms of final API it seems natural to provide a single function which 
provides all of these infos in one shot (NIC status up/down, speed, duplex plus 
all associated addresses provided by getifaddrs) but given that the internal 
implementation uses very different approaches I prefer to treat and develop the 
two functionalities separately for now.

Original issue: http://code.google.com/p/psutil/issues/detail?id=376

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

importedimported from the Google Code migration (deprecated)new-apithe public API grows: a new function, argument, or returned valueunixplatform : shared POSIX code, when no single OS fits

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions