这几天要写一个监控之类东东,其中网卡一项要计算利用率,那就要取得网卡本身速度才能计算出来,本来想用perl实现,但发现网上没有现成的东东,后来几经辗转,最后想起ethtool能取到,就参考了此源码,贴出来供大家以后有个思路吧,
有时间再转成perl的:)
直接编译命令:gcc -p -g getNet.c && gcc -o getNet getNet.c && ./getNet eth0
源码如下:
// filename: getNet.c
// command sample: ./getNet eth0
// compile command: gcc -p -g getNet.c && gcc -o getNet getNet.c
#include <string.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#ifndef SIOCETHTOOL
#define SIOCETHTOOL 0x8946
#endif
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeo