I'm trying to add a wifi network on the raspberry pi via bash inside a python script.

I want to run a subprocess in administrator mode inside a python application, does anyone have any suggestions to help me?

I need to edit the file:


The entire command I need to execute is:

sudo echo "network={ssid="rede" psk="pass" key+mgmt=WPA-PSK}" >> /etc/wpa_supplicant/wpa_supplicant.conf

I was able to find an answer, I don't know if it is the right one for the application, but it worked.

import subprocess

cmd_permission="sudo chmod 777 /etc/wpa_supplicant/wpa_supplicant.conf"
subprocess.run(cmd_permission, shell=True)
cmd="sudo echo "network={ssid="rede" psk="pass" key+mgmt=WPA-PSK}" >> /etc/wpa_supplicant/wpa_supplicant.conf"
subprocess.run(cmd, shell=True)

Security Considerations Unlike some other popen functions, this implementation will never implicitly call a system shell. This means that all characters, including shell metacharacters, can safely be passed to child processes. If the shell is invoked explicitly, via , it is the application’s responsibility to ensure that all whitespace and metacharacters are quoted appropriately to avoid shell injection vulnerabilities.shell=True

When using , the shlex.quote() function can be used to properly escape whitespace and shell metacharacters in strings that are going to be used to construct shell commands.shell=True


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.