shanecode / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

Provide NICs information a-la ifconfig #376

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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

Original issue reported on code.google.com by g.rodola on 10 May 2013 at 1:16

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

Original comment by phi...@cloudera.com on 13 May 2013 at 11:47

GoogleCodeExporter commented 9 years ago
Linux implementeation committed as revision 259cf1979d3a.

Original comment by g.rodola on 15 May 2013 at 11:42

GoogleCodeExporter commented 9 years ago
FreeBSD implementation committed in revision f1e8283cb6a4.

Original comment by g.rodola on 17 May 2013 at 1:57

GoogleCodeExporter commented 9 years ago
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".

Original comment by techtonik@gmail.com on 15 Dec 2013 at 12:43

GoogleCodeExporter commented 9 years ago
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

Original comment by techtonik@gmail.com on 15 Dec 2013 at 12:46

GoogleCodeExporter commented 9 years ago
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

Original comment by techtonik@gmail.com on 15 Dec 2013 at 12:49

GoogleCodeExporter commented 9 years ago
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.

Original comment by g.rodola on 16 Dec 2013 at 1:30

GoogleCodeExporter commented 9 years ago
This interface is very hackish. =)

Original comment by techtonik@gmail.com on 16 Dec 2013 at 3:45

GoogleCodeExporter commented 9 years ago
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.

Original comment by g.rodola on 16 Dec 2013 at 3:51

GoogleCodeExporter commented 9 years ago
I found an easy way to list all NICs:

    $ ls  /sys/class/net
    eth0  lo

Original comment by techtonik@gmail.com on 16 Dec 2013 at 4:48

GoogleCodeExporter commented 9 years ago
Or more pythonically:

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

The only problem now is to get IPs for those.

Original comment by techtonik@gmail.com on 16 Dec 2013 at 4:50

GoogleCodeExporter commented 9 years ago
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. 

Original comment by g.rodola on 16 Dec 2013 at 4:52

GoogleCodeExporter commented 9 years ago
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

Original comment by g.rodola on 26 May 2014 at 3:07

GoogleCodeExporter commented 9 years ago

Original comment by g.rodola on 3 Jun 2014 at 11:15