我很抱歉如果这是一个重复问题,我尝试搜索网页,但大多数人使用sudo。执行subprocess.Popen时挂起('su',shell = True)
但是,我不能使用sudo,我可以使用'su'以root身份登录。我正在执行以下代码:
try:
p_su = subprocess.Popen('su', stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
out_su, err_su = p_su.communicate()
# >>> The program hangs here. <<<
except:
print "Unable to login as root (su). Consult the Software Engineer."
sys.exit()
print out_su
if "Password" in out_su:
try:
p_pw = subprocess.Popen('password', stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
out_pw, err_pw = p_pw.communicate()
except:
print "Unable to login as root (password). Consult the Software Engineer."
sys.exit()
在上面提到的点上,程序至少停留30分钟以上。当我在Linux终端上运行“su”时,需要一两秒钟,有时候会少一点。
它'_yybe_挂起为'su'尝试在控制台(_tty_)上与用户交互?另一方面,在我的Linux系统上,当_stdin_没有连接到_tty_:'echo echo | su' =>'su:必须从终端运行# – 2014-08-28 14:18:03
您可能需要为此使用['pexpect'](http://pexpect.readthedocs.org/en/latest/)。 – dano 2014-08-28 14:19:16
pexpect是否带有Python-2.7.5?如果没有,那么我就不能使用它。 – Everlight 2014-08-28 14:21:35