当前位置:首页 > 操作系统 > Linux

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!n", __FILE__, __LINE__);
                return NULL;
        }

        strcpy(ifr.ifr_name, net_name);

        if(ioctl(skfd, SIOCGIFFLAGS, &ifr) <0 ) {
                printf("%s:%d IOCTL error!n", __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("%sn",net_detect("eth0"));
        return0;
}

        

总结:
该程序是测试 ifconfig 命令中 指定网卡 是有用 RUNNING 。可以配合 ifconfig eth0 up 和 ifconfig eth0 down 测试。

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文:http://blog.csdn.net/u011641885/article/details/46943941


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐