Skip to content

Instantly share code, notes, and snippets.

@perseoq
Forked from aeroaks/sudoSample.py
Created July 27, 2021 01:57
Show Gist options
  • Save perseoq/abf531904fc303b85828412d8b3200f1 to your computer and use it in GitHub Desktop.
Save perseoq/abf531904fc303b85828412d8b3200f1 to your computer and use it in GitHub Desktop.
Run Sudo command using Python
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