I want to get the following details for all the NICs attached to my computer:
1) Interface name (eg. eth0)
2) Interface Number (like in Windows) if such a thing exists in Linux
3) NIC bandwidth capacity and mode (eg. 1Gb/s full duplex)
I want to get the following details for all the NICs attached to my computer:
1) Interface name (eg. eth0)
2) Interface Number (like in Windows) if such a thing exists in Linux
3) NIC bandwidth capacity and mode (eg. 1Gb/s full duplex)
You can use
getifaddrs()
/freeifaddrs()
to obtain a linked list of all interfaces, thenioctl(fd, SIOCGIFINDEX, struct ifreq *)
to obtain the interface index for each. Since the interfaces are consecutive and always listed (regardless of whether or they are up (active) or not), I choose to enumerate them with a loop usingioctl(fd, SIOCGIFNAME, struct ifreq *)
instead. In all casesfd
is anAF_INET
socket.To obtain the duplex and speed of the interface, you need to use the
ioctl(fd, SIOCETHTOOL, struct ifreq *)
with theifr_data
pointing to astruct ethtool_cmd
havingcmd = ETHTOOL_GSET
.The ioctls should return -1 if they fail, and a nonnegative value (zero, I believe) if success.
Here is an example program:
If you save the above as
iflist.c
, you can compile it usingTo see the usage, run
iflist -h
. To list all interfaces, run it without parameters:The above will use the enumeration method I described. To list only specific interfaces, run it naming the interfaces:
Duplex and speed is only listed for ethernet interfaces, of course.
Edited to add:
If the above program does not supply the bandwidth and mode for an interface, here is a simplified version which reports the exact reason (errors). This one takes the interface names as commandline parameters; it does not enumerate interfaces.
Questions?
this command will list all the details about eth1 including speed, duplex, port...
you can use popen() to get the output and map it.
the following link well explain the
getifaddrs
function with a working examplegetifaddrs()
(1) getifaddrs()
(2) if_indextoname(), if_nameindex(), if_nametoindex()
(3) I'm not sure about this one but you might be able to get at it through ioctl() and one of the
SIOCGIF*
parameters or from/proc
.