I have a Python/Flask web app on a Raspberry Pi that calls the following bash script (connect_to_wifi) to connect to WiFi:
sudo killall wpa_supplicant
sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
sudo dhclient wlan0
Basically, a user enters their WiFi credentials, which are saved in wpa_supplicant.conf
, and then this script is run. It works great...however, if they mistype their credentials, dhclient hangs forever before failing.
What I want to do is detect if the credentials are correct before proceeding with dhclient. I know that I can check the output of the wpa_supplicant
command for a 4-Way Handshake failure if creds are wrong, but when I call this script from my Python app via:
p = Popen(['connect_to_wifi'], stdout=PIPE, bufsize=1)
with p.stdout:
for line in iter(p.stdout.readline, b''):
print line,
p.wait()
None of the output from the sudo wpa_supplicant -i...
line is being captured. All I want to do is end immediately if I detect a handshake failure, but I'm having trouble capturing the output in Python.