Linux下检测网卡与网线连接状态
来源:互联网 发布:java push 编辑:程序博客网 时间:2024/05/21 09:46
方法一:
使用ioctl向socket发送
SIOCETHTOOL
命令字
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <linux/if.h>
- #include <linux/sockios.h>
- #include <linux/ethtool.h>
- int get_netlink_status(constchar *if_name);
- int main()
- {
- if(getuid()!= 0)
- {
- fprintf(stderr,"Netlink Status Check Need Root Power.\n");
- return 1;
- }
- printf("Net link status: %d\n", get_netlink_status("eth0"));
- return 0;
- }
- // if_name like "ath0", "eth0". Notice: call this function
- // need root privilege.
- // return value:
- // -1 -- error , details can check errno
- // 1 -- interface link up
- // 0 -- interface link down.
- int get_netlink_status(constchar *if_name)
- {
- int skfd;
- struct ifreq ifr;
- struct ethtool_value edata;
- edata.cmd = ETHTOOL_GLINK;
- edata.data = 0;
- memset(&ifr, 0,sizeof(ifr));
- strncpy(ifr.ifr_name, if_name,sizeof(ifr.ifr_name)- 1);
- ifr.ifr_data =(char *) &edata;
- if (( skfd= socket(AF_INET, SOCK_DGRAM, 0 )) < 0)
- return -1;
- if(ioctl( skfd, SIOCETHTOOL,&ifr ) == -1)
- {
- close(skfd);
- return -1;
- }
- close(skfd);
- return edata.data;
- }
#include<stdio.h>#include<stdlib.h>#include<string.h>#include <fcntl.h>#include <errno.h>#include <sys/ioctl.h>#include <sys/types.h>#include <sys/socket.h>#include <linux/if.h>#include <linux/sockios.h>#include <linux/ethtool.h>int get_netlink_status(constchar *if_name);int main(){ if(getuid()!= 0) { fprintf(stderr,"Netlink Status Check Need Root Power.\n"); return 1; } printf("Net link status: %d\n", get_netlink_status("eth0")); return 0;}// if_name like "ath0", "eth0". Notice: call this function// need root privilege.// return value:// -1 -- error , details can check errno// 1 -- interface link up// 0 -- interface link down.int get_netlink_status(constchar *if_name){ int skfd; struct ifreq ifr; struct ethtool_value edata; edata.cmd = ETHTOOL_GLINK; edata.data = 0; memset(&ifr, 0,sizeof(ifr)); strncpy(ifr.ifr_name, if_name,sizeof(ifr.ifr_name)- 1); ifr.ifr_data =(char *) &edata; if (( skfd= socket(AF_INET, SOCK_DGRAM, 0 )) < 0) return -1; if(ioctl( skfd, SIOCETHTOOL,&ifr ) == -1) { close(skfd); return -1; } close(skfd); return edata.data;}
方法二: 使用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命令检测网卡与网线连通状况:
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- 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;
- }
- Linux下检测网卡与网线连接状态
- Linux下检测网卡与网线连接状态
- Linux下检测网卡与网线的连接状态
- Linux下检测网卡与网线的连接状态
- Linux下检测网卡与网线的连接状态
- Linux 检测网卡与网线连接状态
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下如何用C编程实现检测网卡与网线的连接状态
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下如何用C编程实现检测网卡与网线的连接状态
- linux下检测网卡与网线连通状态
- Linux下检测网卡与网线连通状态
- Linux下检测网卡与网线连通状态
- Linux下检测网卡与网线连通状态
- MapReduce:在大规模集群上的数据处理简化(下)
- POJ 1860 Currency Exchange
- Ns2+Cygwin Terminal在windows系统安装
- java经典题目_完数
- c++ primer 学习摘抄之四(第六章 抽象容器类型)
- Linux下检测网卡与网线连接状态
- 一元稀疏多项式(加减法)
- DirectX小段代码总结(一)
- VC中利用多线程实现线程之间的通信
- 集合(索引表,嵌套表,变长数组)
- STM32单片机的IIC硬件编程---查询等待方式
- 注册表操作(VC_Win32)
- 八、线程的同步(一)
- ASIHTTPRequest系列(1):同步和异步请求