Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 427
adinanp Avatar asked Nov 30 '13 19:11

adinanp


2 Answers

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.

like image 171
da_steve101 Avatar answered Sep 21 '22 02:09

da_steve101


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.

like image 35
sanscore Avatar answered Sep 19 '22 02:09

sanscore