AttributeError: 'NoneType' object has no attribute 'open_session'"

adinanp picture adinanp · Nov 30, 2013 · Viewed 11.2k times · Source

When running the following function:

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")

I received the following error:

    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'

However, when running in python shell the following commands:

>>> 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")

Everything went as expected, only when I transform it in the above function in a file.py is that the error occurs. Does anyone have any idea what it might be?

Answer

da_steve101 picture da_steve101 · Feb 7, 2014

I had the same thing. I found out it occurred when I closed my client in some previous code. I don't know if that is/was your issue but the error wasn't very informative.