shell 判断网线插拔_Linux下检测网卡与网线连通状态

在项目中和网络连接的模块中,要不停的去检查网络是否正常,尝试了很多的方法,最后终于找到了一个比较靠谱的方法,现在分享给大家。

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

/**********************************************************************

*函数名称:get_netlink_status

*功能描述:检测网络链接是否断开*输入参数:const char *(要检测的网口,如:eth0、eth1)*输出参数:无

*返回值:正常链接0,断开返回-1***********************************************************************/

int get_netlink_status(const char *if_name)

{

struct ifreq ifr;

int skfd = socket(AF_INET, SOCK_DGRAM, 0);

strcpy(ifr.ifr_name, if_name);

if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)

{

return -1;

}

if(ifr.ifr_flags & IFF_RUNNING)

return 0; // 网卡已插上网线

else return -1;

}

int main()

{

if(get_netlink_status("eth0") == 0)

printf("detect ok./n");

else

printf("detect fail./n");

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-11-14 16:32

浏览 1065

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值