如果检测到错误密码,wpa_supplicant 结束进程

如果检测到错误密码,wpa_supplicant 结束进程

通常,当我运行时,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不正确的密钥。使用grepon wpa_supplicantstdbuf基于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进程来终止脚本。

相关内容