Linux下檢測網卡與網線串連狀態

來源:互聯網
上載者:User

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

Linux下檢測網卡與網線串連狀態,使用ioctl向socket發送SIOCETHTOOL命令字。
link_stat.c

#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;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.