linux查看网卡是否连接

编程部分转自http://topic.csdn.net/u/20100920/16/7D58E76F-39FE-44B3-B4AC-D65D5AE57D2D.html

以前知道通过ethtool可以查看网卡是否已连接。编程实现如下:#include <sys/types.h> #include <string.h> #include <stdlib.h> #include <sys/ioctl.h> #include <stdio.h> #include <errno.h> #include <net/if.h> struct ethtool_value { __uint32_t cmd; __uint32_t data; }; int main(int argc, char* argv[]) { struct ethtool_value edata; int fd = -1, err = 0; struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); strcpy(ifr.ifr_name, argv[1]); fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { perror("Cannot get control socket"); return 70; } edata.cmd = 0x0000000a; ifr.ifr_data = (caddr_t)&edata; err = ioctl(fd, 0x8946, &ifr); if (err == 0) { fprintf(stdout, "Link detected: %s/n", edata.data ? "yes":"no"); } else if (errno != EOPNOTSUPP){ perror("Cannot get link status"); } return 0; } 

以上代码在Linux MagicLinux 2.6.18-92.el5 #1 SMP Tue Apr 29 13:16:12 EDT 2008 i686 i686 i386 GNU/Linux环境编译通过

 

你可能感兴趣的:(linux查看网卡是否连接)