码迷,mamicode.com
首页 > 系统相关 > 详细

linux c 获取网卡状态(UP or DOWN)

时间:2015-07-18 18:40:52      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:

源码如下:

#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("%s\n",net_detect("eth0"));
        return 0;
}

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

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

linux c 获取网卡状态(UP or DOWN)

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!