-
-
Save perseoq/abf531904fc303b85828412d8b3200f1 to your computer and use it in GitHub Desktop.
Run Sudo command using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
# run command using Popen and provide password using communicate. | |
# password requires byte format. | |
# sudo -S brings password request to PIPE | |
proc = subprocess.Popen(['sudo', '-S', 'nano', 'test.txt'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate(input=b'password\n') | |
# Print output and errors | |
print(proc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment