gpt4 book ai didi

Python try and except 在启动无密码 ssh 连接时

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:09 25 4
gpt4 key购买 nike

我想知道这种使用 ssh 的情况是否适用于 try 和 except。如果 try 部分要求输入 ssh 密码,那么它应该移至 except 部分。这是我的代码,

 import os,sys,subprocess

def ProcesS (input):
print input
A = subprocess.Popen (input,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output,error = A.communicate()
return output,error

try:
ProcesS('ssh system1@123.123.123.123 \'ls\'')
except:
ProcesS('ssh system2@10.101.10.1 \'ssh system1@123.123.123.123 \'ls\' \'')

在我的例子中,这甚至在 ssh 超时时都不起作用。

最佳答案

正如您所写的那样,它不会,因为 ssh 会坐在那里等待密码并且不会失败。

然而,由于您没有向命令发送任何输入,您可以将 -n 选项传递给 ssh,如果它想询问任何内容,它将失败。所以使用 -n 它应该可以工作。

关于Python try and except 在启动无密码 ssh 连接时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23990314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com