AttributeError: 'NoneType' object has no attribute 'time' paramiko
Asked Answered
M

5

16
import paramiko

key = paramiko.RSAKey.from_private_key_file("abc.pem")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print("connecting")
ssh.connect(hostname="1.1.1.1", username="abc", pkey=key)
print("connected")
commands = "ip a"
stdin, stdout, stderr = ssh.exec_command(commands)
print(stdout.read())
print(stderr.read())
print(stdin.read())
ssh.close()

Why have sometime will AttributeError: 'NoneType' object has no attribute 'time' in Python3.8 and sometime need wait long time show as result(or how can i see process)

Error code:

Exception ignored in: <function BufferedFile.__del__ at 0x108271ee0>
Traceback (most recent call last):
  File "/venv/lib/python3.8/site-packages/paramiko/file.py", line 66, in __del__
  File "/venv/lib/python3.8/site-packages/paramiko/channel.py", line 1392, in close
  File "/venv/lib/python3.8/site-packages/paramiko/channel.py", line 991, in shutdown_write
  File "/venv/lib/python3.8/site-packages/paramiko/channel.py", line 967, in shutdown
  File "/venv/lib/python3.8/site-packages/paramiko/transport.py", line 1846, in _send_user_message
AttributeError: 'NoneType' object has no attribute 'time'

Advance

how can i use paramiko double ssh

localhost >> a(server) ssh >> b

Messiah answered 3/2, 2020 at 10:26 Comment(9)
Please all the full error traceback to your question!Jair
Exception ignored in: <function BufferedFile.__del__ at 0x10511fee0> File "/venv/lib/python3.8/site-packages/paramiko/file.py", line 66, in del File "/venv/lib/python3.8/site-packages/paramiko/channel.py", line 1392, in close File "/venv/lib/python3.8/site-packages/paramiko/channel.py", line 991, in shutdown_write File "/venv/lib/python3.8/site-packages/paramiko/channel.py", line 967, in shutdown File "/venv/lib/python3.8/site-packages/paramiko/transport.py", line 1846, in _send_user_message AttributeError: 'NoneType' object has no attribute 'time'Messiah
Please edit your question and add the traceback there, formatted as a code block.Jair
added , thanks your techMessiah
Looking at the code I'd recommend setting a timeout when you run your exec_command. Also I'd recommend running each command line by line in an interpreter to see which part exactly causes the exception - I'm curious as to whether it's during the read of stderr etc and a result of the buffer still being written to whilst you're trying read to it. All else fails, raise a bug report, since its 3.8 and the fact that you're seeing an Exception being ignored and the message in the trace you're getting means it's not caught by any meaningful exception class they provide.Azobenzene
i tried,but just show same errorMessiah
Have you found a solution? I have the same problem.Wellintentioned
no, i dont know how to solve problem,maybe change to use other library?Messiah
I have this problem too. I found that if I added a sleep after command execution the issue doesn't appear. This is my code ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute) time.sleep(0). It seems far from ideal. Adding a timeout to the exec_command didn't make any difference.Thomasinathomasine
A
27

Just close stdin

stdin, stdout, stderr = ssh.exec_command(commands)
stdin.close()
Amok answered 10/12, 2021 at 14:20 Comment(0)
S
10

Maybe you can try something like this:

stdin, stdout, stderr = ssh.exec_command(commands)
time.sleep(5)

(don't forget to import time)

This seems to add more time to process the command

Scaliger answered 22/5, 2020 at 20:47 Comment(0)
C
5

add the below:

if __name__ == "__main__":
    main()

then put your code in the main() def

Crossbeam answered 17/12, 2020 at 13:12 Comment(1)
hm, not sure why, but this actually worked for me.Jerad
E
1

It's the bug opened on https://github.com/paramiko/paramiko/issues/1617. As @NobodyNada said adding a time.sleep(5) is a workaround.

Edifice answered 24/6, 2020 at 12:13 Comment(0)
L
1

In my case the following line resolved the issue (put it on the top of the algorithm) :

import time
Lexy answered 22/11, 2023 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.