I have to open a terminal using sudo
from python. Consider my password is pass
and I need to run a command within script which is sudo critical-stack-intel pull
.
I have following small piece of code:
import subprocess
import shlex
command = "sudo critical-stack-intel pull"
popen = subprocess.Popen(shlex.split(command),stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
popen.communicate("pass")
popen.wait()
# print help(p)
If i run the file as python myfile.py
, it asks me for password within terminal. This is not what I desire. I want the python to handle the password I gave and run normally. How do I get this done?
EDIT
Using popen.communicate(pass + "\n")
along with sudo -S
did what i desired.