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

来源:互联网 发布:java push 编辑:程序博客网 时间:2024/05/21 09:46
Linux下检测网卡与网线连接状态:

方法一:使用ioctl向socket发送SIOCETHTOOL命令字

[cpp] view plaincopyprint?
  1. #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. }  


方法二: 使用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命令检测网卡与网线连通状况:

[cpp] view plaincopyprint?
  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. }