报错信息:
Exception ignored in: <function BufferedFile.__del__ at 0xffff8810f280> Traceback (most recent call last): File "/home/test/.local/lib/python3.9/site-packages/paramiko/file.py", line 66, in __del__ File "/home/test/.local/lib/python3.9/site-packages/paramiko/channel.py", line 1392, in close File "/home/test/.local/lib/python3.9/site-packages/paramiko/channel.py", line 991, in shutdown_write File "/home/test/.local/lib/python3.9/site-packages/paramiko/channel.py", line 967, in shutdown File "/home/test/.local/lib/python3.9/site-packages/paramiko/transport.py", line 1900, in _send_user_message AttributeError: 'NoneType' object has no attribute 'time'
添加:stdin.close()
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import paramiko ssh= paramiko.SSHClient()#创建SSH对象 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#允许连接不在know_hosts文件中的主机 ssh.connect(hostname='192.168.2.1', port=22, username='root', password='12345678')#连接服务器 stdin, stdout, stderr= ssh.exec_command('pwd')#执行命令并获取命令结果 stdin.close() #stdin为输入的命令 #stdout为命令返回的结果 #stderr为命令错误时返回的结果 res, err= stdout.read(), stderr.read() result= res if res else err print(result) ssh.close()#关闭连接
更多改法: