Ok so I found a pseudo solution that kind of works but needs some cleaning up. I'm very much a noob when it comes to Python and especially subprocesses. If anyone can provide some clarity it would be greatly appreciated.
Issues with this code:
1.) It doesn't exit the code when it gets to p6. p6 executes because it disconnects the wifi and reconnects to the correct ssid but it does not move past the p6 line. I added a print statement after p6 to see if it would print out and it would not print. How do you exit a subprocess?
2.) When I physically disconnect from the wifi the wpa_supplicant.conf file only contains the header information and removes the "network {}" block association. When I execute the code below it appends 2 identical "network {}" blocks to the wpa_supplicant.conf file and DOES re-establish a network connection. Im just unsure why it appends it twice.
3.) Any suggestions how to streamline this code? I know its really ugly but it works and I have yet to get any responses on how to accomplish this in another manner?
4.) The wifi ssid I am connecting to does not have a password associated (work guest wifi with captive portal, using pyautogui to click through the captive portal) with it so wpa_passphrase is not an adequate solution to this problem.
Code: Select all
import subprocess
ssid="mySSID"
key_mgmt=None
scanWifi = "sudo wpa_cli scan"
addNtwk= "sudo wpa_cli add_network"
subprocess.Popen(scanWifi.split())
p1 = subprocess.Popen(addNetwk.split(), stdout=subprocess.PIPE)
setSSID = "sudo wpa_cli set_network {0} ssid {1}".format(p1.stdout, ssid)
setKeyMgmt = "sudo wpa_cli set_network {0} key_mgmt {1}".format(p1.stdout, key_mgmt)
enableNtwk = "sudo wpa_cli enable_network {0}".format(p1.stdout)
saveConfig = "sudo wpa_cli save_config"
reconfigNtwk = "sudo wpa_cli -i wlan0 reconfigure"
p2 = subprocess.Popen(setSSID.split())
p3 = subprocess.Popen(setKeyMgmt.split())
p4 = subprocess.Popen(enableNtwk.split())
p5 = subprocess.Popen(saveConfig.split())
p6 = subprocess.Popen(reconfigNtwk.split())
]
Thanks in advance!