Linux下检测网卡与网线的连接状态


在机顶盒的网络连接中,其中有一项就是检测网卡与网线是否连接,当没有连接网线时,通过浏览器打开错误页面提示网线未连接。那么如何检测网卡与网线是否连接。

一、使用ioctl向socket发送SIOCETHTOOL命令字

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

方法二: 使用ifconfigl命令能很方便的查看网卡与网线是否连通:

# 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不在了。

    linux系统提供了popen/pclose进程管道让C和shell很方便的交互,下面C代码结合shell命令检测网卡与网线连通状况:

  1. #include <unistd.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4. #include <string.h>  
  5.   
  6. int GetNetStat( )  
  7. {  
  8.     char    buffer[BUFSIZ];  
  9.     FILE    *read_fp;  
  10.     int        chars_read;  
  11.     int        ret;  
  12.      
  13.     memset( buffer, 0, BUFSIZ );  
  14.     read_fp = popen("ifconfig eth0 | grep RUNNING""r");  
  15.     if ( read_fp != NULL )  
  16.     {  
  17.         chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);  
  18.         if (chars_read > 0)  
  19.         {  
  20.             ret = 1;  
  21.         }  
  22.         else  
  23.         {  
  24.             ret = -1;  
  25.         }  
  26.         pclose(read_fp);  
  27.     }  
  28.     else  
  29.     {  
  30.         ret = -1;  
  31.     }  
  32.   
  33.     return ret;  
  34. }  
  35.   
  36.   
  37. int main()  
  38. {  
  39.     int i=0;  
  40.     i = GetNetStat();  
  41.     printf( "\nNetStat = %d\n", i );  
  42.     return 0;  
  43. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值