nikademus79 / psutil

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

Get IP addresses #204

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This is an enhancement request to allow for getting network interface IP 
address(es) with the new network APis.  For example, you could use 
psutil.network_io_counters() to return not only the interface name but also the 
IP address(es) associated with it.  The reason for this is there is no good way 
to do this in Python now and the netifaces 
(http://alastairs-place.net/netifaces/) Python module seems to be no longer 
maintained.  It would be great to get this information if it fits within the 
scope of psutil.

Original issue reported on code.google.com by jcscoob...@gmail.com on 12 Sep 2011 at 4:18

GoogleCodeExporter commented 8 years ago
I'm not sure about the implications in terms of implementation on every 
platform, but I guess it would be a good addition.
I can research what information we can gather on Linux and Windows.
Generally speaking, what I imagine is a list of namedtuples for every network 
interface installed on the system including what is returned by ifconfig. More 
or less:
- interface name
- inet4 address
- inet6 address (if any)
- broadcast address
- subnet mask

I think sigar [1] has something similar so we might borrow some code from there.

http://support.hyperic.com/display/SIGAR/Home

Original comment by g.rodola on 12 Sep 2011 at 8:05

GoogleCodeExporter commented 8 years ago
I can pretty much do OS X support based on code from ifconfig/netstat.  I've 
already written a test and gotten things working somewhat.  Just let me know 
and we'll go from there.

Original comment by jcscoob...@gmail.com on 12 Sep 2011 at 10:17

GoogleCodeExporter commented 8 years ago
Great! Please go on.

Original comment by g.rodola on 13 Sep 2011 at 8:14

GoogleCodeExporter commented 8 years ago
It seems we might be able to do this entirely in python, assuming we have a C 
function returning all the interface names (which we already have).
From 
http://code.activestate.com/recipes/439094-get-the-ip-address-associated-with-a-
network-inter/ :

import socket
import fcntl
import struct

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

def get_mac_addr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]

print get_ip_address('eth0')
print get_mac_addr('eth0')

I still haven't checked whether this works on Windows though.
Also, here's another example we might want to take a look at:
http://code.google.com/p/python-ifconfig/source/browse/trunk/ifconfig.c

Original comment by g.rodola on 13 Sep 2011 at 1:36

GoogleCodeExporter commented 8 years ago
I should get more sleep. 
Title says "linux only" and in fact none of this works on FreeBSD or OSX. 

Original comment by g.rodola on 13 Sep 2011 at 2:12

GoogleCodeExporter commented 8 years ago
After having read netifaces [1] README I figured out this is more complex than 
I thought at first and things work differently than I expected.
To say one, more than one IP address can be associated with a network 
interface, therefore my API design doesn't make sense and thinking about a new 
one is not immediately obvious.

Considering the amount of work and the existance of the netifaces module which, 
despite not being actively maintained looks quite healthy to me, I'm no longer 
sure this is worth the effort.
If you think differently and still want to give it a try I can create a SVN 
branch for you and we can keep discussing about the API and the implementation 
but consider that this should be implemented at least for OSX, FreeBSD and 
Linux and possibly also for Windows.

[1] http://pypi.python.org/pypi/netifaces/0.3 and 
http://alastairs-place.net/2007/03/netifaces/netifaces-0.5.tar.gz

Original comment by g.rodola on 14 Sep 2011 at 9:35

GoogleCodeExporter commented 8 years ago
I trust your judgement.  It's really not that important yet.  As for 
difficulty, it wouldn't be too hard to have a list of known addresses 
associated with an interface, ipv4 and ipv6.  Since it's no longer a need, I 
don't mind dropping this for now.

Original comment by jcscoob...@gmail.com on 14 Sep 2011 at 9:53

GoogleCodeExporter commented 8 years ago

Original comment by jlo...@gmail.com on 16 Sep 2011 at 6:53

GoogleCodeExporter commented 8 years ago

Original comment by g.rodola on 28 Sep 2011 at 7:55