C语言linux网口状态检测

1.C语言+shell实现linux网卡状态检测

直接上代码 要求linux环境具备grep和awk(awk可选)

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
int get_if_status(char *if_name)
{
  char  buffer[BUFSIZ];
 char  cmd[100];
  FILE  *read_fp;
  int    chars_read;
  int    ret =0;
  
  memset( buffer, 0, BUFSIZ );
  memset( cmd, 0, 100 );
 sprintf(cmd, "ifconfig -a | grep %s",if_name);
  read_fp = popen(cmd, "r");
  if ( read_fp != NULL )
  {
    chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
 pclose(read_fp);
 
    if (chars_read > 0)
    {
      ret = 1;
    }
    else
    {
  fprintf(stderr, "%s: NO FOUND\r\n",if_name);
  return 0;
    }
  }
 
 if(ret == 1)
 {
 memset( buffer, 0, BUFSIZ );
 memset( cmd, 0, 100 );
 sprintf(cmd, "ifconfig |grep %s",if_name);
 read_fp = popen(cmd, "r");
 if ( read_fp != NULL )
 {
   chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
   pclose(read_fp);
  
   if (chars_read > 0)
   {
     ret = 2;
   }
   else
   {
  fprintf(stderr, "%s: DOWN\r\n",if_name);
  return 1;
   }
 }
 }
 
 if(ret == 2)
 {
 memset( buffer, 0, BUFSIZ );
 memset( cmd, 0, 100 );
 sprintf(cmd, "ifconfig %s | grep RUNNING | awk '{print $3}'",if_name);
 read_fp = popen(cmd, "r");
 if ( read_fp != NULL )
 {
   chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
   pclose(read_fp);
  
   if (chars_read > 0)
   {
  fprintf(stderr, "%s: LINKED\r\n",if_name);
  return 3;
   }
   else
   {
  fprintf(stderr, "%s: UNPLUGGED\r\n",if_name);
  return 2;
   }
 }
 }
 
 return -1;
}
 
 
int main(int argc, char* argv[])
{
  int i=0;
 if(argc != 2)
 {
 fprintf(stderr, "usage: %s <ethname>", argv[0]);
 return -1;
 }
 
  i = get_if_status(argv[1]);
  printf( "if_status = %d\n", i );
  return 0;
}

嵌入式编译 mips-linux-gnu-gcc -mips32 -EL -mhard-float -Wall -o netlink netlink.c

测试结果

# ./netlink eth100
eth100: NO FOUND
if_status = 0
# 
# ifconfig eth0 down
# ./netlink eth0 
eth0: DOWN
if_status = 1
# 
# ifconfig eth0 up
# ./netlink eth0
eth0: UNPLUGGED
if_status = 2
#
# ./netlink eth0
eth0: LINKED
if_status = 3

2.C语言实现linux网卡连接检测的方法

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
 
 
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/mii.h>
#include <linux/sockios.h>
#include <errno.h>
 
 
int get_if_miireg(const char *if_name, int phy_id, int reg_num )
{
 int fd = -1; 
 struct ifreq ifr; 
 struct mii_ioctl_data *mii; 
 int value; 
 
 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
 {
 perror("socket");
 close(fd);
 return -1; 
 }
 
 bzero(&ifr, sizeof(ifr));
 strncpy(ifr.ifr_name, if_name, IFNAMSIZ-1); 
 ifr.ifr_name[IFNAMSIZ-1] = 0; 
 
 if (ioctl(fd, SIOCGMIIPHY, &ifr) < 0)
 { 
 perror("ioctl");
 close(fd);
 return -1; 
 }
 
 mii = (struct mii_ioctl_data *)&ifr.ifr_data;
 mii->reg_num = reg_num;//0x01
 if (ioctl(fd, SIOCGMIIREG, &ifr) < 0)
 { 
 perror("ioctl");
 close(fd);
 return -1;
 }
 close(fd);
 value = ((mii->val_out&0x04)>>2);
 return value;
}
 
int main(int argc, char* argv[])
{
  int i=0;
  if(argc != 2)
  {
    fprintf(stderr, "usage: %s <ethname>", argv[0]);
    return -1;
  }
 
  i = get_if_miireg(argv[1],0x10,0x01);
  printf( "if_status = %d\n", i );
  return 0;
}

只能识别网线是否连接,还没识别网卡是否存在状态,也不识别网卡存在是否为down状态

3.C语言实现linux网卡检测改进版

C语言+shell 实现linux网卡状态检测 和 C语言实现linux网卡连接检测 2文的方法各有缺陷,比如有些系统执行ifconfig需要root权限,要不就不支持ioctl(fd, SIOCGMIIPHY, &ifr)这样的操作。以下给出了C语言实现linux网卡连接检测 的改进版实现与C语言+shell 实现linux网卡状态检测 同样的功能

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
 
 
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/mii.h>
#include <linux/sockios.h>
#include <errno.h>
 
#include <ifaddrs.h>
#include <arpa/inet.h> 
 
#include <linux/ethtool.h>
 
int cshell_netlink_status(char *if_name)
{
 char  buffer[BUFSIZ];
 char  cmd[100];
 FILE  *read_fp;
 int    chars_read;
 int    ret =0;
 
 memset( buffer, 0, BUFSIZ );
 memset( cmd, 0, 100 );
 sprintf(cmd, "ifconfig -a | grep %s",if_name);
 read_fp = popen(cmd, "r");
 if ( read_fp != NULL )
 {
 chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
 pclose(read_fp);
 
 if (chars_read > 0)
 {
  ret = 1;
 }
 else
 {
  fprintf(stderr, "DEVICE_NONE\r\n");
  return 0;
 }
 }
 
 if(ret == 1)
 {
 memset( buffer, 0, BUFSIZ );
 memset( cmd, 0, 100 );
 sprintf(cmd, "ifconfig |grep %s",if_name);
 read_fp = popen(cmd, "r");
 if ( read_fp != NULL )
 {
  chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
  pclose(read_fp);
 
  if (chars_read > 0)
  {
  ret = 2;
  }
  else
  {
  fprintf(stderr, "DEVICE_DOWN\r\n");
  return 1;
  }
 }
 }
 
 if(ret == 2)
 {
 memset( buffer, 0, BUFSIZ );
 memset( cmd, 0, 100 );
 sprintf(cmd, "ifconfig %s | grep RUNNING | awk '{print $3}'",if_name);
 read_fp = popen(cmd, "r");
 if ( read_fp != NULL )
 {
   chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
   pclose(read_fp);
  
   if (chars_read > 0)
   {
  fprintf(stderr, "DEVICE_LINKED\r\n");
  return 3;
   }
   else
   {
  fprintf(stderr, "DEVICE_UNPLUGGED\r\n");
  return 2;
   }
 }
 }
 
 return -1;
}
 
 
int c_netlink_status(const char *if_name )
{
 int fd = -1; 
 struct ifreq ifr; 
 
 struct ifconf ifc; 
 struct ifreq ifrs_buf[100]; 
 int if_number =0;
 int i;
 
 
 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
 {
 fprintf(stderr, "%s: socket error [%d] %s\r\n",if_name, errno, strerror(errno));
 close(fd);
 return -1; 
 }
 
 ifc.ifc_len = sizeof(ifrs_buf); 
 ifc.ifc_buf = (caddr_t)ifrs_buf; 
 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) <0) 
 {
 fprintf(stderr, "%s: ioctl SIOCGIFCONF error [%d] %s\r\n",if_name, errno, strerror(errno));
 close(fd);
 return -1; 
 }
 
 if_number = ifc.ifc_len / sizeof(struct ifreq);
 for(i=0; i< if_number; i++)
 {
 if(strcmp(if_name,ifrs_buf[i].ifr_name ) == 0)
 {
  break;
 }
 }
 
 if(i >= if_number)
 {
 close(fd);
 fprintf(stderr, "DEVICE_NONE\r\n");
 return 0;
 }
 
 bzero(&ifr, sizeof(ifr));
 strncpy(ifr.ifr_name, if_name, IFNAMSIZ-1); 
 ifr.ifr_name[IFNAMSIZ-1] = 0; 
 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) <0) 
 {
 fprintf(stderr, "%s: ioctl SIOCGIFFLAGS error [%d] %s\r\n",if_name, errno, strerror(errno));
 close(fd);
 return -1; 
 }
#if 1 
 if(!(ifr.ifr_flags & IFF_UP))
 {
 close(fd);
 fprintf(stderr, "DEVICE_DOWN\r\n");
 return 1;
 }
 
 if(!(ifr.ifr_flags & IFF_RUNNING))
 {
 close(fd);
 fprintf(stderr, "DEVICE_UNPLUGGED\r\n");
 return 2 ;
 }
 
 fprintf(stderr, "DEVICE_LINKED\r\n");
 return 3;
 
#else
{
 struct ethtool_value edata;
 if(!(ifr.ifr_flags & IFF_UP) || !(ifr.ifr_flags & IFF_RUNNING))
 {
 close(fd);
 fprintf(stderr, "%s: DOWN\r\n",if_name);
 return 1;
 }
 edata.cmd = ETHTOOL_GLINK;
 edata.data = 0;
 ifr.ifr_data = (char *) &edata;
 if(ioctl( fd, SIOCETHTOOL, &ifr ) < 0)
 {
 fprintf(stderr, "%s: ioctl SIOCETHTOOL error [%d] %s\r\n",if_name, errno, strerror(errno));
 close(fd);
 return -1; 
 }
 
 if(edata.data == 0)
 {
 fprintf(stderr, "DEVICE_UNPLUGGED\r\n");
 return 2; 
 }
 else
 {
 fprintf(stderr, "DEVICE_LINKED\r\n");
 return 3; 
 }
}
#endif
}
 
int main(int argc, char* argv[])
{
 int i=0;
 if(argc != 2)
 {
 fprintf(stderr, "usage: %s <ethname>\r\n", argv[0]);
 return -1;
 }
 
 i = cshell_netlink_status(argv[1]);
 
 printf( "cshell_netlink_status if_status = %d\n", i );
 
 i = c_netlink_status(argv[1]);
 printf( "c_netlink_status if_status = %d\n", i );
 
 return 0;
}

4.C语言实现linux网卡检测精简版

万能的网络,通过getifaddrs可以大大减少编码量,获得 C语言实现linux网卡检测-改进版 同样的效果

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <ifaddrs.h>
#include <arpa/inet.h> 
 
 
int c_ifaddrs_netlink_status(const char *if_name )
{
 struct ifaddrs *ifa = NULL, *ifList; 
 
 if (getifaddrs(&ifList) < 0)
 {
 return -1;
 }
 
 for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next) 
 {
 if(ifa->ifa_addr->sa_family == AF_INET) 
 {
  if(strcmp(ifa->ifa_name, if_name) ==0)
  {
  if(!(ifa->ifa_flags & IFF_UP))
  {
   printf("DEVICE_DOWN\r\n");
   freeifaddrs(ifList);
   return 1;
  }
 
  if(!(ifa->ifa_flags & IFF_RUNNING))
  {
   printf("DEVICE_UNPLUGGED\r\n");
   freeifaddrs(ifList);
   return 2;
  }
 
  printf("DEVICE_LINKED\r\n");
  freeifaddrs(ifList);
  return 3;
  }
 } 
 } 
 
 printf(stderr, "DEVICE_NONE\r\n");
 freeifaddrs(ifList);
 return 0;
}
 
int main(int argc, char* argv[])
{
 int i=0;
 if(argc != 2)
 {
 fprintf(stderr, "usage: %s <ethname>\r\n", argv[0]);
 return -1;
 }
 
 i = c_ifaddrs_netlink_status(argv[1]);
 
 fprintf(stderr,"c_ifaddrs_netlink_status if_status = %d\n", i );
 return 0;
}

5.demo

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <glob.h>
#include <errno.h>
#include <sys/sysmacros.h>
#include <sys/utsname.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <linux/un.h>
#include <poll.h>
#include <assert.h>
#include <linux/if.h>
#include <linux/types.h>
#include <linux/wireless.h>
 
int check_assoc(char* ifname)
{
	int socket_id, i;
	struct iwreq wrq;
 
	socket_id = socket(AF_INET, SOCK_DGRAM, 0);
	strcpy(wrq.ifr_ifrn.ifrn_name, ifname);
	ioctl(socket_id, SIOCGIWAP, &wrq);
	close(socket_id);
 
	for (i = 0; i < 6; i++)
		if (wrq.u.ap_addr.sa_data[i])
			return 1;
	return 0;
}
 
int main()
{
 
    if (check_assoc("apcli0")) 
    {
         printf("设备网络可用.\n");
	}
	else 
    {
         printf("设备网络不可用.\n");
	}
    return 0}

6.参考链接

C语言+shell实现linux网卡状态检测
C语言实现linux网卡连接检测的方法
C语言实现linux网卡检测改进版
C语言实现linux网卡检测精简版

  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值