Linux下检测网卡与网线连通状态

来源:互联网 发布:后台数据怎么传到前台 编辑:程序博客网 时间:2024/05/22 10:44

Linux下检测网卡与网线连通状态

分类: Linux 1123人阅读 评论(0)收藏 举报
linuxinterfacebuffershellfpstruct

Linux_stat.c

[cpp] view plaincopyprint?
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <string.h>   
  4. #include <fcntl.h>   
  5. #include <errno.h>   
  6. #include <sys/ioctl.h>   
  7. #include <sys/types.h>   
  8. #include <sys/socket.h>   
  9. #include <linux/if.h>   
  10. #include <linux/sockios.h>   
  11. #include <linux/ethtool.h>  
  12. int get_netlink_status(const char *if_name);  
  13. int main()  
  14. {  
  15.     if(getuid() != 0)  
  16.     {  
  17.         fprintf(stderr, "Netlink Status Check Need Root Power./n");  
  18.         return 1;  
  19.     }  
  20.       
  21.     printf("Net link status: %d/n", get_netlink_status("eth0"));  
  22.     return 0;  
  23. }  
  24. // if_name like "ath0", "eth0". Notice: call this function  
  25. // need root privilege.   
  26. // return value:   
  27. // -1 -- error , details can check errno  
  28. // 1 -- interface link up   
  29. // 0 -- interface link down.   
  30. int get_netlink_status(const char *if_name)  
  31. {  
  32.     int skfd;  
  33.     struct ifreq ifr;  
  34.     struct ethtool_value edata;  
  35.     edata.cmd = ETHTOOL_GLINK;  
  36.     edata.data = 0;  
  37.     memset(&ifr, 0, sizeof(ifr));  
  38.     strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1);  
  39.     ifr.ifr_data = (char *) &edata;  
  40.     if (( skfd = socket( AF_INET, SOCK_DGRAM, 0 )) < 0)  
  41.         return -1;  
  42.     if(ioctl( skfd, SIOCETHTOOL, &ifr ) == -1)  
  43.     {  
  44.         close(skfd);  
  45.         return -1;  
  46.     }  
  47.     close(skfd);  
  48.     return edata.data;  
  49. }  

 

在linux下使用ifconfigl命令能很方便的查看网卡与网线是否连通,运行ifconfig eth0命令大致输出如下:

# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:25:35:68:CC:D6 
          inet addr:192.168.1.168  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::215:c5ff:fe18:ccd6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:130722 errors:0 dropped:0 overruns:0 frame:0
          TX packets:112560 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:104371099 (99.5 MiB)  TX bytes:20518584 (19.5 MiB)
          Interrupt:16

其中的RUNNING就表示网卡与网线正常链接,拔掉网线再运行此命令就会发现RUNNING不在了。

    我的目的是用C语言来实现程序,而linux系统提供了popen/pclose进程管道让C和shell很方便的交互,不过使用的时候要注意设置权限,以免造成安全隐患。废话不多说,看下面C代码结合shell命令检测网卡与网线连通状况:
netstat.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/**********************************************************************
* 函数名称: GetNetStat
* 功能描述: 检测网络链接是否断开
* 输入参数:
* 输出参数: 无
* 返 回 值: 正常链接1,断开返回-1
* 其它说明: 本程序需要超级用户权限才能成功调用ifconfig命令
* 修改日期        版本号     修改人          修改内容
* ---------------------------------------------------------------------
* 2010/04/02      V1.0      eden_mgqw
***********************************************************************/
int GetNetStat( )
{
    char    buffer[BUFSIZ];
    FILE    *read_fp;
    int        chars_read;
    int        ret;
   
    memset( buffer, 0, BUFSIZ );
    read_fp = popen("ifconfig eth0 | grep RUNNING", "r");
    if ( read_fp != NULL )
    {
        chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
        if (chars_read > 0)
        {
            ret = 1;
        }
        else
        {
            ret = -1;
        }
        pclose(read_fp);
    }
    else
    {
        ret = -1;
    }

    return ret;
}


int main()
{
    int i=0;
    i = GetNetStat();
    printf( "/nNetStat = %d/n", i );
    return 0;
}


下面是编译运行程序的输出结果(正常返回1,断开返回-1):
# cc netstat.c
# ./a.out
NetStat = 1

在此特别感谢linuxeden的shell区版主li-jiahuan帅锅,为本文提供了shell支持-_-!

原创粉丝点击