linux下无线网卡测试,Linux C程序如何检测WIFI无线USB网卡是否可用?
最新做一个WIFI应用项目。如何检测WIFI USB设备是否插上了呢?特此共享。
第一种方法,采用读取文件的方式。在linux下,任何一种设备都可看成文件。通过分析相关文件信息,可得知WIFI设备是否存在;代码示例如下:
static void WIFI_Enum_Device(void)
{
char buff[1024];
FILE * fh;
/* Check if /proc/net/wireless is available */
fh = fopen(PROC_NET_WIRELESS, "r");
if(fh != NULL)
{
/* Success : use data from /proc/net/wireless */
/* Eat 2 lines of header */
fgets(buff, sizeof(buff), fh);
fgets(buff, sizeof(buff), fh);
/* Read each device line */
while(fgets(buff, sizeof(buff), fh))
{
char name[IFNAMSIZ + 1];
char *s;
/* Skip empty or almost empty lines. It seems that in some
* cases fgets return a line with only a newline. */
if((buff[0] == '\0') || (buff[1] == '\0'))
continue;
/* Extract interface name */
s = WIFI_Get_DeviceName(name, sizeof(name), buff);
if(!s)
{
/* Failed to parse, complain and continue */
#ifndef IW_RESTRIC_ENUM
fprintf(stderr, "Cannot parse " PROC_NET_DEV "\n");
#else
fprintf(stderr, "Cannot parse " PROC_NET_WIRELESS "\n");
#endif
}
else
/* Got it, save the name about this interface */
{//we always use the first detected device when doing first time detecting
if(s_DeviceCount == 0)
{
if(strcmp(s_Deviceinfo.DeviceName,name))
{
memset((char *)&s_Deviceinfo, 0, sizeof(DeviceInfo_t));
memcpy(s_Deviceinfo.DeviceName,name,IFNAMSIZ);
}
if(strlen(s_SavedDevice) == 0)//this is the first detected device when doing first time detecting, we save it
memcpy(s_SavedDevice,name,IFNAMSIZ);
}
else
{//there is more than one device, we should use the first detected
if(!strcmp(s_SavedDevice,name))
{
memset((char *)&s_Deviceinfo, 0, sizeof(DeviceInfo_t));
memcpy(s_Deviceinfo.DeviceName,name,IFNAMSIZ);
}
}
s_DeviceCount++;
}
}
fclose(fh);
}
}
static char* WIFI_Get_DeviceName(char * name, /* Where to store the name */
int nsize, /* Size of name buffer */
char * buf) /* Current position in buffer */
{
char * end;
/* Skip leading spaces */
while(isspace(*buf))
buf++;
#ifndef IW_RESTRIC_ENUM
/* Get name up to the last ':'. Aliases may contain ':' in them,
* but the last one should be the separator */
end = strrchr(buf, ':');
#else
/* Get name up to ": "
* Note : we compare to ": " to make sure to process aliased interfaces
* properly. Doesn't work on /proc/net/dev, because it doesn't guarantee
* a ' ' after the ':'*/
end = strstr(buf, ": ");
#endif
/* Not found ??? To big ??? */
if((end == NULL) || (((end - buf) + 1) > nsize))
return(NULL);
/* Copy */
memcpy(name, buf, (end - buf));
name[end - buf] = '\0';
/* Return value currently unused, just make sure it's non-NULL */
return(end);
}
RETURN_TYPE APP_WIFI_DetectDevice(void)
{
char command[50] = {'\0'};
s_DeviceCount = 0; //reset count
WIFI_Enum_Device();
s_LastDeviceCount = s_DeviceCount;
if(s_DeviceCount > 0)
{
sprintf(command,"ifconfig %s up",s_Deviceinfo.DeviceName);
system(command); //boot up the device firstly
return SYS_NOERROR;
}
else
return SYS_FAILED;
}
第二种方法,利用linux ioctl函数读取I/O接口的相关信息。
/*****************************************************************************
* Name : trid_char * APP_NetLink_GetIFFLAGS(char *NetDev )
* Description : Get net interface IFFLAGS
* Params : NetDev
* Returns : the string of the NetDev
* Author/date : Danny.Hu /2011.11.16
*****************************************************************************/
RETURN_TYPE APP_NetLink_GetIFFlags( trid_char *NetDev )
{
int fd = -1;
int InterfaceFlags;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, NetDev);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
printf("Cannot get control socket\n");
close(fd);
return SYS_FAILED;
}
else if( 0!=(ioctl(fd, SIOCGIFFLAGS, (char*)&ifr)) )
{
printf("Cannot get Network Interface Flags!\n");
close(fd);
return SYS_FAILED;
}
InterfaceFlags = ifr.ifr_flags;
printf("
if ( InterfaceFlags & IFF_UP) printf("Network %s is UP, ", NetDev);
if ( InterfaceFlags & IFF_BROADCAST) printf("Network %s is BCAST, ", NetDev);
if ( InterfaceFlags & IFF_MULTICAST) printf("Network %s is MCAST, ", NetDev);
if ( InterfaceFlags & IFF_LOOPBACK) printf("Network %s is LOOP, ", NetDev);
if ( InterfaceFlags & IFF_POINTOPOINT) printf("Network %s is P2P, ", NetDev);
printf(">\n");
close(fd);
return SYS_NOERROR;
}
linux下无线网卡测试,Linux C程序如何检测WIFI无线USB网卡是否可用?相关推荐
- Linux下配置IPV6,C程序适配IPV6
Linux下配置IPV6,C程序适配IPV6 1. 环境 CentOS7.7 最小系统 2. linux下配置IPV6 需要先确认CentOS7.7操作系统中已安装 ip6tables 组件 修改网络 ...
- linux下 为自己编写的程序 添加tab自动补全 功能
linux下 为自己编写的程序 添加tab自动补全功能 入门 complete 在我的tmp下随便写了一个a.sh, 为他补全 edit /etc/bash_completion.d/foo _foo ...
- Kali Linux 下渗透测试 | 3389 批量爆破神器 | hydra | 内网渗透测试
kali linux 下渗透测试,3389 批量爆破神器 hydra -M target.txt rdp -L userlist.txt -P passwordlist.txt -V root@kal ...
- 在Linux下使用linuxdeployqt发布Qt程序
一.简介 linuxdeployqt 是Linux下的qt打包工具,可以将应用程序使用的资源(如库,图形和插件)复制到二进制运行文件所在的文件夹中. 二.安装linuxdeployqt 去github ...
- 【OpenCV】Linux 下用 g++ 编译 OpenCV 程序
编译命令: g++ gaussianBlur.cpp -o test `pkg-config --cflags --libs opencv` 执行编译生成的可执行文件: ./test gaussian ...
- linux下使用AppImage打包qt程序
linux下使用AppImage打包qt程序 下载工具 1.1 linuxdeployqt 下载地址:https://github.com/probonopd/linuxdeployqt/releas ...
- jmeter在Linux下执行测试
目录 jmeter在Linux下执行测试 1.背景 2.环境安装 3.no-gui模式运行脚本 4.在本地查看Linux上运行的结果 jmeter在Linux下执行测试 1.背景 JMeter不仅能十 ...
- linux下ioctl操作网络接口,linux下无线网卡的ioctl 接口
var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/ba ...
- Linux下推荐的常用应用程序列表
Linux下推荐的常用应用程序列表 一,网页浏览 1,firefox firefox是现在最火的一个浏览器,支持好多扩展和插件,也有很多漂亮的主题.firefox就是mozilla-firefox,他 ...
最新文章
- 抛弃注意力,比EfficientNet快3.5倍,类Transformer新模型跨界视觉任务实现新SOTA
- 智能生产的现状与未来!
- 网络资源(9) - TDD视频
- 实例45:python
- nodejs express
- Spark Streaming之Kafka的Receiver和Direct方式
- chrome内核浏览器调用本地exe客户端
- Java初级程序员需要知道的基本Java代码规范
- 【HTML】使用Vscode快速书写HTML
- 人工智能Logo设计师Brandmark
- 作为一名APP开发者,你更Care什么?
- 黑产反诈有方法,异常识别我在行—欺诈反洗钱等领域用得最多的经典算法
- 苹果开发者账号和邓白氏编码申请总结
- 信号与电源完整性分析(一)提高信号质量
- QT程序打包--教你做一个可安装的应用程序
- SEIR模型及多染病仓室再生数的推导
- 数据通信,数据通信原理是什么?
- 看雪学院-OllyDBG入门系列(四)内存写入 笔记
- nvidia orin简介
- 【数字电路】Y图 | 逻辑操作符 | 布尔函数 | Combinational systems