简介
在数据传输过程中出现网络偶然间断开的情况,考虑是否为网线接触不良。因此写一个程序监测网络的硬件状态。
程序的主要原理是参考ifconfig命令。当网线连接时执行ifconfig命令会打印“RUNING”字符串。当网线断开时则不会打印。参考ifconfig的源码实现了如下代码。
代码会将网络状态写入当前文件夹下的net.txt文件中。
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <stdlib.h>
int net_detect(char* net_name)
{
int skfd = 0;
struct ifreq ifr;
time_t timep;
time (&timep);
char* cur_time = asctime(gmtime(&timep));
cur_time[strlen(cur_time) - 1] = 0; //去掉尾部的换行符
skfd = socket(AF_INET, SOCK_DGRAM, 0);
if(skfd < 0)
{
printf("%s ",cur_time);
perror("Open socket error!");
return -1;
}
strcpy(ifr.ifr_name, net_name);
if(ioctl(skfd, SIOCGIFFLAGS, &ifr) <0 )
{
perror("IOCTL error!");
printf("Maybe ethernet inferface %s is not valid!\n", ifr.ifr_name);
close(skfd);
return -1;
}
FILE *fp = NULL;
fp = fopen("./net.txt","a");
if (!fp)
{
perror("file open failed!");
return -2;
}
//判断是否存在IFF_RUNNING标志
if(ifr.ifr_flags & IFF_RUNNING)
{
fprintf(fp,"%s :: %s is running ^_^\n", cur_time, ifr.ifr_name);
}
else
{
fprintf(fp,"%s :: %s is not running ---------\n", cur_time, ifr.ifr_name);
printf("%s :: %s is not running ---------\n", cur_time, ifr.ifr_name);
}
close(skfd);
fclose(fp);
return 0;
}
void handle_sigINT(int num)
{
printf("signal: kill %d (Ctrl + C). Stop running! \n ",num);
exit(1);
}
void msleep(long t)
{
usleep(t*1000);
}
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("Usage: ./NetMonitor netName [...time(ms)]\n");
return -1;
}
int time = 1000;
if (argc == 3)
time = atoi(argv[2]);
//重定向输出流时,程序结束才会写入,需要捕获Ctrl + C信号
// signal(SIGINT,handle_sigINT);
while(1)
{
net_detect(argv[1]);
msleep(time);
}
return 0;
}
LINUX 检测网线热插拔事件
来自:https://blog.csdn.net/al86866365/article/details/79066227
1.cat /sys/class/net/eth0/carrier
如果carrier为1表示connect,否则disconnect
2.Netlink实现网卡上下线监控
#include <sys/types.h>
#include <sys/socket.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <string.h>
#define BUFLEN 20480
int main(int argc, char *argv[])
{
int fd, retval;
char buf[BUFLEN] = {0};
int len = BUFLEN;
struct sockaddr_nl addr;
struct nlmsghdr *nh;
struct ifinfomsg *ifinfo;
struct rtattr *attr;
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len));
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_groups = RTNLGRP_LINK;
bind(fd, (struct sockaddr*)&addr, sizeof(addr));
while ((retval = read(fd, buf, BUFLEN)) > 0)
{
for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, retval); nh = NLMSG_NEXT(nh, retval))
{
if (nh->nlmsg_type == NLMSG_DONE)
break;
else if (nh->nlmsg_type == NLMSG_ERROR)
return -1;
else if (nh->nlmsg_type != RTM_NEWLINK)
continue;
ifinfo = NLMSG_DATA(nh);
printf("%u: %s", ifinfo->ifi_index,
(ifinfo->ifi_flags & IFF_LOWER_UP) ? "up" : "down" );
attr = (struct rtattr*)(((char*)nh) + NLMSG_SPACE(sizeof(*ifinfo)));
len = nh->nlmsg_len - NLMSG_SPACE(sizeof(*ifinfo));
for (; RTA_OK(attr, len); attr = RTA_NEXT(attr, len))
{
if (attr->rta_type == IFLA_IFNAME)
{
printf(" %s", (char*)RTA_DATA(attr));
break;
}
}
printf("\n");
}
}
return 0;
}