Linux(树莓派)系统中判断WiFi是否连接上路由器的方法

之前(https://blog.csdn.net/u010299133/article/details/105823339)介绍过在Linux系统中使用wpa_supplicant连接到指定的WiFi路由器的方法,现在需要判断是否连接上路由器,以及如何获取因为WiFi密码错误造成的无法连接路由器的方法。大致思路是在使用wpa_suppliant工具连接WiFi路由器的时候使用参数(-f )指定一个log文件存放的路径文件。如:-f /tmp/wpa.log。当执行完wpa_suppliant(sudo wpa_supplicant -B -D wext -f /tmp/wpa.log -c /etc/wpa_suppl.conf -i wlan0)后,会将log信息存放在/tmp/wpa.log中,然后应用程序就打开这个文件并且解析里面的内容,从而获取到WiFi的连接状态。

注意:这种方法的时效性比较差,一般执行完5、6秒钟后,才会生成完整的log信息。

正常连接路由器的wpa.log信息:

Successfully initialized wpa_supplicant
rfkill: Cannot get wiphy information
ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument
wlan0: Trying to associate with 9c:a6:15:a0:0d:5a (SSID='TP-LINK' freq=2462 MHz)
Failed to add supported operating classes IE
wlan0: Association request to the driver failed
wlan0: Associated with 9c:a6:15:a0:0d:5a
wlan0: WPA: Key negotiation completed with 9c:a6:15:a0:0d:5a [PTK=CCMP GTK=CCMP]
wlan0: CTRL-EVENT-CONNECTED - Connection to 9c:a6:15:a0:0d:5a completed [id=0 id_str=]

从中可以获取到关键信息:wlan0: CTRL-EVENT-CONNECTED - Connection to 9c:a6:15:a0:0d:5a completed [id=0 id_str=]。代表正常连接到路由器。

 

如果WiFi的密码输入有误,那么wpa.log的信息:

ioctl[SIOCSIWAP]: Operation not permitted
wlan0: CTRL-EVENT-DISCONNECTED bssid=9c:a6:15:a0:0d:5a reason=3 locally_generated=1
ioctl[SIOCSIWAP]: Operation not permitted
wlan0: CTRL-EVENT-TERMINATING 
Successfully initialized wpa_supplicant
rfkill: Cannot get wiphy information
ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument
wlan0: Trying to associate with 9c:a6:15:a0:0d:5a (SSID='TP-LINK' freq=2462 MHz)
Failed to add supported operating classes IE
wlan0: Association request to the driver failed
wlan0: Associated with 9c:a6:15:a0:0d:5a
wlan0: Authentication with 9c:a6:15:a0:0d:5a timed out.
ioctl[SIOCSIWAP]: Operation not permitted
wlan0: CTRL-EVENT-DISCONNECTED bssid=9c:a6:15:a0:0d:5a reason=3 locally_generated=1
wlan0: WPA: 4-Way Handshake failed - pre-shared key may be incorrect
wlan0: CTRL-EVENT-SSID-TEMP-DISABLED id=0 ssid="TP-LINK" auth_failures=1 duration=10 reason=WRONG_KEY
wlan0: CTRL-EVENT-SSID-REENABLED id=0 ssid="TP-LINK"
wlan0: Trying to associate with 9c:a6:15:a0:0d:5a (SSID='TP-LINK' freq=2462 MHz)
Failed to add supported operating classes IE
wlan0: Association request to the driver failed
wlan0: Associated with 9c:a6:15:a0:0d:5a
wlan0: Authentication with 9c:a6:15:a0:0d:5a timed out.
ioctl[SIOCSIWAP]: Operation not permitted
wlan0: CTRL-EVENT-DISCONNECTED bssid=9c:a6:15:a0:0d:5a reason=3 locally_generated=1
wlan0: WPA: 4-Way Handshake failed - pre-shared key may be incorrect
wlan0: CTRL-EVENT-SSID-TEMP-DISABLED id=0 ssid="TP-LINK" auth_failures=2 duration=26 reason=WRONG_KEY
wlan0: CTRL-EVENT-SSID-TEMP-DISABLED id=0 ssid="TP-LINK" auth_failures=3 duration=36 reason=CONN_FAILED

定位关键信息:

wlan0: WPA: 4-Way Handshake failed - pre-shared key may be incorrect
或者:
reason=WRONG_KEY

可以发现连不上路由器是因为密码的错误。

 

 

 

你可能感兴趣的:(WiFi,shell,linux工具使用,wifi,linux,raspberry,pi)