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

13,272

Solution 1

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.

Solution 2

I just hit this issue. In my case, I went into the debugger and experimented with the 'ssh' object. This is what I saw:

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'

If anyone else encounters this issue, then I would first make certain that you're not seeing an error elsewhere. In my case, it's a server account with an expired password. That issue caused a cascade of issues which manifested in the NoneType error referenced in the question.

Share:
13,272
adinanp
Author by

adinanp

Updated on July 26, 2022

Comments

  • adinanp
    adinanp almost 2 years

    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?