您的位置:首页 > 运维架构 > Linux

linux下检测网卡与网线连通状态

2013-03-18 09:57 525 查看
1、用C代码实现检测网卡是否插上网线

#include <net/if.h> // IFF_RUNNING

#include <sys/ioctl.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <net/if_arp.h>

int main()

{

if(check_nic("eth0") == 0)

printf("detect ok./n");

else

printf("detect fail./n");

}

//如果网卡已连上网线,返回0,否则返回-1.

int check_nic(char *nic_name)

{

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;

}

}

2、Linux_stat.c

view plaincopy to clipboardprint?

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

}

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

}

3、在linux下使用ifconfig命令能很方便的查看网卡与网线是否连通,运行ifconfig eth0命令大致输出如下:

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

}

下面是编译运行程序的输出结果(正常返回1,断开返回-1):

# cc netstat.c

# ./a.out

NetStat = 1

4、Linux命令检测网卡是否连上网线

root# ethtool eth0

5、(只能针对100M网卡):

/* Check network adapter is up or down */

// Note: one or two adapter drivers may be not support this method

// exemple: based virtual machine

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <sys/socket.h>

#include <sys/ioctl.h>

#include <net/if.h>

#include <linux/sockios.h>

#include <linux/types.h>

const char* VERSION = "0.6.0.48 --maintain by isnowran";

struct mii_data

{

__u16 phy_id;

__u16 reg_num;

__u16 val_in;

__u16 val_out;

};

int main( int argc, char* argv[] )

{

if( argc != 2 )

{

printf( "Version: %s\n", VERSION );

printf( "Useage: argv[0] ethNO.\n" );

return 0;

}

int skfd = 0;

if( ( skfd = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 )

{

perror( "socket" );

return -1;

}

struct ifreq ifr;

bzero( &ifr, sizeof( ifr ) );

strncpy( ifr.ifr_name, argv[1], IFNAMSIZ - 1 );

ifr.ifr_name[IFNAMSIZ - 1] = 0;

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

{

perror( "ioctl" );

return -1;

}

struct mii_data* mii = NULL;

mii = (struct mii_data*)&ifr.ifr_data;

mii->reg_num = 0x01;

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

{

perror( "ioctl2" );

return -1;

}

if( mii->val_out & 0x0004 )

printf( "Linkup\n" );

else

printf( "Linkdown\n" );

close( skfd );

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: