您的当前位置:首页linux下使用shell命令通过wpa_cli控制wpa_supplicant连接wifi

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

2022-07-04 来源:爱问旅游网
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/wlan0eapol_version=1ap_scan=1fast_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 and contributorsThis 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:1eaddress=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显⽰扫描的结果

> scanOK

<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_5G5e:ea:1d:8b:2b:58 5745 -86 [WPA2-PSK-CCMP][WPS][ESS] DIRECT-58-HP M130 LaserJetf2: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_Guestdc: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_Oc0:21:0d:45:10:c1 5260 -76 [ESS] X-Link 10C1f4:28:53:a6:ed:f1 5220 -90 [ESS] PIX-LINK_5G2a: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_network1

= set network variables (shows list of variables when run without arguments)

= set network variables (shows list of variables when run without arguments)

= 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 1OK

<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:31ssid=AirFlyid=1

mode=station

pairwise_cipher=CCMPgroup_cipher=TKIPkey_mgmt=WPA2-PSKwpa_state=COMPLETED

p2p_device_address=7c:dd:90:ec:f2:1eaddress=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 ifconfiglo 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 -iwlan0udhcpc (v1.20.0) startedSending 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.55root@p1:/ # busybox-smp ifconfiglo 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=64192.168.11.55 的 Ping 统计信息:

数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),往返⾏程的估计时间(以毫秒为单位):

最短 = 18ms,最长 = 31ms,平均 = 24msC:\\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测试有⼀次丢掉了⼀个包。

因篇幅问题不能全部显示,请点此查看更多更全内容