如何让我的 python 应用程序在继续之前等待某个子进程
我在 python 脚本中生成一个 ssh 进程,如下所示:
env = {'SSH_ASKPASS':'/home/max/repo/vssh/vssh/vssh.py', 'DISPLAY':':123'}
for x in sessions:
current_session = Popen(["ssh", x.address],
stdin=PIPE,
stdout=PIPE,
stderr=None,
shell=False,
env=env,
preexec_fn=os.setsid)
问题是我的脚本启动子进程,然后继续运行。 ssh
需要为其提供密码,这就是我试图重定向到脚本的 STDOUT/STDIN 的密码。除了当它询问我的密码时,我的脚本已经完成。
如何让我的脚本停止,直到我输入密码并通过 ssh
成功建立连接?
I'm spawning an ssh
process in my python script like this:
env = {'SSH_ASKPASS':'/home/max/repo/vssh/vssh/vssh.py', 'DISPLAY':':123'}
for x in sessions:
current_session = Popen(["ssh", x.address],
stdin=PIPE,
stdout=PIPE,
stderr=None,
shell=False,
env=env,
preexec_fn=os.setsid)
The problem is my script starts the subprocess, then keeps on going. ssh
requires a password to be given to it, which is what I'm trying to redirect to the STDOUT/STDIN of my script. Except that by the time it asks for my password, my script is done.
How can I make my script stop until I enter the password and successfully set up a connection via ssh
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它确实从标准输入读取,那么您可以使用沟通方法。但密码提示通常会绕过标准输入并与控制终端进行通信,在这种情况下,您需要类似 pexpect。
您可能想要研究 Python 的 SSH 模块,它应该比管理子进程更简单、更健壮。
If it's really reading from stdin, then you can use the communicate method. But password prompts often bypass stdin and communicate with the controlling terminal, in which case you need something like pexpect.
You might want to look into SSH modules for Python, which should be simpler and more robust than managing a subprocess.