Around 20% of the time, my script which uses paramiko for a simple ssh command:
ssh = SSHClient()
ssh.load_system_host_keys()
try:
ssh.connect(machine, username=user, password=password)
stdin, stdout, stderr = ssh.exec_command(commande)
return_code = stdout.channel.recv_exit_status()
out = stdout.read().decode('utf-8')
err = stderr.read().decode('utf-8')
if out.strip():
print(out.strip())
if err.strip():
print(err.strip())
print("CR=" + str(return_code))
rc = 0
except SSHException as e:
print(e)
rc = -2
finally:
ssh.close()
sys.exit(cr)
Spews out those exceptions:
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/home/user/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 62, in __del__
File "/home/user/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 80, in close
File "/home/user/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 89, in flush
TypeError: 'NoneType' object is not callable
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/home/user/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 62, in __del__
File "/home/user/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 80, in close
File "/home/user/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 89, in flush
TypeError: 'NoneType' object is not callable
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/home/user/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 62, in __del__
File "/home/user/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 80, in close
File "/home/user/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 89, in flush
TypeError: 'NoneType' object is not callable
This does look like #17, but does not occur in the same context. If I remove sys.exit from my script, the error does not seem to occur anymore (or maybe at a greatly reduced rate, which caused me not to observe it after 50 runs or so). When the error occurs, the ssh command seems to have run correctly.
Update
The error does not occur when the program is not exited with sys.exit. So I tried wrapping my whole script in a function, calling that function and then calling sys.exit so that the latter does not run in the same scope as the paramiko SSHClient, and magic, the error seems to have vanished.
This is still an issue in the current version.
still seeing same issue as above.
Exception ignored in:
Traceback (most recent call last): File "C:Users\ts\PycharmProjects\tzx_learning\venv\lib\site-packages\paramiko\file.py", line 66, in __del__ File "C:Users\ts\PycharmProjects\tzx_learning\venv\lib\site-packages\paramiko\channel.py", line 1392, in close File "C:Users\ts\PycharmProjects\tzx_learning\venv\lib\site-packages\paramiko\channel.py", line 991, in shutdown_write File "C:Users\ts\PycharmProjects\tzx_learning\venv\lib\site-packages\paramiko\channel.py", line 963, in shutdown File "C:Users\ts\PycharmProjects\tzx_learning\venv\lib\site-packages\paramiko\channel.py", line 1246, in _send_eof File "C:Users\ts\PycharmProjects\tzx_learning\venv\lib\site-packages\paramiko\message.py", line 232, in add_int TypeError: 'NoneType' object is not callable
New to paramiko, getting the same error and traceback as @STejas91, but I'm not using sys. _send_eof File made me wonder if its related to https://github.com/paramiko/paramiko/pull/1585.
Same problem
paramiko==2.7.1
Python 3.7.5
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.5', username='USER', password='PASSWORD')
stdin, stdout, stderr = ssh.exec_command("sudo ifconfig")
stdout_str = stdout.read().decode('utf-8')
print(stdout_str)
ssh.close()
I also am seeing this issue here
LOG:
Exception ignored in: <function BufferedFile.__del__ at 0x7f7e7b5e1d08>
Traceback (most recent call last):
File "/home/local_username/flexget/lib/python3.7/site-packages/paramiko/file.py", line 66, in __del__
File "/home/local_username/flexget/lib/python3.7/site-packages/paramiko/channel.py", line 1392, in close
File "/home/local_username/flexget/lib/python3.7/site-packages/paramiko/channel.py", line 991, in shutdown_write
File "/home/local_username/flexget/lib/python3.7/site-packages/paramiko/channel.py", line 963, in shutdown
File "/home/local_username/flexget/lib/python3.7/site-packages/paramiko/channel.py", line 1246, in _send_eof
File "/home/local_username/flexget/lib/python3.7/site-packages/paramiko/message.py", line 232, in add_int
TypeError: 'NoneType' object is not callable
Guys, i found that the IP i was using had network connection issue and once i fixed that my code started working just fine. I would suggest you all to cross check the IP's and access you guys are using to connect.
Problem remains. No problem with the network connection (@STejas91) since I got the output.
Program is:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=PRODUCTION_SERVER, port=22, username=PRODUCTION_USER)
stdin, stdout, stderr = client.exec_command('echo "Hello World"')
for line in stdout:
print(line.strip('\n'))
client.close()
Output:
Hello World
Exception ignored in: <function BufferedFile.__del__ at 0x106dff378>
Traceback (most recent call last):
File "/Users/bert/pytest/venv/lib/python3.7/site-packages/paramiko/file.py", line 66, in __del__
File "/Users/bert/pytest/venv/lib/python3.7/site-packages/paramiko/channel.py", line 1392, in close
File "/Users/bert/pytest/venv/lib/python3.7/site-packages/paramiko/channel.py", line 991, in shutdown_write
File "/Users/bert/pytest/venv/lib/python3.7/site-packages/paramiko/channel.py", line 963, in shutdown
File "/Users/bert/pytest/venv/lib/python3.7/site-packages/paramiko/channel.py", line 1246, in _send_eof
File "/Users/bert/pytest/venv/lib/python3.7/site-packages/paramiko/message.py", line 232, in add_int
TypeError: 'NoneType' object is not callable
@BertCatsburg Are you able to connect to the host via ssh on terminal ?
and hope you are passing password parameter for "client.connect".
@STejas91 Yes, I can connect via terminal. Password not necessary since I have the Public/Private key pair set up between client and host. Logging in via terminal is therefore by doing:
$ ssh user@host
@BertCatsburg If you are using passwordless authentication try below, this should work.
privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
ssh.connect(IP, username = user, pkey = mykey)
@BertCatsburg I was getting exactly the same error. For me it was a garbage collector error so, in order to work around this I did my own deletion:
if client is not None:
client.close()
del client, stdin, stdout, stderr
Hope this helps.
My experience shows that deleting just stdin is enough. But clearly it has to be fixed in the library.
@RafaARV @rgezikov Anecdotally, looks like just setting stdin to None works as well.
For what it's worth, I also have this same issue (Paramiko 2.7.1 and Python 3.6.9), and it seems that deleting stdin suffices to suppress the error message. Tough to say for certain because the TypeError is irregular and does not appear every time I execute the Python script.
import paramiko, getpass
client = paramiko.SSHClient()
client.load_system_host_keys()
password = getpass.getpass("Password: ")
client.connect('192.168.0.10', username='USER', password=password)
# test.exe is a simple C++ program on a Windows machine. It reads floats,
# adds them to a variable, and prints the current value of that variable.
stdin, stdout, stderr = client.exec_command('.\\Desktop\\test.exe')
stdin.write('5\n')
stdin.write('10\n')
stdin.write('-3.5\n')
stdin.write('-4.5\n')
stdin.write('-1\n')
for line in stdout.readlines():
print(line[:-1])
print(stderr.readlines())
print(stdout.channel.recv_exit_status())
client.close()
# del stdin
Still occurs paramiko 2.7.2
del stdin helps
Most helpful comment
@BertCatsburg I was getting exactly the same error. For me it was a garbage collector error so, in order to work around this I did my own deletion:
Hope this helps.