处理缺失的SSH连接
为了通过SSH连接,我使用以下代码:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(IP, username=USER, password=PSW,look_for_keys=False)
我的问题是,如何处理丢失的连接? 特别是我想要的,如果没有建立连接,则代码(也许是goto?)到了代码的另一点,我基本上是我重新启动服务器。
In order to connect via SSH I'm using the following code:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(IP, username=USER, password=PSW,look_for_keys=False)
my question is, how can I handle a missing connection?
In particular I'd like that in case the connection is not established the code goes (maybe with a GOTO?) to another point of the code where basically I restart the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地尝试尝试捕获异常
martin-prikryl ,例如:
对于SSH例外的其他示例,您可以查看 https://www.programcreek.com/python/example/example/7495/paramiko.sshexception
You could simply use a try and catch the exception as mentioned by
martin-prikryl, such as:
For other examples of ssh exceptions you can look at https://www.programcreek.com/python/example/7495/paramiko.SSHException