Paramiko "TypeError: 'NoneType' object is not callable" at the end of a script that execute command on the server

Paramiko "TypeError: 'NoneType' object is not callable" at the end of a script that execute command on the server

我正在编写一个脚本来在 EC2 实例中执行命令,然后再回来。为此,我正在使用 Paramiko。我的脚本是这样的:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('<hostname>', username='<username>', password='<password>')
print('connected')
stdin, stdout, stderr = ssh.exec_command("ls -lah")
ssh.close()
print('done')

我得到的轨迹是:

connected
done
Exception ignored in: <function BufferedFile.__del__ at 0x7fa5de814950>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/file.py", line 66, in __del__
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/channel.py", line 1392, in close
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/channel.py", line 991, in shutdown_write
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/channel.py", line 963, in shutdown
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/channel.py", line 1246, in _send_eof
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/message.py", line 232, in add_int
TypeError: 'NoneType' object is not callable

我用来登录的命令是:

ssh -o PubkeyAuthentication=no -l <username> <hostname>

谁能告诉我我做错了什么?

在关闭 SSH 连接之前,您没有等待命令完成。

当 Python 垃圾收集 stdinstdoutstderr 时,Paramiko 崩溃,因为它不希望连接关闭。

有关正确的代码,请参阅 Wait to finish command executed with Python Paramiko