无法通过SSH隧道使用paramiko的exec_command

问题描述

我无权直接访问该设备,因此我首先创建了一个通往中间设备的SSH隧道;然后登录到远端远程设备;然后执行。这在CLI上效果很好。

ssh user@127.0.0.1 -p 2222 -oKexAlgorithms=+diffie-hellman-group1-sha1 -c aes128-cbc
user@127.0.0.1's password: 

使用Paramiko时,我可以正确登录,但是无法发送命令。我已启用loggin来查看发生了什么,但找不到任何线索。

import paramiko
conn2rtr = paramiko.SSHClient()
conn2rtr.load_system_host_keys()
conn2rtr.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn2rtr.connect(hostname='127.0.0.1',port=2222,username='user',password='pass')
DEBUG:paramiko.transport:Adding ssh-rsa host key for [127.0.0.1]:2222: b'feaaa6e6e79b979e3a99c213e92a0b34'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!

但是当我要发送命令(即exec_command)时,会收到异常EOFError。

stdin,stdout,stderr = conn2rtr.exec_command("show version\n")                                                                                                                                                                     
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:EOF in transport thread
---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
<ipython-input-20-01669f7eafd5> in <module>
----> 1 stdin,stderr = conn2rtr.exec_command("show version\n")

/usr/local/lib/python3.8/dist-packages/paramiko/client.py in exec_command(self,command,bufsize,timeout,get_pty,environment)
    512         if environment:
    513             chan.update_environment(environment)
--> 514         chan.exec_command(command)
    515         stdin = chan.makefile_stdin("wb",bufsize)
    516         stdout = chan.makefile("r",bufsize)

/usr/local/lib/python3.8/dist-packages/paramiko/channel.py in _check(self,*args,**kwds)
     70         ):
     71             raise SSHException("Channel is not open")
---> 72         return func(self,**kwds)
     73 
     74     return _check

/usr/local/lib/python3.8/dist-packages/paramiko/channel.py in exec_command(self,command)
    255         self._event_pending()
    256         self.transport._send_user_message(m)
--> 257         self._wait_for_event()
    258 
    259     @open_only

/usr/local/lib/python3.8/dist-packages/paramiko/channel.py in _wait_for_event(self)
   1224         if e is None:
   1225             e = SSHException("Channel closed.")
-> 1226         raise e
   1227 
   1228     def _set_closed(self):

/usr/local/lib/python3.8/dist-packages/paramiko/transport.py in run(self)
   2053                         self._send_kex_init()
   2054                     try:
-> 2055                         ptype,m = self.packetizer.read_message()
   2056                     except NeedRekeyException:
   2057                         continue

/usr/local/lib/python3.8/dist-packages/paramiko/packet.py in read_message(self)
    457         :raises: `.NeedRekeyException` -- if the transport should rekey
    458         """
--> 459         header = self.read_all(self.__block_size_in,check_rekey=True)
    460         if self.__etm_in:
    461             packet_size = struct.unpack(">I",header[:4])[0]

/usr/local/lib/python3.8/dist-packages/paramiko/packet.py in read_all(self,n,check_rekey)
    301                 x = self.__socket.recv(n)
    302                 if len(x) == 0:
--> 303                     raise EOFError()
    304                 out += x
    305                 n -= len(x)

EOFError: 

如果我在bufsize内定义exec_command(),也会发生同样的情况。

有任何提示吗?谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)