如何在 Linux/Mac OSX 中获取网络适配器统计信息?

发布于 2024-07-27 03:48:31 字数 277 浏览 2 评论 0 原文

我正在寻找一种在 Linux 和 MacOSX 上用 C 语言获取网络统计信息的方法。 具体来说,我需要监视从系统上每个网络适配器上传和下载的字节数 - 我不需要进行数据包检查,或区分协议,只需一个我可以定期轮询的“总字节”计数器即可没事的。 在 Windows 中,我可以通过 GetIfTable(列出网络适配器)和 GetIfEntry(读取统计信息)使用 iphlpapi.dll 库来执行此操作,但我找不到 Linux/OSX 等效项。 我对 C 的了解相当基础,所以我希望有一个不太复杂的解决方案。 任何帮助将非常感激!

I'm looking for a way to get hold of network stats in C on Linux and MacOSX. Specifically, I need to monitor the number of bytes uploaded and downloaded from each network adapter on the system - I don't need to do packet inspection, or differentiate between protocols, just a 'total bytes' counter which I can poll at intervals would be fine. In Windows I can do this using the iphlpapi.dll library via GetIfTable (to list the network adapters) and GetIfEntry (to read the stats), but I can't find the Linux/OSX equivalents. My knowledge of C is fairly basic so I would appreciate a solution that isn't too involved. Any help would be much appreciated!

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

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

发布评论

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

评论(3

初吻给了烟 2024-08-03 03:48:32

Darwin netstat 源代码使用 sysctl。
下面是一些在 OSX 上打印输入和输出字节数的代码:

#import <Foundation/Foundation.h>
#include <sys/sysctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/route.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int mib[] = {
        CTL_NET,
        PF_ROUTE,
        0,
        0,
        NET_RT_IFLIST2,
        0
    };
    size_t len;
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        fprintf(stderr, "sysctl: %s\n", strerror(errno));
        exit(1);
    }
    char *buf = (char *)malloc(len);
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        fprintf(stderr, "sysctl: %s\n", strerror(errno));
        exit(1);
    }
    char *lim = buf + len;
    char *next = NULL;
    u_int64_t totalibytes = 0;
    u_int64_t totalobytes = 0;
    for (next = buf; next < lim; ) {
        struct if_msghdr *ifm = (struct if_msghdr *)next;
        next += ifm->ifm_msglen;
        if (ifm->ifm_type == RTM_IFINFO2) {
            struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm;
            totalibytes += if2m->ifm_data.ifi_ibytes;
            totalobytes += if2m->ifm_data.ifi_obytes;
        }
    }
    printf("total ibytes %qu\tobytes %qu\n", totalibytes, totalobytes);
    [pool drain];
    return 0;
}

The Darwin netstat source code uses sysctl.
Here's some code that prints the number of bytes in and out on OSX:

#import <Foundation/Foundation.h>
#include <sys/sysctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/route.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int mib[] = {
        CTL_NET,
        PF_ROUTE,
        0,
        0,
        NET_RT_IFLIST2,
        0
    };
    size_t len;
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        fprintf(stderr, "sysctl: %s\n", strerror(errno));
        exit(1);
    }
    char *buf = (char *)malloc(len);
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        fprintf(stderr, "sysctl: %s\n", strerror(errno));
        exit(1);
    }
    char *lim = buf + len;
    char *next = NULL;
    u_int64_t totalibytes = 0;
    u_int64_t totalobytes = 0;
    for (next = buf; next < lim; ) {
        struct if_msghdr *ifm = (struct if_msghdr *)next;
        next += ifm->ifm_msglen;
        if (ifm->ifm_type == RTM_IFINFO2) {
            struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm;
            totalibytes += if2m->ifm_data.ifi_ibytes;
            totalobytes += if2m->ifm_data.ifi_obytes;
        }
    }
    printf("total ibytes %qu\tobytes %qu\n", totalibytes, totalobytes);
    [pool drain];
    return 0;
}
儭儭莪哋寶赑 2024-08-03 03:48:32

我无法与 OSX 对话,但在 Linux 上查看 /proc/net/dev。

如果执行“cat /proc/net/dev”,您应该会看到包括“字节”在内的统计信息——接口传输或接收的数据字节总数。 您可以在自己的程序中读取该文件。

编辑:

我没有阅读你的整个问题。 这篇文章应该帮助您开始使用 /proc 并且有一个关于 /proc/net/ 的部分开发人员。

另外,要列出接口,您可以使用 ioctl ://linux.die.net/man/2/ioctl_list" rel="nofollow noreferrer">SIOCGIFCONF 选项。 您可以通过 Google 搜索有关如何循环返回的数据的不错的代码示例。 或者你可以简单地从上面提到的 /proc.net/dev 数据中提取它,这应该更容易。

I can't speak to OSX but on linux take a look at /proc/net/dev.

If you do 'cat /proc/net/dev' you should see statistics including 'bytes' - the total number of bytes of data transmitted or received by the interface. You can read the file within your own program.

EDIT:

I didn't read your whole question. This article should help you get started with /proc and has a section on /proc/net/dev.

Also, to list the interfaces you can call ioctl with the SIOCGIFCONF option. You can Google for a decent code example on how to loop through the returned data. Or you can simply pull it out of the /proc.net/dev data mentioned above, which should be easier.

我的奇迹 2024-08-03 03:48:32

在 Linux 上:

  • 低级别:检查 /sys/class/net/eth0/statistics/
  • 稍高级别:ip -s link show eth0
  • 图形:iftop
  • 互动:iptraf

on Linux:

  • low level: check /sys/class/net/eth0/statistics/
  • slightly higher level: ip -s link show eth0
  • graphical: iftop
  • interactive: iptraf
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文