From 3b918eebd1892a416f3bc0c9ffd841a42a5b4e8b Mon Sep 17 00:00:00 2001 From: xiasf <811800545@qq.com> Date: Fri, 24 Jun 2022 11:29:04 +0800 Subject: [PATCH] =?UTF-8?q?F=20=E4=BF=AE=E5=A4=8D=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=8F=91=E5=B8=83=E5=89=8D=E4=BB=BB=E5=8A=A1=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E5=87=BA=E7=8E=B0response=20timeout=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BUG原因:`_get_channel()` 这里没有考虑到应用环境变量命令长度问题,当命令在目标机执行时被截断时就会出问题,我参照 `exec_command()` 让 `_get_channel()` 这里也用一样的方式(`put_file_by_fl()`),就解决这个问题了。 另外`send()` 都调整成了 `sendall()` 防止极端情况下命令字符没有被全部发送出去而出问题。 有人曾反映过这个问题:https://github.com/openspug/spug/issues/455 ---- 相关的资料: https://unix.stackexchange.com/questions/643777/is-there-any-limit-on-line-length-when-pasting-to-a-terminal-in-linux https://stackoverflow.com/questions/18015137/linux-terminal-input-reading-user-input-from-terminal-truncating-lines-at-4095 https://github.com/torvalds/linux/blob/v5.11/drivers/tty/n_tty.c#L1681 --- spug_api/libs/ssh.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spug_api/libs/ssh.py b/spug_api/libs/ssh.py index b7f9d1b..f964c99 100644 --- a/spug_api/libs/ssh.py +++ b/spug_api/libs/ssh.py @@ -31,7 +31,7 @@ class SSH: self.sftp = None self.exec_file = None self.eof = 'Spug EOF 2108111926' - self.default_env = self._make_env_command(default_env) + self.default_env = default_env self.regex = re.compile(r'Spug EOF 2108111926 (-?\d+)[\r\n]?') self.arguments = { 'hostname': hostname, @@ -83,7 +83,7 @@ class SSH: def exec_command(self, command, environment=None): channel = self._get_channel() command = self._handle_command(command, environment) - channel.send(command) + channel.sendall(command) out, exit_code = '', -1 for line in self.stdout: match = self.regex.search(line) @@ -112,7 +112,7 @@ class SSH: def exec_command_with_stream(self, command, environment=None): channel = self._get_channel() command = self._handle_command(command, environment) - channel.send(command) + channel.sendall(command) exit_code, line = -1, '' while True: line = self._decode(channel.recv(8196)) @@ -153,14 +153,14 @@ class SSH: counter = 0 self.channel = self.client.invoke_shell() command = 'set +o history\nset +o zle\nset -o no_nomatch\nexport PS1= && stty -echo\n' - if self.default_env: - command += f'{self.default_env}\n' - command += f'echo {self.eof} $?\n' - self.channel.send(command.encode()) + command = self._handle_command(command, self.default_env) + self.channel.sendall(command) + out = line = '' while True: if self.channel.recv_ready(): line = self._decode(self.channel.recv(8196)) - if self.regex.search(line): + out += line + if self.regex.search(out): self.stdout = self.channel.makefile('r') break elif counter >= 100: