Through a lot of searching and playing with snippets found on stackoverflow, I was able to create the following code that allows me to, using a Python GUI, launch a shell script as sudo with a password input through the GUI, and then not wait for the shell to return, allowing a progress bar to update as the shell script runs.
build_script = ["Sudo -S sh dummyfile.sh"]
sudo_password = 'password'
sp = subprocess.Popen(build_script, shell=True, stdin=subprocess.PIPE, universal_newlines=True)
sp.stdin.write(sudo_password+'\n')
sp.stdin.flush()
If, the password is entered properly the first time, the tool runs fine. However, if the password is entered incorrectly, then entered correctly it fails and the GUI needs to be closed then reopened.
How can I go about validating if the password was accepted by:
sp.stdin.write(sudo_password+'\n')
So that I can return an error back to the user.
I do understand that there are security risks associated with what I am attempting. I am open to suggestions that will allow me to:
- Validate the password is correct
- Launch a sh file as Sudo with a user input password
- Continue running the python (don't wait) while the sh script runs
It needs to be compatible with Python 2.7, but I am also trying to keep it working in Python 3 for the future.
Thank you for your help!