以编程方式获取链接速度?

发布于 2024-09-02 13:58:00 字数 183 浏览 3 评论 0原文

我正在编写一个报告本地计算机上网络设备属性的应用程序。我需要 MAC 地址、MTU、链接速度和其他一些信息。我为此使用 udev。我已经弄清楚如何获取 mac 地址和 mtu,但不知道如何获取链接速度。我可以使用 ethtool 从终端获取它,但我需要一种以编程方式获取它的方法。

有谁知道如何使用 udev 或其他库获取链接速度属性?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿越时光隧道 2024-09-09 13:58:00

您需要使用 SIOCETHTOOL ioctl() 调用。有一个很好的 ioctl/SIOCETHTOOL 介绍 在 LinuxJournal 上调用,下面的代码(这并不是良好的 C 实践的示例!)应该向您展示如何使用它来获得速度。

#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <linux/ethtool.h>
#include <string.h>
#include <stdlib.h>

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 < 0) {
        perror("socket");
        exit(1);
    }

    strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));
    ifr.ifr_data = &edata;

    edata.cmd = ETHTOOL_GSET;

    rc = ioctl(sock, SIOCETHTOOL, &ifr);
    if (rc < 0) {
        perror("ioctl");
        exit(1);
    }
    switch (ethtool_cmd_speed(&edata)) {
        case SPEED_10: printf("10Mbps\n"); break;
        case SPEED_100: printf("100Mbps\n"); break;
        case SPEED_1000: printf("1Gbps\n"); break;
        case SPEED_2500: printf("2.5Gbps\n"); break;
        case SPEED_10000: printf("10Gbps\n"); break;
        default: printf("Speed returned is %d\n", edata.speed);
    }

    return (0);
}

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 <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <linux/ethtool.h>
#include <string.h>
#include <stdlib.h>

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 < 0) {
        perror("socket");
        exit(1);
    }

    strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));
    ifr.ifr_data = &edata;

    edata.cmd = ETHTOOL_GSET;

    rc = ioctl(sock, SIOCETHTOOL, &ifr);
    if (rc < 0) {
        perror("ioctl");
        exit(1);
    }
    switch (ethtool_cmd_speed(&edata)) {
        case SPEED_10: printf("10Mbps\n"); break;
        case SPEED_100: printf("100Mbps\n"); break;
        case SPEED_1000: printf("1Gbps\n"); break;
        case SPEED_2500: printf("2.5Gbps\n"); break;
        case SPEED_10000: printf("10Gbps\n"); break;
        default: printf("Speed returned is %d\n", edata.speed);
    }

    return (0);
}
别低头,皇冠会掉 2024-09-09 13:58:00

您可以使用 sysfs 接口获取每秒比特数的链接速度:

cat /sys/class/net/eth0/speed
1000

You can get the link speed in bit per second using the sysfs interface:

cat /sys/class/net/eth0/speed
1000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文