Last active
April 8, 2025 05:56
-
-
Save aeroaks/f6150bd0add14bdbc244 to your computer and use it in GitHub Desktop.
Run Sudo command using Python
This file contains hidden or 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) |
Rajan-sust
commented
Sep 10, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment