mattg31
Posts: 55
Joined: Fri Jan 05, 2018 9:55 pm

changing network settings via python subcommand

Thu Jun 21, 2018 10:49 pm

I am trying to connect to a wifi network via a python script using a Raspberry Pi 3 Model B+, running latest version of raspbian.
I have found that I can do this from the terminal using:

Code: Select all

wpa_passphrase "testing" "testingPassword" | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null
So I figured I could use the subprocess library in python:

Code: Select all

import subprocess

ssid = '"testing"'
passkey='"testingPassword"'

command = ["wpa_passphrase", ssid, passkey, "|", "sudo", "tee", "-a", "/etc/wpa_supplicant/wpa_supplicant.conf", ">", "/dev/null"]

subprocess.run(command)
But all this does is output (in the terminal):

Code: Select all

network={
	ssid=""testing""
	#psk=""testingPassword""
	psk=3f446508c00cab57863ae62c8aaa505a4fae5d73461a0f10aee09988be8a2ae6
}


------------------
(program exited with code: 0)
Press return to continue
It does not actually append the string to wpa_supplicant.conf
Any idea what I'm doing wrong?

User avatar
topguy
Posts: 4996
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: changing network settings via python subcommand

Fri Jun 22, 2018 10:49 am

I have a feeling you need to run the command in a shell to get the piping working.

Code: Select all

subprocess.run(command, shell=True)

mattg31
Posts: 55
Joined: Fri Jan 05, 2018 9:55 pm

Re: changing network settings via python subcommand

Sat Jun 23, 2018 12:56 am

Thanks for the reply. When I run with shell I get:

Code: Select all

usage: wpa_passphrase <ssid> [passphrase]

If passphrase is left out, it will be read from stdin

User avatar
DougieLawson
Posts: 34166
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: changing network settings via python subcommand

Sat Jun 23, 2018 8:41 am

Code: Select all

import subprocess
ssid="MickeyMouse"
passkey="MinnieMouse"
p1 = subprocess.Popen(["wpa_passphrase", ssid, passkey], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["sudo","tee","-a","/etc/wpa_supplicant/wpa_supplicant.conf",">","/dev/null"], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()  # Give p1 a SIGPIPE if p2 dies.
output,err = p2.communicate()
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

mattg31
Posts: 55
Joined: Fri Jan 05, 2018 9:55 pm

Re: changing network settings via python subcommand

Sat Jun 23, 2018 12:29 pm

@DougieLawson you are the best!
That worked. Thanks!

Nate102
Posts: 6
Joined: Fri Apr 06, 2018 8:21 pm

Re: changing network settings via python subcommand

Mon Nov 12, 2018 8:32 pm

How would you go about accomplishing this if there is no passkey on the discrete SSID? As far as I know wpa_passphrase would not work because it requires a 6-13 digit password. Per the Pi documentation, if there is no password on the SSID you need to add in 'key_mgmt=NONE' in leiu.

Thanks

Nate102
Posts: 6
Joined: Fri Apr 06, 2018 8:21 pm

Re: changing network settings via python subcommand

Tue Nov 13, 2018 6:38 pm

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!

Return to “Python”