AttributeError:'NoneType'对象没有属性'open_session'"

adi*_*anp 6 python ssh paramiko attributeerror

运行以下功能时:

def conEnclosure():
    freebay = open("freebay", "w+")
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    password = getpass.getpass("Enter the password for infra: ")

    for host in open("full.json", "r"):
        print host
        ssh.connect("%s" % host, username="infra", password="%s" % password)
        stdin, stdout, stderr = ssh.exec_command("show server info all")
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

    Traceback (most recent call last):
  File "./findbay_v2.py", line 53, in <module>
    conEnclosure()
  File "./findbay_v2.py", line 41, in conEnclosure
    ssh.exec_command("show server info all")
  File "build/bdist.macosx-10.9-intel/egg/paramiko/client.py", line 364, in exec_command
AttributeError: 'NoneType' object has no attribute 'open_session'
Run Code Online (Sandbox Code Playgroud)

但是,在python shell中运行时,请执行以下命令:

>>> import paramiko
>>> ssh = paramiko.SSHClient()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect("host", username="xxx", password="xxx")
>>> stdin, stdout, stderr = ssh.exec_command("show server info all")
Run Code Online (Sandbox Code Playgroud)

一切都按预期进行,只有当我在file.py中的上述函数中对其进行转换时才会发生错误.有谁知道它可能是什么?

da_*_*101 2

我也有同样的事情。当我在以前的一些代码中关闭客户端时,我发现它发生了。我不知道这是否是您的问题,但该错误并没有提供太多信息。