Issue 376 in psutil: Provide NICs information a-la ifconfig

1 view
Skip to first unread message

psu...@googlecode.com

unread,
May 9, 2013, 9:17:06 PM5/9/13
to psutil-...@googlegroups.com
Status: New
Owner: g.rodola
CC: psutil-c...@googlegroups.com
Labels: Type-Enhancement Priority-Medium

New issue 376 by g.rodola: Provide NICs information a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

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.

- Giampaolo

--
You received this message because you were CC'd on the issue.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

Reply to this email to add a comment.

psu...@googlecode.com

unread,
May 13, 2013, 7:48:03 PM5/13/13
to psutil-...@googlegroups.com

Comment #1 on issue 376 by phi...@cloudera.com: Provide NICs information
a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

For what it's worth, http://alastairs-place.net/projects/netifaces/ is a
library with what looks like similar goals to yours.

psu...@googlecode.com

unread,
May 15, 2013, 7:43:17 PM5/15/13
to psutil-...@googlegroups.com

Comment #2 on issue 376 by g.rodola: Provide NICs information a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

Linux implementeation committed as revision 259cf1979d3a.

psu...@googlecode.com

unread,
May 16, 2013, 9:57:15 PM5/16/13
to psutil-...@googlegroups.com
Updates:
Status: Started

Comment #3 on issue 376 by g.rodola: Provide NICs information a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

FreeBSD implementation committed in revision f1e8283cb6a4.

psu...@googlecode.com

unread,
Dec 15, 2013, 7:43:06 AM12/15/13
to psutil-...@googlegroups.com

Comment #4 on issue 376 by tech...@gmail.com: Provide NICs information
a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

I can't see how can I get a list of all available network interface ids?

I am looking into some call to return ["lo", "eth0"] list in one step.
The next I'd like to do is to get all IPs for each interface "by-id".

psu...@googlecode.com

unread,
Dec 15, 2013, 7:47:08 AM12/15/13
to psutil-...@googlegroups.com

Comment #5 on issue 376 by tech...@gmail.com: Provide NICs information
a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

Probably a hack, but I found this:

$ cat /proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes
packets errs drop fifo colls carrier compressed
lo: 9108970 99862 0 0 0 0 0 0
9108970 99862 0 0 0 0 0 0
eth0: 350377139 290149 0 0 0 0 0 0
31993244 236043 0 0 0 0 0 0

psu...@googlecode.com

unread,
Dec 15, 2013, 7:49:37 AM12/15/13
to psutil-...@googlegroups.com

Comment #6 on issue 376 by tech...@gmail.com: Provide NICs information
a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

So, I an list those as:

ifaces = []
with open("/proc/net/dev", "rb") as netstat:
for i, line in enumerate(netstat):
if i < 2: # skip first two header lines
continue
parts = line.split(':')
ifaces += [parts[0].strip()]
print ifaces

psu...@googlecode.com

unread,
Dec 16, 2013, 8:30:43 AM12/16/13
to psutil-...@googlegroups.com

Comment #7 on issue 376 by g.rodola: Provide NICs information a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

I started developing this feature in a separate branch so that's why it is
not available yet (Windows, OSX and Solaris implementations are still
missing).
If you want a list of all available NIC names you can already do that by
using:

>>> import psutil
>>> list(psutil.net_io_counters(pernic=True))
['lo', 'wlan0', 'eth1']


Obviously you won't get the associated IP addresses.

psu...@googlecode.com

unread,
Dec 16, 2013, 10:45:27 AM12/16/13
to psutil-...@googlegroups.com

Comment #8 on issue 376 by tech...@gmail.com: Provide NICs information
a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

This interface is very hackish. =)

psu...@googlecode.com

unread,
Dec 16, 2013, 10:51:09 AM12/16/13
to psutil-...@googlegroups.com

Comment #9 on issue 376 by g.rodola: Provide NICs information a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

The main goal of net_io_counters() is to provide IO stats, not NIC names,
that's why "list(psutil.net_io_counters(pernic=True))" looks hackish.

psu...@googlecode.com

unread,
Dec 16, 2013, 11:48:47 AM12/16/13
to psutil-...@googlegroups.com

Comment #10 on issue 376 by tech...@gmail.com: Provide NICs information
a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

I found an easy way to list all NICs:

$ ls /sys/class/net
eth0 lo

psu...@googlecode.com

unread,
Dec 16, 2013, 11:50:08 AM12/16/13
to psutil-...@googlegroups.com

Comment #11 on issue 376 by tech...@gmail.com: Provide NICs information
a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

Or more pythonically:

>>> import os
>>> os.listdir('/sys/class/net')
['lo', 'eth0']

The only problem now is to get IPs for those.

psu...@googlecode.com

unread,
Dec 16, 2013, 11:52:18 AM12/16/13
to psutil-...@googlegroups.com

Comment #12 on issue 376 by g.rodola: Provide NICs information a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

The purpose of this ticket is not to provide an API to retrieve NIC names.
We already have a function which can indirectly be used to do that and when
this issue will be closed as fixed we'll have 2.
I don't think providing yet another (3) API for the same functionality is a
good idea.
We'll just use psutil.net_ifaces().keys() and be done with it.

psu...@googlecode.com

unread,
May 26, 2014, 11:13:05 AM5/26/14
to psutil-...@googlegroups.com

Comment #13 on issue 376 by g.rodola: Provide NICs information a-la ifconfig
http://code.google.com/p/psutil/issues/detail?id=376

psutil has been migrated from Google Code to Github (see:
http://grodola.blogspot.com/2014/05/goodbye-google-code-im-moving-to-github.html).
Please do NOT reply here but use this instead:
https://github.com/giampaolo/psutil/issues/376
Reply all
Reply to author
Forward
0 new messages