linux 監測網線插拔狀態

簡介
在數據傳輸過程中出現網絡偶然間斷開的情況,考慮是否爲網線接觸不良。因此寫一個程序監測網絡的硬件狀態。

程序的主要原理是參考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;  
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章