Get link speed programmatically?

匿名 (未验证) 提交于 2019-12-03 01:19:01

问题:

I am writing an application that reports attributes of network devices on the local machine. I need the mac address, mtu, link speed and a few others. I'm using udev for this. I've already figured out how to get the mac address and mtu, but not how to get the link speed. I can get it with ethtool from the terminal, but I need a way to get it programmatically.

Does anyone know how I can get the link speed attribute with udev or another library?

回答1:

You need to use the SIOCETHTOOL ioctl() call. There's a nice introduction to ioctl/SIOCETHTOOL call on LinuxJournal, and the code below (which is not intended to be an example of good C practices!) should show you how to use it to get the speed.

#include  #include  #include  #include  #include  #include  #include  #include  #include   int main (int argc, char **argv) {     int sock;     struct ifreq ifr;     struct ethtool_cmd edata;     int rc;      sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);     if (sock 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!