linux 监测网线插拔状态

https://blog.csdn.net/chexlong/article/details/102977406

原文链接:https://blog.csdn.net/baidu_33850454/article/details/82631948

简介
在数据传输过程中出现网络偶然间断开的情况,考虑是否为网线接触不良。因此写一个程序监测网络的硬件状态。

程序的主要原理是参考ifconfig命令。当网线连接时执行ifconfig命令会打印“RUNING”字符串。当网线断开时则不会打印。参考ifconfig的源码实现了如下代码。

代码会将网络状态写入当前文件夹下的net.txt文件中。

 
  1. #include <sys/socket.h>

  2. #include <sys/ioctl.h>

  3. #include <linux/if.h>

  4. #include <string.h>

  5. #include <stdio.h>

  6. #include <unistd.h>

  7. #include <time.h>

  8. #include <signal.h>

  9. #include <stdlib.h>

  10.  
  11. int net_detect(char* net_name)

  12. {

  13. int skfd = 0;

  14. struct ifreq ifr;

  15. time_t timep;

  16. time (&timep);

  17. char* cur_time = asctime(gmtime(&timep));

  18. cur_time[strlen(cur_time) - 1] = 0; //去掉尾部的换行符

  19.  
  20. skfd = socket(AF_INET, SOCK_DGRAM, 0);

  21. if(skfd < 0)

  22. {

  23. printf("%s ",cur_time);

  24. perror("Open socket error!");

  25. return -1;

  26. }

  27.  
  28. strcpy(ifr.ifr_name, net_name);

  29.  
  30. if(ioctl(skfd, SIOCGIFFLAGS, &ifr) <0 )

  31. {

  32. perror("IOCTL error!");

  33. printf("Maybe ethernet inferface %s is not valid!\n", ifr.ifr_name);

  34. close(skfd);

  35. return -1;

  36. }

  37.  
  38. FILE *fp = NULL;

  39. fp = fopen("./net.txt","a");

  40. if (!fp)

  41. {

  42. perror("file open failed!");

  43. return -2;

  44. }

  45. //判断是否存在IFF_RUNNING标志

  46. if(ifr.ifr_flags & IFF_RUNNING)

  47. {

  48. fprintf(fp,"%s :: %s is running ^_^\n", cur_time, ifr.ifr_name);

  49. }

  50. else

  51. {

  52. fprintf(fp,"%s :: %s is not running ---------\n", cur_time, ifr.ifr_name);

  53. printf("%s :: %s is not running ---------\n", cur_time, ifr.ifr_name);

  54. }

  55.  
  56. close(skfd);

  57. fclose(fp);

  58.  
  59. return 0;

  60. }

  61.  
  62. void handle_sigINT(int num)

  63. {

  64. printf("signal: kill %d (Ctrl + C). Stop running! \n ",num);

  65. exit(1);

  66. }

  67.  
  68. void msleep(long t)

  69. {

  70. usleep(t*1000);

  71. }

  72.  
  73. int main(int argc, char** argv)

  74. {

  75. if (argc < 2)

  76. {

  77. printf("Usage: ./NetMonitor netName [...time(ms)]\n");

  78. return -1;

  79. }

  80.  
  81. int time = 1000;

  82. if (argc == 3)

  83. time = atoi(argv[2]);

  84. //重定向输出流时,程序结束才会写入,需要捕获Ctrl + C信号

  85. // signal(SIGINT,handle_sigINT);

  86.  
  87. while(1)

  88. {

  89. net_detect(argv[1]);

  90. msleep(time);

  91. }

  92. return 0;

  93. }

 

 

LINUX 检测网线热插拔事件

来自:https://blog.csdn.net/al86866365/article/details/79066227

1.cat /sys/class/net/eth0/carrier 

如果carrier为1表示connect,否则disconnect

2.Netlink实现网卡上下线监控

 
  1. #include <sys/types.h>

  2. #include <sys/socket.h>

  3. #include <asm/types.h>

  4. #include <linux/netlink.h>

  5. #include <linux/rtnetlink.h>

  6. #include <unistd.h>

  7. #include <stdlib.h>

  8. #include <stdio.h>

  9. #include <sys/ioctl.h>

  10. #include <linux/if.h>

  11. #include <string.h>

  12.  
  13. #define BUFLEN 20480

  14.  
  15. int main(int argc, char *argv[])

  16. {

  17. int fd, retval;

  18. char buf[BUFLEN] = {0};

  19. int len = BUFLEN;

  20. struct sockaddr_nl addr;

  21. struct nlmsghdr *nh;

  22. struct ifinfomsg *ifinfo;

  23. struct rtattr *attr;

  24.  
  25. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);

  26. setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len));

  27. memset(&addr, 0, sizeof(addr));

  28. addr.nl_family = AF_NETLINK;

  29. addr.nl_groups = RTNLGRP_LINK;

  30. bind(fd, (struct sockaddr*)&addr, sizeof(addr));

  31. while ((retval = read(fd, buf, BUFLEN)) > 0)

  32. {

  33. for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, retval); nh = NLMSG_NEXT(nh, retval))

  34. {

  35. if (nh->nlmsg_type == NLMSG_DONE)

  36. break;

  37. else if (nh->nlmsg_type == NLMSG_ERROR)

  38. return -1;

  39. else if (nh->nlmsg_type != RTM_NEWLINK)

  40. continue;

  41. ifinfo = NLMSG_DATA(nh);

  42. printf("%u: %s", ifinfo->ifi_index,

  43. (ifinfo->ifi_flags & IFF_LOWER_UP) ? "up" : "down" );

  44. attr = (struct rtattr*)(((char*)nh) + NLMSG_SPACE(sizeof(*ifinfo)));

  45. len = nh->nlmsg_len - NLMSG_SPACE(sizeof(*ifinfo));

  46. for (; RTA_OK(attr, len); attr = RTA_NEXT(attr, len))

  47. {

  48. if (attr->rta_type == IFLA_IFNAME)

  49. {

  50. printf(" %s", (char*)RTA_DATA(attr));

  51. break;

  52. }

  53. }

  54. printf("\n");

  55. }

  56. }

  57.  
  58. return 0;

  59. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值