Linux下wifi工具wpa_cli使用
扫描[root@demo]# wpa_cli -i wlan0 scanOK查看扫描结果[root@demo]# wpa_cli -i wlan0 scan_resultbssid / frequency / signal level / flags / ssiddc:fe:18:19:bf:4d2462-47[WPA-PSK-CCMP][WPA2-P...
·
wpa_cli使用
- 扫描
- 查看扫描结果
- 添加network
- 设置SSID
- 选择某个network
- 查看networks
- 使能某个network
- 查询连接情况
- 自动分配IP地址
- API Demo
扫描
[root@demo]# wpa_cli -i wlan0 scan
OK
查看扫描结果
- 注意返回格式
bssid ’\t’ frequency ’\t’ signal level ’\t’ flags ’\t’ ssid ’\n’
这中间是用制表符’\t’,隔开的,最后一个字符是’\n’
[root@demo]# wpa_cli -i wlan0 scan_result
bssid / frequency / signal level / flags / ssid
dc:fe:18:19:bf:4d 2462 -47 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP-LINK_BF4D
34:0a:98:97:9a:14 2427 -66 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS] CMCC-3KUu
d4:62:ea:80:84:10 2462 -71 [WPA2-PSK-CCMP][WPS][ESS] HUAWEI-H
68:db:54:6b:25:8f 2422 -75 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] @PHICOMM_8D
50:fa:84:88:6a:85 2462 -75 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] TP-LINK_6856
d0:76:e7:35:4a:03 2437 -77 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP-LINK_903
添加network
[root@demo]#wpa_cli -i wlan0 add_network
0
[root@demo]#wpa_cli -i wlan0 add_network
1
设置 SSID
- [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] (wpa加密),wifi名称是name, wifi密码是psk
[root@demo]$ wpa_cli -i wlan0 set_network 0 ssid '"name"'
[root@demo]$ wpa_cli -i wlan0 set_network 0 psk '"psk"'
[root@demo]$ wpa_cli -i wlan0 enable_network 0
- [WEP][ESS]wep加密
[root@demo]$ wpa_cli -i wlan0 set_network 0 ssid '"name"'
[root@demo]$ wpa_cli -i wlan0 set_network 0 key_mgmt NONE
[root@demo]$ wpa_cli -i wlan0 set_network 0 wep_key0 '"psk"'
[root@demo]$ wpa_cli -i wlan0 enable_network 0
- [ESS]无加密连接,wifi名称是name
[root@demo] $ wpa_cli -i wlan0 set_network 0 ssid '"name"'
[root@demo] $ wpa_cli -i wlan0 set_network 0 key_mgmt NONE
[root@demo]$ wpa_cli -i wlan0 enable_network 0
查看networks
[root@demo] $ wpa_cli list_networks
Selected interface 'wlan0'
network id /ssid /bssid/ flags
0 any [CURRENT]
1 any [DISABLE]
选择某个network
[root@demo] $ wpa_cli select_netowrk 0
Selected interface 'wlan0'
OK
使能某个network
[root@demo] $ wpa_cli -i wlan0 enable_network 0
OK
查询连接情况
[root@demo] $ wpa_cli -i wlan0 status
//返回内容请见下图
自动分配IP地址
[root@demo] $ udhcpc -i wlan0
udhcpc(v1.23.2) started
Sending discover...
Sending select for 192.168.0.104...
Lease of 192.168.0.104 obtained, lease time 7200
deleting routers
adding dns
API Demo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
static char wifi_ssid[16][128];
static char *wifi_name[16];
//----------------------------------------------------------------------
// D E F I N I T I O N
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// L O C A L D E F I N I T I O N
//----------------------------------------------------------------------
static const char *SCAN ="wpa_cli -i wlan0 scan";
static const char *SCAN_RES ="wpa_cli -i wlan0 scan_result";
static const char *ADD_NETWORK = "wpa_cli -i wlan0 add_network";
static const char *SELECT_NETWORK ="wpa_cli -i wlan0 select_network";
static const char *STATUS ="wpa_cli -i wlan0 status";
static const char *SET_SSID = "wpa_cli -i wlan0 set_network 0 ssid";
static const char *SET_PASSWORD = "wpa_cli -i wlan0 set_network 0 psk";
static const char *ENABLE_NETWORK = "wpa_cli -i wlan0 enable_network 0";
static const char *UDHCPC_NETWORK = "udhcpc -i wlan0";
//----------------------------------------------------------------------
// L O C A L F U N C T I O N D E F I N I T I O N
//----------------------------------------------------------------------
static int wifi_scan(void);
static void wifi_scan_result(char (*ssid_buf)[128]);
//----------------------------------------------------------------------
// L O C A L F U N C T I O N I M P L E M E N T A T I O N
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// function: wifi_scan
// parameter: none
// return: 1 -- scan OK 0 -- scan failed
//----------------------------------------------------------------------
static int wifi_scan(void)
{
FILE *fp;
char buf[8]={0};
if((fp = popen(SCAN,"r"))==NULL)
{
perror("SCAN");
exit(1);
}
while(fgets(buf,sizeof(buf),fp)!=NULL) //fgets()此处是否会阻塞?
{
printf("%s\n",buf);
if(strstr(buf,"OK")!=NULL)
{
printf("close fp and return\n");
pclose(fp);
return 1;
}
}
return 0;
}
//----------------------------------------------------------------------
// function: set_noblock_read
// statement: set fgets noblock read mode
// parameter: FILE* fstream, which is popen func returned
// return: none
//----------------------------------------------------------------------
static void set_noblock_read(FILE *fstream)
{
int fd;
int flags;
fd = fileno(fstream);
flags = fcntl(fd,F_GETFL,0);
flags |= O_NONBLOCK;
fcntl(fd,F_SETFL,flags);
return ;
}
//----------------------------------------------------------------------
// function: wifi_scan_res
// statement: show scan result, get ssid to the ssid buf
// parameter: wifi_ssid buf
// return: none
//----------------------------------------------------------------------
static void wifi_scan_result(char (*ssid_buf)[128])
{
FILE *fp;
char *p_n=NULL;
char *p_ssid_end = NULL;
char *p_ssid_start=NULL;
char buf[4096]={0};
int flags;
int ssid_index=0;
if((fp=popen(SCAN_RES,"r"))==NULL)
{
perror("SCAN_RES");
exit(1);
}
int fd = fileno(fp); //把文件指针转换成文件描述符用fileno函数
flags = fcntl(fd,F_GETFL,0);
flags |= O_NONBLOCK; //O_NONBLOCK = 04000
fcntl(fd,F_SETFL,flags);
sleep(1); //这里必须要等待一下,否则fgets立即返回NULL,捕获不到数据
while(fgets(buf,sizeof(buf),fp)!=NULL) //wifi扫描不到是否意味着就一直阻塞在这里?比如在没有WiFi的地方
{
//printf("%s",buf);
if(strstr(buf,(const char*)"bssid")!=NULL) //忽略第一行
{
continue;
}
if((p_n = strstr(buf,"\n"))!=NULL) //注意,\n是接收到的最后一个字符
{
p_ssid_end = p_n-1; //'\n前面一个字符'
while(*p_n !='\t') //中间是用'\t'制表符隔开的
{
p_n--;
}
p_ssid_start = p_n; //'\t'
strncpy(ssid_buf[ssid_index],(p_ssid_start+1),(p_ssid_end-p_ssid_start));
printf("%s\n",ssid_buf[ssid_index]);
ssid_index++;
}
}
printf("fgets() return without block and close fp\n");
pclose(fp);
return ;
}
//----------------------------------------------------------------------
// function: wifi_add_network
// statement: add net work, there where a network list, wpa_cli list_networks
// can see how many network add add in the lsit, but there is only
// one in the current state.
// parameter: null
// return: the network id in the system
//
//----------------------------------------------------------------------
static int wifi_add_network(void)
{
FILE *fp;
char buf[8]={0};
if((fp = popen(ADD_NETWORK,"r"))==NULL)
{
perror("ADD_NETWORK");
exit(1);
}
while(fgets(buf,sizeof(buf),fp)!=NULL)
{
printf("%s\n",buf);
}
return 0;
}
//----------------------------------------------------------------------
// function: wifi_set_ssid(char *ssid_name)
// statement: set ssid name
//
// parameter:
// return: 1 -- success
// 0 -- failed
//----------------------------------------------------------------------
static int wifi_set_ssid(char *ssid_name)
{
FILE *fp;
char buf[128]={0};
char cmd[128]="";
strcat(cmd,SET_SSID);
strcat(cmd," '\"");
strcat(cmd,ssid_name);
strcat(cmd,"\"'");
printf("%s\n",cmd);
if((fp=popen(cmd,"r"))==NULL)
{
perror("set ssid failed");
exit(1);
}
set_noblock_read(fp); //no block read
sleep(1);
while(fgets(buf,sizeof(buf),fp)!=NULL)
{
printf("%s\n",buf);
if(strstr(buf,"OK")!=NULL)
{
pclose(fp);
return 1;
}
}
pclose(fp);
return 0;
}
//----------------------------------------------------------------------
// function: wifi_set_password(char *password)
// statement: set password
//
// parameter:
// return: 1 -- success
// 0 -- failed
//----------------------------------------------------------------------
static int wifi_set_password(char *password)
{
FILE *fp;
char buf[128]={0};
char cmd[128]="";
strcat(cmd,SET_PASSWORD);
strcat(cmd," '\"");
strcat(cmd,password);
strcat(cmd,"\"'");
printf("%s\n",cmd);
if((fp=popen(cmd,"r"))==NULL)
{
perror("set password failed");
exit(1);
}
set_noblock_read(fp); //no block read
sleep(1);
while(fgets(buf,sizeof(buf),fp)!=NULL)
{
printf("%s\n",buf);
if(strstr(buf,"OK")!=NULL)
{
pclose(fp);
return 1;
}
}
pclose(fp);
return 0;
}
//----------------------------------------------------------------------
// function: wifi_enable_netword(char *password)
// statement: after set ssid and psk, need to enable network.
// then can use udhcpc -i wlan0
// parameter:
// return: 1 -- success
// 0 -- failed
//----------------------------------------------------------------------
static int wifi_enable_network(void)
{
FILE *fp;
char buf[8]={0};
fp=popen(ENABLE_NETWORK,"r");
while(fgets(buf,8,fp)!=NULL)
{
printf("%s\n",buf);
if(strstr(buf,"OK")!=NULL)
{
pclose(fp);
return 1;
}
}
pclose(fp);
return 0;
}
//----------------------------------------------------------------------
// M I A N F U N C T I O N
//----------------------------------------------------------------------
int main()
{
memset(wifi_ssid,0,sizeof(char)*16*128);
wifi_scan();
sleep(2);
wifi_scan_result(wifi_ssid);
wifi_add_network();
wifi_set_ssid("TP-LINK_BF4D");
wifi_set_password("wjj198712162803");
wifi_enable_network();
exit(1);
}
更多推荐
已为社区贡献1条内容
所有评论(0)