在Python Parami中捕获AuthenticationException

2024-09-26 17:49:42 发布

您现在位置:Python中文网/ 问答频道 /正文

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"

{cdm>无法捕获^'。 有人能给我建议其他方法来避免破坏脚本只显示错误吗?在


Tags: importcmdhostparamikoportstdoutusernameroot
1条回答
网友
1楼 · 发布于 2024-09-26 17:49:42

^{}发生在^{}

Raises: AuthenticationException – if authentication failed

并且您的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"

相关问题 更多 >

    热门问题