linux下使用shell命令通过wpa_cli控制wpa_supplicant连接wifi

最近在调试wifi,已经把wpa_supplicant 工具编译打包好了,为了测试wif驱动及wifi模块是否ok,需要用shell命令临时启动wifi服务连接wifi热点测试。

首先板子启动用ifconfig后先把wlan0启动起来

root@p1:/ # busybox-smp ifconfig wlan0 up
root@p1:/ # busybox-smp ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:13931 errors:0 dropped:0 overruns:0 frame:0
TX packets:13931 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:773000 (754.8 KiB) TX bytes:773000 (754.8 KiB) wlan0 Link encap:Ethernet HWaddr 7C:DD:90:EC:F2:1E
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:2 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

现在wlan0已经up起来了。

然后启动wpa服务,需要一个wpa_supplicant.conf,我的配置如下

update_config=1
ctrl_interface=/data/misc/wifi/wlan0
eapol_version=1
ap_scan=1
fast_reauth=1

其中的ctrl_interface就是用来和wpa_cli通信使用,其原理是wpa_supplicant是个干活的wifi服务器(比如连接wifi,断开wifi,启动热点等),wpa_cli就是个客户端,通过本地socket通信连接上wpa_supplicant服务器,发命令叫他干活。

现在把wpa_supplicant服务启动起来

root@p1:/ # wpa_supplicant -iwlan0 -c/system/etc/wifi/wpa_supplicant.conf -B

-B是在后台运行,如果想要看过程中的详细log,可以加上参数-dd,然后用logcat同时看log。

现在wpa服务已经起来了,下面用wpa_cli客户端连接到wpa

wpa_cli -iwlan0 -p/data/misc/wifi/wlan0
wpa_cli v2.1-devel-4.4.2
Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi> and contributors This software may be distributed under the terms of the BSD license.
See README for more details. Interactive mode >

以上log显示已经连接上wpa了

如果wpa异常,cli就无法连接会看到一下log,然后wpa_cli处于阻塞状态

Could not connect to wpa_supplicant: wlan0 - re-trying

现在我们的wpa_cli已经连上了,可以通过命令来查看当前的wifi状态

> status
wpa_state=DISCONNECTED
p2p_device_address=7c:dd:90:ec:f2:1e
address=7c:dd:90:ec:f2:1e
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=0 BSSID=00:00:00:00:00:00 SSID=

现在我们还没有连接网络,首先使用scan命令扫描一下附近的wifi网络,然后使用scan_results显示扫描的结果

> scan
OK
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=3 BSSID=00:00:00:00:00:00 SSID=
<3>CTRL-EVENT-SCAN-RESULTS
<3>WPS-AP-AVAILABLE
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=2 BSSID=00:00:00:00:00:00 SSID=
scan scan_interval scan_results
> scan_results
bssid / frequency / signal level / flags / ssid
50:fa:84:50:31:5e 2472 -75 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] \xe6\xb5\xb7\xe8\xb1\x9a
28:6c:07:41:ec:32 5745 -83 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS] AirFly_5G
5e:ea:1d:8b:2b:58 5745 -86 [WPA2-PSK-CCMP][WPS][ESS] DIRECT-58-HP M130 LaserJet
f2:85:c1:92:1e:41 5765 -86 [WPA2-PSK-CCMP][ESS] AIRFLY-921E52
28:6c:07:41:ec:31 2417 -85 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS] AirFly
2a:6c:07:41:ec:31 2417 -86 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS] AirFly_Guest
dc:fe:18:14:e4:65 5805 -92 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] 632-5G
f4:28:53:b9:dc:00 5745 -92 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS] Tony_5G_O
c0:21:0d:45:10:c1 5260 -76 [ESS] X-Link 10C1
f4:28:53:a6:ed:f1 5220 -90 [ESS] PIX-LINK_5G
2a:6c:07:40:ec:31 2417 -89 [ESS]
f4:28:53:a6:ed:f0 2437 -92 [ESS] PIX-LINK_2.4G

这里也支持命令的tab补全功能,现在已经扫描到附近的一些wifi热点了,比如AirFly,AirFly_5G等是我们办公室的无线网,由于这个wifi模块不支持5G频道,现在只能连接2.4G的,AirFly

下面我们来添加一个刚才扫描的网络进来

> add_network
0
> add_network
1
<network id> <variable> <value> = set network variables (shows
list of variables when run without arguments)
<network id> <variable> <value> = set network variables (shows
list of variables when run without arguments)
<network id> <variable> <value> = set network variables (shows
list of variables when run without arguments)
> set_network 1 ssid "AirFly"
OK
> set_network 1 psk "********"
OK

现在只是添加了网络,还没连接,真正连接命令是

enable_network n

后面的参数n就是刚才设置的那个网络 1

> enable_network 1
OK
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=3 BSSID=00:00:00:00:00:00 SSID=
<3>CTRL-EVENT-SCAN-RESULTS
<3>WPS-AP-AVAILABLE
<3>Trying to associate with 28:6c:07:41:ec:31 (SSID='AirFly' freq=2417 MHz)
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=5 BSSID=00:00:00:00:00:00 SSID=
<3>CTRL-EVENT-ASSOC-REJECT status_code=1
<3>CTRL-EVENT-STATE-CHANGE id=1 state=0 BSSID=00:00:00:00:00:00 SSID=AirFly
<3>CTRL-EVENT-ASSOC-REJECT status_code=1
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=3 BSSID=00:00:00:00:00:00 SSID=
<3>CTRL-EVENT-SCAN-RESULTS
<3>WPS-AP-AVAILABLE
<3>Trying to associate with 28:6c:07:41:ec:31 (SSID='AirFly' freq=2417 MHz)
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=5 BSSID=00:00:00:00:00:00 SSID=
<3>CTRL-EVENT-ASSOC-REJECT status_code=1
<3>CTRL-EVENT-STATE-CHANGE id=1 state=0 BSSID=00:00:00:00:00:00 SSID=AirFly
<3>CTRL-EVENT-ASSOC-REJECT status_code=1
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=3 BSSID=00:00:00:00:00:00 SSID=
<3>CTRL-EVENT-SCAN-RESULTS
<3>WPS-AP-AVAILABLE
<3>Trying to associate with 28:6c:07:41:ec:31 (SSID='AirFly' freq=2417 MHz)
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=5 BSSID=00:00:00:00:00:00 SSID=
<3>CTRL-EVENT-STATE-CHANGE id=1 state=6 BSSID=00:00:00:00:00:00 SSID=AirFly
<3>Associated with 28:6c:07:41:ec:31
<3>CTRL-EVENT-STATE-CHANGE id=1 state=7 BSSID=28:6c:07:41:ec:31 SSID=AirFly
<3>CTRL-EVENT-STATE-CHANGE id=1 state=8 BSSID=28:6c:07:41:ec:31 SSID=AirFly
<3>WPA: Key negotiation completed with 28:6c:07:41:ec:31 [PTK=CCMP GTK=TKIP]
<3>CTRL-EVENT-CONNECTED - Connection to 28:6c:07:41:ec:31 completed (auth) [id=1 id_str=]
<3>CTRL-EVENT-STATE-CHANGE id=1 state=9 BSSID=28:6c:07:41:ec:31 SSID=AirFly
<3>CTRL-EVENT-DISCONNECTED bssid=28:6c:07:41:ec:31 reason=15
<3>CTRL-EVENT-STATE-CHANGE id=1 state=0 BSSID=28:6c:07:41:ec:31 SSID=AirFly
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=3 BSSID=00:00:00:00:00:00 SSID=
<3>CTRL-EVENT-SCAN-RESULTS
<3>WPS-AP-AVAILABLE
<3>Trying to associate with 28:6c:07:41:ec:31 (SSID='AirFly' freq=2417 MHz)
<3>CTRL-EVENT-STATE-CHANGE id=-1 state=5 BSSID=00:00:00:00:00:00 SSID=
<3>CTRL-EVENT-STATE-CHANGE id=1 state=6 BSSID=00:00:00:00:00:00 SSID=AirFly
<3>Associated with 28:6c:07:41:ec:31
<3>CTRL-EVENT-STATE-CHANGE id=1 state=7 BSSID=28:6c:07:41:ec:31 SSID=AirFly
<3>CTRL-EVENT-STATE-CHANGE id=1 state=8 BSSID=28:6c:07:41:ec:31 SSID=AirFly
<3>WPA: Key negotiation completed with 28:6c:07:41:ec:31 [PTK=CCMP GTK=TKIP]
<3>CTRL-EVENT-CONNECTED - Connection to 28:6c:07:41:ec:31 completed (auth) [id=1 id_str=]
<3>CTRL-EVENT-STATE-CHANGE id=1 state=9 BSSID=28:6c:07:41:ec:31 SSID=AirFly

这个过程有点长,好几秒到最后显示

CTRL-EVENT-CONNECTED - Connection to 28:6c:07:41:ec:31 completed

的时候才是真正连接上了,现在再用status看一下wifi的状态

> status
bssid=28:6c:07:41:ec:31
ssid=AirFly
id=1
mode=station
pairwise_cipher=CCMP
group_cipher=TKIP
key_mgmt=WPA2-PSK
wpa_state=COMPLETED
p2p_device_address=7c:dd:90:ec:f2:1e
address=7c:dd:90:ec:f2:1e
<3>CTRL-EVENT-STATE-CHANGE id=1 state=9 BSSID=28:6c:07:41:ec:31 SSID=AirFly
<3>CTRL-EVENT-CONNECTED - connection to 28:6c:07:41:ec:31 completed (auth) [id=1 id_str=]
> quit

现在显示已经连接上了wifi了,使用quit命令可以退出wpa_cli。

接下来用udhcpc处理ip地址的

root@p1:/ # busybox-smp ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:295043 errors:0 dropped:0 overruns:0 frame:0
TX packets:295043 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:16374716 (15.6 MiB) TX bytes:16374716 (15.6 MiB) wlan0 Link encap:Ethernet HWaddr 7C:DD:90:EC:F2:1E
inet6 addr: fe80::7edd:90ff:feec:f21e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:468 errors:0 dropped:203 overruns:0 frame:0
TX packets:10 errors:0 dropped:1 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:136293 (133.0 KiB) TX bytes:1248 (1.2 KiB) root@p1:/ # busybox-smp udhcpc -iwlan0
udhcpc (v1.20.0) started
Sending discover...
Sending select for 192.168.11.55...
Lease of 192.168.11.55 obtained, lease time 43200

现在获取到一个可用的ip地址,使用ifconfig设置给wlan0

root@p1:/ # busybox-smp ifconfig wlan0 192.168.11.55
root@p1:/ # busybox-smp ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:326867 errors:0 dropped:0 overruns:0 frame:0
TX packets:326867 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:18140948 (17.2 MiB) TX bytes:18140948 (17.2 MiB) wlan0 Link encap:Ethernet HWaddr 7C:DD:90:EC:F2:1E
inet addr:192.168.11.55 Bcast:192.168.11.255 Mask:255.255.255.0
inet6 addr: fe80::7edd:90ff:feec:f21e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:765 errors:0 dropped:253 overruns:0 frame:0
TX packets:12 errors:0 dropped:1 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:225470 (220.1 KiB) TX bytes:1960 (1.9 KiB)

网络现在算是简单的配好了,可以和电脑端用ping命令来看下是否连通

在pc上通过cmd窗口ping 192.158.11.55

C:\Users\LXN>ping 192.168.11.55

正在 Ping 192.168.11.55 具有 32 字节的数据:
来自 192.168.11.55 的回复: 字节=32 时间=23ms TTL=64
来自 192.168.11.55 的回复: 字节=32 时间=31ms TTL=64
来自 192.168.11.55 的回复: 字节=32 时间=27ms TTL=64
来自 192.168.11.55 的回复: 字节=32 时间=18ms TTL=64 192.168.11.55 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 18ms,最长 = 31ms,平均 = 24ms C:\Users\LXN>ping 192.168.11.55 正在 Ping 192.168.11.55 具有 32 字节的数据:
来自 192.168.11.55 的回复: 字节=32 时间=627ms TTL=64
来自 192.168.11.55 的回复: 字节=32 时间=580ms TTL=64
来自 192.168.11.55 的回复: 字节=32 时间=275ms TTL=64
请求超时。 192.168.11.55 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 3,丢失 = 1 (25% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 275ms,最长 = 627ms,平均 = 494ms

已经可以ping通,不过貌似不是很稳定,两次ping测试有一次丢掉了一个包。

上一篇:linux下实现shell脚本自动连接mongodb数据库并创建索引


下一篇:jqueryUI弹出框问题