尝试在Python 3.6中使用paramiko library时,我遇到了尽可能简单的代码片段。尝试从我的Linux主机连接到我的Windows主机(ssh服务器正在运行,我可以通过终端连接),我得到了这个错误:
SSHException: Channel closed.代码:
@keyword('Open Connection And Log In ${host} ${port} ${username} ${pwd}')
def open_connection_and_login(host, port, username, pwd):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, port=22, username='root', password='root')
command = 'dir'
(stdin, stdout, stderr) = client.exec_command(command)
print(stdout.read())
client.close()回溯:
File "<string>", line 1, in <module>
File "/home/matejb/Development/automated-testing/python_libs/topit/ssh/basicOperations.py", line 13, in open_connection_and_login
(stdin, stdout, stderr) = client.exec_command(command)
File "/usr/local/lib/python3.6/dist-packages/paramiko/client.py", line 478, in exec_command
chan.exec_command(command)
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 63, in _check
return func(self, *args, **kwds)
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 241, in exec_command
self._wait_for_event()
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 1197, in _wait_for_event
raise e
paramiko.ssh_exception.SSHException: Channel closed.
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/matejb/Development/automated-testing/python_libs/topit/ssh/basicOperations.py", line 13, in open_connection_and_login
(stdin, stdout, stderr) = client.exec_command(command)
File "/usr/local/lib/python3.6/dist-packages/paramiko/client.py", line 478, in exec_command
chan.exec_command(command)
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 63, in _check
return func(self, *args, **kwds)
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 241, in exec_command
self._wait_for_event()
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 1197, in _wait_for_event
raise e
paramiko.ssh_exception.SSHException: Channel closed.更多日志:
DEBUG:paramiko.transport:starting thread (client mode): 0x10223668
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.3.1
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-WeOnlyDo 2.4.3
INFO:paramiko.transport:Connected (version 2.0, client WeOnlyDo)
DEBUG:paramiko.transport:kex algos:['ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group1-sha1', 'diffie-hellman-group14-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-cbc', 'aes128-ctr', '3des-cbc', 'blowfish-cbc', 'aes192-cbc', 'aes192-ctr', 'aes256-cbc', 'aes256-ctr', 'rijndael128-cbc', 'rijndael192-cbc', 'rijndael256-cbc', 'rijndael-cbc@lysator.liu.se', 'cast128-cbc'] server encrypt:['aes128-cbc', 'aes128-ctr', '3des-cbc', 'blowfish-cbc', 'aes192-cbc', 'aes192-ctr', 'aes256-cbc', 'aes256-ctr', 'rijndael128-cbc', 'rijndael192-cbc', 'rijndael256-cbc', 'rijndael-cbc@lysator.liu.se', 'cast128-cbc'] client mac:['hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1', 'hmac-sha1-96', 'hmac-md5', 'none'] server mac:['hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1', 'hmac-sha1-96', 'hmac-md5', 'none'] client compress:['none', 'none'] server compress:['none', 'none'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: ecdh-sha2-nistp256
DEBUG:paramiko.transport:HostKey agreed: ssh-rsa
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-rsa host key for 10.99.2.20: b'eea39f0acdfb5c2b7d042887e668ebac'
DEBUG:paramiko.transport:Trying SSH agent key b'e440c2d0b5fb98ab3951ddc52c4ba86b'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.
DEBUG:paramiko.transport:Trying discovered key b'e440c2d0b5fb98ab3951ddc52c4ba86b' in /home/matejb/.ssh/id_rsa
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
<function basicConfig at 0x7f9311f11950>
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 0] Max packet out: 98304 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:[chan 0] EOF sent (0)
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/matejb/Development/automated-testing/python_libs/topit/ssh/basicOperations.py", line 17, in open_connection_and_login
client.open_sftp()
File "/usr/local/lib/python3.6/dist-packages/paramiko/client.py", line 513, in open_sftp
return self._transport.open_sftp_client()
File "/usr/local/lib/python3.6/dist-packages/paramiko/transport.py", line 978, in open_sftp_client
return SFTPClient.from_transport(self)
File "/usr/local/lib/python3.6/dist-packages/paramiko/sftp_client.py", line 138, in from_transport
chan.invoke_subsystem('sftp')
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 63, in _check
return func(self, *args, **kwds)
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 267, in invoke_subsystem
self._wait_for_event()
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 1197, in _wait_for_event
raise e
paramiko.ssh_exception.SSHException: Channel closed.
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/matejb/Development/automated-testing/python_libs/topit/ssh/basicOperations.py", line 17, in open_connection_and_login
client.open_sftp()
File "/usr/local/lib/python3.6/dist-packages/paramiko/client.py", line 513, in open_sftp
return self._transport.open_sftp_client()
File "/usr/local/lib/python3.6/dist-packages/paramiko/transport.py", line 978, in open_sftp_client
return SFTPClient.from_transport(self)
File "/usr/local/lib/python3.6/dist-packages/paramiko/sftp_client.py", line 138, in from_transport
chan.invoke_subsystem('sftp')
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 63, in _check
return func(self, *args, **kwds)
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 267, in invoke_subsystem
self._wait_for_event()
File "/usr/local/lib/python3.6/dist-packages/paramiko/channel.py", line 1197, in _wait_for_event
raise e
paramiko.ssh_exception.SSHException: Channel closed.发布于 2017-12-04 17:25:48
你安装了丢失的包并抛出了ModuleNotFoundError: No module named 'apt_pkg'吗?
否则,如果问题出在不允许执行特定命令的服务器上,您可能需要查看错误代码。
https://stackoverflow.com/questions/47153957
复制相似问题