Linux下如何用C编程实现检测网卡与网线的连接状态
来源:互联网 发布:手机迅雷网络异常 编辑:程序博客网 时间:2024/05/21 10:34
http://www.caojunfei.com/?p=614
- int get_netportstatus(const char *interface) {
- char cmd[1024];
- char *tt;
- FILE *fp;
- int devflag;
- devflag=get_netflag(interface);
- if (devflag==DEV_DOWN) {
- sprintf(cmd,"ifconfig %s up",interface);
- system(cmd);
- }
- sprintf(cmd,"ethtool %s | grep \"Link detected\" > /tmp/eth.temp",interface);
- system(cmd);
- if (devflag==DEV_DOWN) {
- sprintf(cmd,"ifconfig %s down",interface);
- system(cmd);
- }
- fp=fopen("/tmp/eth.temp","r");
- if (fp==NULL) {
- system("rm -rf /tmp/eth.temp");
- return -1;
- }
- fgets(cmd,1024,fp);
- fclose(fp);
- system("rm -rf /tmp/eth.temp");
- tt=strstr(cmd,"no");
- if (tt!=NULL) return LINK_DOWN;
- tt=strstr(cmd,"yes");
- if (tt!=NULL) return LINK_UP;
- return -1;
- }
- int get_netportstatus(const char *interface) {
- char cmd[1024];
- char *tt;
- FILE *fp;
- int devflag;
- devflag=get_netflag(interface);
- if (devflag==DEV_DOWN) {
- sprintf(cmd,"ifconfig %s up",interface);
- system(cmd);
- }
- sprintf(cmd,"ethtool %s | grep \"Link detected\" > /tmp/eth.temp",interface);
- system(cmd);
- if (devflag==DEV_DOWN) {
- sprintf(cmd,"ifconfig %s down",interface);
- system(cmd);
- }
- fp=fopen("/tmp/eth.temp","r");
- if (fp==NULL) {
- system("rm -rf /tmp/eth.temp");
- return -1;
- }
- fgets(cmd,1024,fp);
- fclose(fp);
- system("rm -rf /tmp/eth.temp");
- tt=strstr(cmd,"no");
- if (tt!=NULL) return LINK_DOWN;
- tt=strstr(cmd,"yes");
- if (tt!=NULL) return LINK_UP;
- return -1;
- }
用c/c++实现Linux下检测网络接口状态
- #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 , char* [])
- {
- struct ethtool_value edata;
- int fd = -1, err = 0;
- struct ifreq ifr;
- memset(&ifr, 0, sizeof(ifr));
- strcpy(ifr.ifr_name, "eth0");
- 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;
- }
- #include <net if.h=""> // IFF_RUNNING
- //如果网卡已脸上网线,返回0,否则返回-1.
- int check_nic(char *nic)
- {
- struct ifreq ifr;
- int skfd = socket(AF_INET, SOCK_DGRAM, 0);
- strcpy(ifr.ifr_name, nic_name);
- if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
- {
- return -1;
- }
- if(ifr.ifr_flags & IFF_RUNNING)
- return 0; // 网卡已插上网线
- else return -1;
- }
转载:http://blog.csdn.net/lwpping/article/details/7440817
阅读全文
0 0
- Linux下如何用C编程实现检测网卡与网线的连接状态
- Linux下如何用C编程实现检测网卡与网线的连接状态
- Linux下检测网卡与网线的连接状态
- Linux下检测网卡与网线的连接状态
- Linux下检测网卡与网线的连接状态
- Linux下检测网卡与网线连接状态
- Linux下检测网卡与网线连接状态
- Linux 检测网卡与网线连接状态
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- Linux下检测网卡与网线连接状态,使用ioctl向socket发送SIOCETHTOOL命令字
- linux下检测网卡与网线连通状态
- Linux下检测网卡与网线连通状态
- Linux下检测网卡与网线连通状态
- Linux下检测网卡与网线连通状态
- 非负矩阵分解(NMF)论文笔记(3)
- 修改Gitlab端口 ( Bitnami 方式安装下)
- 建议linux下使用stat()获取文件大小
- 正则表达式相关:C# 抓取网页类(获取网页中所有信息)
- 浮点数取模
- Linux下如何用C编程实现检测网卡与网线的连接状态
- SVN服务器搭建和使用(三)
- 机器学习小试(1)TensorFlow的第一个程序
- jquery js checkbox勾选/取消勾选的诡异问题
- ==和===比较
- DEVC++下编译helib
- 告知服务器意图的 HTTP 方法
- ionic配置自定义keystore签名文件并直接安装已签名apk到手机
- Codeforces Round #419 (Div. 2) B. Karen and Coffee (差分标记)