zoukankan      html  css  js  c++  java
  • 如何判断网线是否连接

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

    NTP-SERVER:/#mii-tool
    eth0: negotiated 100baseTx-FD, link ok
    eth1: no link
    eth2: no link
    eth3: no link

    [NTP-Fedora20 ~]#mii-tool p32p1
    p32p1: negotiated 100baseTx-FD, link ok
    [NTP-Fedora20 ~]#ethtool p32p1
    Settings for p32p1:
    Supported ports: [ TP ]
    Supported link modes: 10baseT/Half 10baseT/Full
    100baseT/Half 100baseT/Full
    1000baseT/Full
    Supported pause frame use: No
    Supports auto-negotiation: Yes
    Advertised link modes: 10baseT/Half 10baseT/Full
    100baseT/Half 100baseT/Full
    1000baseT/Full
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Speed: 100Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 1
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: on (auto)
    Supports Wake-on: pumbg
    Wake-on: g
    Current message level: 0x00000007 (7)
    drv probe link
    Link detected: yes
    [NTP-Fedora20 ~]#

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

    [cpp] view plain copy
     
    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. ");  
    22.         return 1;  
    23.     }  
    24.       
    25.     printf("Net link status: %d ", 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 plain copy
     
      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( " NetStat = %d ", i );  
      42.     return 0;  
      43. }  
  • 相关阅读:
    C++——多态
    C++——继承
    PAT1002 A+B for Polynomials
    PAT1001-A+B Format
    C++——运算符重载(下)
    图像处理与Python实现(岳亚伟)笔记二——彩色图像处理
    图像处理与Python实现(岳亚伟)笔记一
    827. Making A Large Island
    995. Minimum Number of K Consecutive Bit Flips (2021/2/18每日一题)
    685. Redundant Connection II (LeetCode 刷题笔记)
  • 原文地址:https://www.cnblogs.com/zhouhbing/p/5379258.html
Copyright © 2011-2022 走看看