【问题标题】:Catch AuthenticationException in Python Paramiko在 Python Paramiko 中捕获 AuthenticationException
【发布时间】:2018-09-24 02:09:13
【问题描述】:
import paramiko

host='x.x.x.x'
port=22
username='root'
password='password'

cmd='dmidecode > a' 

ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host,port,username,password)
try:
    stdin,stdout,stderr=ssh.exec_command(cmd)
    outlines=stdout.readlines()
    resp=''.join(outlines)
    print(resp)
except paramiko.AuthenticationException as error:
    print "ERROR"

我无法接听AuthenticationException。 谁能建议我任何其他不破坏脚本并只显示错误的方法?

【问题讨论】:

  • 发布回溯错误可能会有所帮助。另外,我建议将 stdin,stdout,stderr=ssh.exec_command(cmd) 放在 try 块中,因为其他代码行不会发生该异常。

标签: python ssh exception-handling try-catch paramiko


【解决方案1】:

AuthenticationException 发生在SSHClient.connect

引发AuthenticationException – 如果身份验证失败

您的SSHClient.connect 呼叫不在您的try 块中。

这应该可行:

try:
    ssh.connect(host,port,username,password)
    stdin,stdout,stderr=ssh.exec_command(cmd)
    outlines=stdout.readlines()
    resp=''.join(outlines)
    print(resp)
except paramiko.AuthenticationException as error:
    print "ERROR"

【讨论】:

    猜你喜欢
    • 2011-05-07
    • 2016-08-28
    • 1970-01-01
    • 2017-02-26
    • 2018-01-28
    • 1970-01-01
    • 2012-06-29
    • 2019-12-03
    • 1970-01-01
    相关资源
    最近更新 更多