http://blog.chinaunix.net/space.php?uid=20357359&do=blog&cuid=1798479

Linux下检测网卡与网线连接状态,使用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(const char *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(const char *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;
}

-----------------------------------------------------------

# 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不在了。
我的目的是用C语言来实现程序,而linux系统提供了popen/pclose进程管道让C和shell很方便的交互,不过使用的时候要注意设置权限,以免造成安全隐患。废话不多说,看下面C代码结合shell命令检测网卡与网线连通状况:
netstat.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**********************************************************************
* 函数名称: GetNetStat
* 功能描述: 检测网络链接是否断开
* 输入参数:
* 输出参数: 无
* 返 回 值: 正常链接1,断开返回-1
* 其它说明: 本程序需要超级用户权限才能成功调用ifconfig命令
* 修改日期 版本号 修改人 修改内容
* ---------------------------------------------------------------------
* 2010/04/02 V1.0 eden_mgqw
***********************************************************************/
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下检测网卡与网线连接状态的更多相关文章

  1. Linux下双网卡绑定(bonding技术)

    Linux网卡绑定探析   2013-08-20 15:39:31 现在很多服务器都自带双千兆网口,利用网卡绑定既能增加网络带宽,同时又能做相应的冗余,目前应用于很多的场景.linux操作系统下自带的 ...

  2. Linux下设置网卡静态ip

    Linux下设置网卡静态ip 如果是服务器版,没有图形界面只用用命令行修改配置文件 如果是客户端版本,可以用图形界面 配置的前提是要在root用户下才能重启网卡服务 图形界面: system-conf ...

  3. Linux下查看网卡驱动和版本信息

    Linux下查看网卡驱动和版本信息 查看网卡生产厂商和信号 查看基本信息:lspci 查看详细信息:lspci -vvv # 3个小写的v 查看网卡信息:lspci | grep Ethernet 查 ...

  4. Linux下多网卡同网段多IP网络分流设定方法

    Linux下多网卡同网段多IP网络分流设定方法 -- :: 标签:Linux下多网卡同网段多IP网络分流设定方法 当服务器需要较高的网络流量时,在其它资源不造成瓶颈的情况下无疑会用到多网卡. 第1选项 ...

  5. Linux下修改网卡IP、DNS和网关

    Linux下修改网卡IP和网关 建议通过终端字符方式下来修改 一.修改IP地址 vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOO ...

  6. Linux 下修改网卡MAC地址

    Linux下修改网卡MAC地址 by:授客 QQ:1033553122 例子:修改网卡接口eth0的mac地址 #停用网卡接口,比如eth0 # ifconfig eth0 down #编辑对应的网卡 ...

  7. Linux 下修改网卡接口名

    Linux下修改网卡接口名 by:授客 QQ:1033553122 (测试环境:CentOS-6.0-x86_64-bin-DVD1.iso+Vmware) 作用 可以用于解决类似如下Device n ...

  8. linux下一个网卡配置多个ip【虚拟ip】

    Linux下配置网卡ip别名何谓ip别名?用windows的话说,就是为一个网卡配置多个ip.什么场合增加ip别名能派上用场?布网需要.多ip访问测试.特定软件对多ip的需要...and so on. ...

  9. linux 下查看网卡工作速率

    [root@hadoop058 ~]# mii-tool eth0: negotiated 100baseTx-FD, link ok 100M linux 下查看网卡工作速率 Ethtool是用于查 ...

随机推荐

  1. Machine Learning Books Suggested by Michael I. Jordan from Berkeley

    http://www.statsblogs.com/2014/12/30/machine-learning-books-suggested-by-michael-i-jordan-from-berke ...

  2. kernel dump Analysis

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/0c418482-7edd-4c91-b7f4-6005d550244a/got-the- ...

  3. Nginx 权限问题

    At my job we are moving to Nginx for the load balancing of our sites. Nginx is a very powerful load ...

  4. VS注释与取消注释快捷键

    最近在使用VS2010开发ASP.Net,突然发现想全部注释时找不到注释的快捷键,网上查了下,原来很简单,只是需要使用组合键. 注释:        先CTRL+K,然后CTRL+C 取消注释: 先C ...

  5. iOS应用管理(优化)

    // //  ViewController.m //  01-应用管理 //  Created by apple on 17-5-14. //  Copyright (c) 2017年  All ri ...

  6. 【流媒体】UPnP的工作过程

    UPnP简介 通用即插即用(英语:Universal Plug and Play,简称UPnP)是由“通用即插即用论坛”(UPnP™ Forum)推广的一套网络协议. 该协议的目标是使家庭网络(数据共 ...

  7. Asp.Net模拟post提交数据方法

    方法1: System.Net.WebClient WebClientObj = new System.Net.WebClient(); System.Collections.Specialized. ...

  8. Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明

    https://www.sojson.com/blog/246.html ******************************************** Jackson  是  Spring ...

  9. Django admin 常用方法 model 增加只读权限

    1.Django admin model 设置查看权限 Django model 默认只有增加.删除.修改权限.没有查看权限 #model class Ad_Campaing(models.Model ...

  10. 解决hibernate向mysql插入中文乱码问题(转)

    转载自:http://blog.csdn.net/peditable/article/details/7047573 1.首先需要修改MySQL数据库的配置文件my.ini,此文件放在mysql根目录 ...