【发布时间】:2021-02-02 11:47:27
【问题描述】:
我想运行 shell 命令 'su - testuser -c "id"' 并获取输出。之后在控制台中询问密码。我的意图是在 python 脚本中运行它,它登录到另一个用户(源用户和目标用户都没有 root 权限)。问题是,密码应该以非交互方式输入,这样我就可以在不输入密码的情况下启动脚本并查看输出。所以我可以启动 python 脚本,它会自动运行命令而无需等待密码并给我输出。
我用 pexpect 包试过了:
child = pexpect.spawn('su - testuser -c "id"')
child.expect_exact('Password:')
child.sendline('mypassword')
print(child.before) # Prints the output of the "id" command to the console
问题是代码不起作用。输出就像一个随机字符串,而不是 id 等等。我该怎么做?
【问题讨论】:
标签: python linux shell authentication process