我必須用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