Linux怎么编程检测网线是不是拔掉了
答案:1 悬赏:10 手机版
解决时间 2021-12-16 19:28
- 提问者网友:做你怀里的猫
- 2021-12-16 04:10
Linux怎么编程检测网线是不是拔掉了
最佳答案
- 五星知识达人网友:上分大魔王
- 2022-01-10 03:59
使用ifconfig命令,如果含有“RUNNING”,说明网线接入,否则就没有。
例:
ifconfig
ifconfig eth0
ifconfig eth0|grep "RUNNING"
代码可以写成
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <string.h>
#include <stdio.h>
#include <netdb.h>
int net_detect(char* net_name)
{
int skfd = 0;
struct ifreq ifr;
struct sockaddr_in *pAddr = NULL;
skfd = socket(AF_INET, SOCK_DGRAM, 0);
if(skfd < 0)
{
printf("%s:%d Open socket error!\n", __FILE__, __LINE__);
return -1;
}
strcpy(ifr.ifr_name, net_name);
if(ioctl(skfd, SIOCGIFFLAGS, &ifr) <0 )
{
printf("%s:%d IOCTL error!\n", __FILE__, __LINE__);
printf("Maybe ethernet inferface %s is not valid!", ifr.ifr_name);
close(skfd);
return -1;
}
if(ifr.ifr_flags & IFF_RUNNING)
{
printf("%s is running :)\n", ifr.ifr_name);
}
else
{
printf("%s is not running :(\n", ifr.ifr_name);
}
if(ioctl(skfd,SIOCGIFADDR,&ifr)<0)
{
printf("SIOCGIFADDR IOCTL error!\n");
close(skfd);
return -1;
}
pAddr = (struct sockaddr_in *)&(ifr.ifr_addr);
printf("ip addr :[%s]\n", inet_ntoa(pAddr->sin_addr));
if(ioctl(skfd,SIOCGIFHWADDR,&ifr)<0)
{
printf("SIOCGIFHWADDR IOCTL error!\n");
close(skfd);
return -1;
}
printf("mac addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
(unsigned char)ifr.ifr_hwaddr.sa_data[0],
(unsigned char)ifr.ifr_hwaddr.sa_data[1],
(unsigned char)ifr.ifr_hwaddr.sa_data[2],
(unsigned char)ifr.ifr_hwaddr.sa_data[3],
(unsigned char)ifr.ifr_hwaddr.sa_data[4],
(unsigned char)ifr.ifr_hwaddr.sa_data[5]);
close(skfd);
return 0;
}
int main()
{
int ret;
ret = net_detect("eth0");
return ret;
}
例:
ifconfig
ifconfig eth0
ifconfig eth0|grep "RUNNING"
代码可以写成
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <string.h>
#include <stdio.h>
#include <netdb.h>
int net_detect(char* net_name)
{
int skfd = 0;
struct ifreq ifr;
struct sockaddr_in *pAddr = NULL;
skfd = socket(AF_INET, SOCK_DGRAM, 0);
if(skfd < 0)
{
printf("%s:%d Open socket error!\n", __FILE__, __LINE__);
return -1;
}
strcpy(ifr.ifr_name, net_name);
if(ioctl(skfd, SIOCGIFFLAGS, &ifr) <0 )
{
printf("%s:%d IOCTL error!\n", __FILE__, __LINE__);
printf("Maybe ethernet inferface %s is not valid!", ifr.ifr_name);
close(skfd);
return -1;
}
if(ifr.ifr_flags & IFF_RUNNING)
{
printf("%s is running :)\n", ifr.ifr_name);
}
else
{
printf("%s is not running :(\n", ifr.ifr_name);
}
if(ioctl(skfd,SIOCGIFADDR,&ifr)<0)
{
printf("SIOCGIFADDR IOCTL error!\n");
close(skfd);
return -1;
}
pAddr = (struct sockaddr_in *)&(ifr.ifr_addr);
printf("ip addr :[%s]\n", inet_ntoa(pAddr->sin_addr));
if(ioctl(skfd,SIOCGIFHWADDR,&ifr)<0)
{
printf("SIOCGIFHWADDR IOCTL error!\n");
close(skfd);
return -1;
}
printf("mac addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
(unsigned char)ifr.ifr_hwaddr.sa_data[0],
(unsigned char)ifr.ifr_hwaddr.sa_data[1],
(unsigned char)ifr.ifr_hwaddr.sa_data[2],
(unsigned char)ifr.ifr_hwaddr.sa_data[3],
(unsigned char)ifr.ifr_hwaddr.sa_data[4],
(unsigned char)ifr.ifr_hwaddr.sa_data[5]);
close(skfd);
return 0;
}
int main()
{
int ret;
ret = net_detect("eth0");
return ret;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯