我必须用python打开一个使用sudo
的终端。考虑我的密码是pass
,我需要在sudo critical-stack-intel pull
的脚本中运行一个命令。用sudo python打开终端
我有以下小块代码:
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)
如果我运行该文件python myfile.py
,它要求我为终端内的密码。这不是我想要的。我想让python处理我给的密码并正常运行。我如何完成这项工作?
编辑
使用popen.communicate(pass + "\n")
与sudo -S
一起做了什么,我想要的。
'sudo'打开tty,默认情况下它不使用stdin。用'sudo -S'代替 –
看看这个:http://stackoverflow.com/questions/567542/running-a-command-as-a-super-user-from-a-python-script – nablahero
我想要使用'subprocess.Popen'而不是'os.Popen'。 – Pant