zoukankan      html  css  js  c++  java
  • linux c 获取网卡状态(UP or DOWN)

    源代码例如以下:

    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <linux/if.h>
    #include <string.h>
    #include <stdio.h>
    
    
    char *net_detect(char* net_name)
    {
            int skfd = 0;
            struct ifreq ifr;
    
            skfd = socket(AF_INET, SOCK_DGRAM, 0);
            if(skfd < 0) {
                    printf("%s:%d Open socket error!
    ", __FILE__, __LINE__);
                    return NULL;
            }
    
            strcpy(ifr.ifr_name, net_name);
    
            if(ioctl(skfd, SIOCGIFFLAGS, &ifr) <0 ) {
                    printf("%s:%d IOCTL error!
    ", __FILE__, __LINE__);
                    printf("Maybe ethernet inferface %s is not valid!", ifr.ifr_name);
                    close(skfd);
                    return NULL;
            }
    
            if(ifr.ifr_flags & IFF_RUNNING) {
                    return "UP";
            } else {
                    return "DOWN";
            }
    
    }
    int main()
    {
            printf("%s
    ",net_detect("eth0"));
            return 0;
    }
    

    总结:
    该程序是測试 ifconfig 命令中 指定网卡 是实用 RUNNING 。

    能够配合 ifconfig eth0 up 和 ifconfig eth0 down 測试。

  • 相关阅读:
    【USACO18JAN】MooTube G
    【JSOI2008】星球大战
    【ECF#87】小结
    【NOIP2013】火柴排队
    【USACO04OPEN】MooFest G
    【NOI OL #2】T3 游戏
    【NOI OL #2】T2 子序列问题
    简单NLT
    python中的位运算
    列表和元组
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7262803.html
Copyright © 2011-2022 走看看