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

时间:2013-11-30 19:42:48

标签: 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")

我收到以下错误:

    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'

但是,在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")

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

2 个答案:

答案 0 :(得分:1)

我有同样的事情。我发现当我在之前的代码中关闭我的客户端时发生了这种情况。我不知道这是不是你的问题,但是错误不是很有用。

答案 1 :(得分:1)

我刚刚遇到这个问题。在我的情况下,我进入调试器并试验了&#39; ssh&#39;宾语。这就是我所看到的:

ipdb> ssh.connect(...omit details...)
ipdb> resp = ssh.exec_command('echo hello')
ipdb> rest[1].read()   ## read from STDOUT, expected 'hello'
''                     ##   but, I got ''
ipdb> resp[2].read()   ## read from STDERR
'WARNING: Your password has expired.\nPassword change required but no TTY available.\n'

如果其他人遇到此问题,那么我首先要确定您在其他地方没有看到错误。就我而言,它是一个密码过期的服务器帐户。该问题引发了一系列问题,这些问题表现在问题中引用的NoneType错误中。