python3子流程popen将密码发送到sudo并获取流程的结果
我有一个PYQT5程序,其中使用QinputDialog窗口获取某些PCI操作的用户密码。我看不到弄清楚如何获得由sudo运行的命令的输出。
john@d10shop:~/bin$ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import Popen, PIPE
>>> password = 'xyz'
>>> p = Popen(['sudo', '-S', 'ls'], stdin=PIPE, stderr=PIPE, text=True)
>>> prompt = p.communicate(password + '\n')
camera imap-1.py pass.py
>>> prompt
(None, '[sudo] password for john: ')
>>>
我可以看到命令已执行,但我获得了约翰的[sudo]密码:但是我需要在此示例中从LS输出。
OS Debian 10
谢谢 JT
I have a PyQt5 program where I get the users password for some PCI operations with a QInputDialog window. I can't seen to figure out how to get the output of the command that is ran by sudo.
john@d10shop:~/bin$ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import Popen, PIPE
>>> password = 'xyz'
>>> p = Popen(['sudo', '-S', 'ls'], stdin=PIPE, stderr=PIPE, text=True)
>>> prompt = p.communicate(password + '\n')
camera imap-1.py pass.py
>>> prompt
(None, '[sudo] password for john: ')
>>>
I can see the command is executed but I get the [sudo] password for john: but I need the output from ls in this example.
OS Debian 10
Thanks
JT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 python docs :
您缺少
stdout =管道
零件以获取输出。这对我有用:
From the python docs:
You are missing the
stdout=PIPE
part to get the output.This works for me: