通常,當我運行時,wpa_supplicant
我會得到以下輸出:
Successfully initialized wpa_supplicant
ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument
wlan3: Trying to associate with 9c:3d:cf:fb:95:96 (SSID='Bell420' freq=2462 MHz)
wlan3: Association request to the driver failed
wlan3: Associated with 9c:3d:cf:fb:95:96
wlan3: Authentication with 9c:3d:cf:fb:95:96 timed out.
ioctl[SIOCSIWAP]: Operation not permitted
wlan3: CTRL-EVENT-DISCONNECTED bssid=9c:3d:cf:fb:95:96 reason=3 locally_generated=1
wlan3: WPA: 4-Way Handshake failed - pre-shared key may be incorrect
問題是它只是不斷地嘗試。
有沒有一種方法可以讓我wpa_supplicant
在出現明顯錯誤(例如錯誤的按鍵)時立即退出?
我使用的是較舊的嵌入式設備,帶有wpa_supplicant v2.1
.
我寫了一個解決方法來監視wpa_supplicant
不正確的密鑰。使用grep
on wpa_supplicant
(stdbuf
基於Stéphane Chazelas 的評論在這裡):
# Create conf file with ssid and password
wpa_passphrase "$ssid" "$password" > /etc/wpa_supplicant/wpa_supplicant.conf
# If wifi key is wrong kill subshell
subshell=$BASHPID
(sudo stdbuf -o0 wpa_supplicant -Dwext -iwlan1 -c/etc/wpa_supplicant/wpa_supplicant.conf 2>&1 \
| grep -m 1 "pre-shared key may be incorrect" \
&& kill -s PIPE "$subshell") &
注意:上面的區塊位於腳本的子 shell 內。
它最初似乎有效,但隨著時間的推移,我發現有時當密碼正確時它會導致整個腳本失敗。其他時候它會按預期工作。
一定有更好的方法來做到這一點。
編輯:也許我應該wpa_cli
在這裡使用?
答案1
#!/bin/bash
# SSID and password
ssid="YourSSID"
password="YourPassword"
# Create wpa_supplicant.conf
wpa_passphrase "$ssid" "$password" > /etc/wpa_supplicant/wpa_supplicant.conf
# Run wpa_supplicant in the background
wpa_supplicant -Dwext -iwlan1 -c/etc/wpa_supplicant/wpa_supplicant.conf > /tmp/wpa_supplicant.log 2>&1 &
# Monitor log file for error messages
tail -f /tmp/wpa_supplicant.log | while read -r line; do
if [[ $line == *"pre-shared key may be incorrect"* ]]; then
echo "Incorrect key detected. Exiting..."
kill $!
fi
done
wpa_supplicant
在背景運行,其輸出被重定向到日誌檔案。此tail
命令會連續讀取日誌檔案並檢查指定的錯誤訊息。如果發現錯誤訊息,它會列印一條訊息並透過終止wpa_supplicant
進程來終止腳本。