当前位置: 技术问答>linux和unix
急等~Linux网卡信息提取
来源: 互联网 发布时间:2017-05-25
本文导语: 代码功能是要求提取系统内的网卡信息,包括名称,类型,MAC,IP,MASK,速度等等。用C编写,数据存到结构体里。 因为多数数据在ifconfig命令下都可以显示,所以我的想法是从命令返回值中提取数据,使用popen , fgets...
代码功能是要求提取系统内的网卡信息,包括名称,类型,MAC,IP,MASK,速度等等。用C编写,数据存到结构体里。
因为多数数据在ifconfig命令下都可以显示,所以我的想法是从命令返回值中提取数据,使用popen , fgets,sscanf等函数实现。但是问题来了,在多网卡的系统下,使用命令ifconfig返回的内容很多,格式也不固定,提取起来很困难,比如:
xxx@xxxxx:~$ ifconfig
br0 Link encap:Ethernet HWaddr a4:1f:72:98:ec:a9
inet addr:172.16.200.114 Bcast:172.16.200.255 Mask:255.255.255.0
inet6 addr: fe80::a61f:72ff:fe98:eca9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:602739296 errors:0 dropped:0 overruns:0 frame:0
TX packets:562145310 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:261880870281 (261.8 GB) TX bytes:480978897517 (480.9 GB)
eth0 Link encap:Ethernet HWaddr a4:1f:72:98:ec:a9
inet6 addr: fe80::a61f:72ff:fe98:eca9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:686510262 errors:0 dropped:527755 overruns:0 frame:0
TX packets:726027323 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:278594375202 (278.5 GB) TX bytes:494699541647 (494.6 GB)
我需要提取每一个网卡端口的对应信息,工作中刚接触C不久,所以请教各位有没有好的算法来提取?或者使用其他方式提取这些信息呢?
因为多数数据在ifconfig命令下都可以显示,所以我的想法是从命令返回值中提取数据,使用popen , fgets,sscanf等函数实现。但是问题来了,在多网卡的系统下,使用命令ifconfig返回的内容很多,格式也不固定,提取起来很困难,比如:
xxx@xxxxx:~$ ifconfig
br0 Link encap:Ethernet HWaddr a4:1f:72:98:ec:a9
inet addr:172.16.200.114 Bcast:172.16.200.255 Mask:255.255.255.0
inet6 addr: fe80::a61f:72ff:fe98:eca9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:602739296 errors:0 dropped:0 overruns:0 frame:0
TX packets:562145310 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:261880870281 (261.8 GB) TX bytes:480978897517 (480.9 GB)
eth0 Link encap:Ethernet HWaddr a4:1f:72:98:ec:a9
inet6 addr: fe80::a61f:72ff:fe98:eca9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:686510262 errors:0 dropped:527755 overruns:0 frame:0
TX packets:726027323 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:278594375202 (278.5 GB) TX bytes:494699541647 (494.6 GB)
我需要提取每一个网卡端口的对应信息,工作中刚接触C不久,所以请教各位有没有好的算法来提取?或者使用其他方式提取这些信息呢?
|
or munin or nagios. There are so many free open source monitoring tools made by perl/python/C. I just worry that you are reinventing the wheel...
|
用ioctl可以获得指定网卡的IP地址 MAC地址 广播地址 子网掩码
{
struct sockaddr_in *addr;
struct sockaddr sa;
struct ifreq ifr;
char *name,*address;
int sockfd;
unsigned char mac[6];
if(argc != 2) {
printf("usage : ipconfig interface /n");
return -1;
}
else{
name = argv[1];
}
sockfd = socket(AF_INET,SOCK_DGRAM,0);
strncpy(ifr.ifr_name,name,IFNAMSIZ-1);
if(ioctl(sockfd,SIOCGIFADDR,&ifr) == -1){
perror("ioctl error");
return -1;
}
addr = (struct sockaddr_in *)&(ifr.ifr_addr);
address = inet_ntoa(addr->sin_addr);
printf("inet addr: %s n",address);
if(ioctl(sockfd,SIOCGIFBRDADDR,&ifr) == -1){
perror("ioctl error");
return -1;
}
addr = (struct sockaddr_in *)&ifr.ifr_broadaddr;
address = inet_ntoa(addr->sin_addr);
printf("broad addr: %s n",address);
if(ioctl(sockfd,SIOCGIFNETMASK,&ifr) == -1){
perror("ioctl error");
return -1;
}
addr = (struct sockaddr_in *)&ifr.ifr_addr;
address = inet_ntoa(addr->sin_addr);
printf("inet mask: %s n",address);
if(ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1){
perror("ioctl error");
return -1;
}
memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in));
memcpy(mac, sa.sa_data, sizeof(mac));
printf("%s mac: %.2X:%.2X:%.2X:%.2X:%.2X:%.2Xn",name, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return 1;
}
{
struct sockaddr_in *addr;
struct sockaddr sa;
struct ifreq ifr;
char *name,*address;
int sockfd;
unsigned char mac[6];
if(argc != 2) {
printf("usage : ipconfig interface /n");
return -1;
}
else{
name = argv[1];
}
sockfd = socket(AF_INET,SOCK_DGRAM,0);
strncpy(ifr.ifr_name,name,IFNAMSIZ-1);
if(ioctl(sockfd,SIOCGIFADDR,&ifr) == -1){
perror("ioctl error");
return -1;
}
addr = (struct sockaddr_in *)&(ifr.ifr_addr);
address = inet_ntoa(addr->sin_addr);
printf("inet addr: %s n",address);
if(ioctl(sockfd,SIOCGIFBRDADDR,&ifr) == -1){
perror("ioctl error");
return -1;
}
addr = (struct sockaddr_in *)&ifr.ifr_broadaddr;
address = inet_ntoa(addr->sin_addr);
printf("broad addr: %s n",address);
if(ioctl(sockfd,SIOCGIFNETMASK,&ifr) == -1){
perror("ioctl error");
return -1;
}
addr = (struct sockaddr_in *)&ifr.ifr_addr;
address = inet_ntoa(addr->sin_addr);
printf("inet mask: %s n",address);
if(ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1){
perror("ioctl error");
return -1;
}
memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in));
memcpy(mac, sa.sa_data, sizeof(mac));
printf("%s mac: %.2X:%.2X:%.2X:%.2X:%.2X:%.2Xn",name, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return 1;
}