Reading network interface data in linux

Edvard Majakari edvard+news at majakari.net
Wed Jul 13 04:09:57 EDT 2005


Suppose one wants to fetch the following data from given network interface,
say, eth0:

>>> Ethinf('eth0').addr()
'192.168.1.42/24'
>>> Ethinf('eth0').route('default')
'192.168.1.1'
>>> Ethinf('eth0').duplex()
'full'
>>> Ethinf('eth0').speed()
100

Some statistics:

>>> Ethstat('eth0').rx_bytes()
14325235341223
>>> Ethstat('eth0').tx_bytes()
2513152423

One could implement modules by installing eg. ethtool and reading speed/duplex
information by parsing ethtool output, but it is ugly way to do it, prone to
errors, requires launching a process as well as installation of ethtool.

As for the byte counts, you could get all information ifconfig show by reading
/proc/net/dev (which is more nice to parse), but it seems like those counters
are 32-bit, and they wrap around quite quickly in a 1000 gbit network (~30
seconds).

I was wondering is there really no module implemented which already does those
things?

I've already implemented a proto by doing exactly as I told (parsing ethtool &
/proc/net/dev), but I strive for more elegant solution. I'm willing to join
the effort with someone else, if possible.

-- 
#!/usr/bin/perl -w
$h={23,69,28,'6e',2,64,3,76,7,20,13,61,8,'4d',24,73,10,'6a',12,'6b',21,68,14,
72,16,'2c',17,20,9,61,11,61,25,74,4,61,1,45,29,20,5,72,18,61,15,69,20,43,26,
69,19,20,6,64,27,61,22,72};$_=join'',map{chr hex $h->{$_}}sort{$a<=>$b}
keys%$h;m/(\w).*\s(\w+)/x;$_.=uc substr(crypt(join('',60,28,14,49),join'',
map{lc}($1,substr $2,4,1)),2,4)."\n"; print;



More information about the Python-list mailing list