From 0feae81eb8e41cba1d9388714b585c86290e0af1 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 22 Sep 2023 11:07:25 +0800 Subject: [PATCH] bugfix --- appPublic/sshx.py | 45 +++++++++++++++++++++++++++++++++------------ requirements.txt | 1 + 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/appPublic/sshx.py b/appPublic/sshx.py index ccd6af3..e8a9b46 100644 --- a/appPublic/sshx.py +++ b/appPublic/sshx.py @@ -1,6 +1,7 @@ import os import sys import shlex +from functools import partial from threading import Thread from appPublic.myTE import tmpTml import asyncio, asyncssh, sys @@ -98,8 +99,11 @@ class SSHNode: async def _cmd(self, cmd, input=None, stdin=None, stdout=None): return await self.conn.run(cmd, input=input, stdin=stdin, stdout=stdout) - async def _xcmd(self, cmd, xmsgs=[], ns={}): + async def _xcmd(self, cmd, xmsgs=[], ns={}, + show_input=None, + show_stdout=None): proc = await self._process(cmd, term_type='xterm', + term_size=(80,24), encoding='utf-8' ) @@ -111,23 +115,40 @@ class SSHNode: break s = xmsgs[i][1].format(**ns) proc.stdin.write(s) + keyin = False + def feed_data(xmsgs, debug_input): + if len(xmsgs) == 0: + return + a = xmsgs[0] + xmsgs.reverse() + xmsgs.pop() + xmsgs.reverse() + s = a[1].format(**ns) + proc.stdin.write(s) + if debug_input: + debug_input(f'{s=},{a=}') + keyin = True + already_output = False + callee = None + loop = asyncio.get_event_loop() while True: if proc.stdout.at_eof(): break - x = await proc.stdout.read(1024) - buffer += x - if i >= msglen: - continue - k = xmsgs[i][0].format(**ns) - kin = buffer.split(k, 1) - if len(kin) > 1: - s = xmsgs[i][1].format(**ns) - proc.stdin.write(s) + if keyin: + keyin = False await proc.stdin.drain() - buffer = kin[1] - i += 1 + + if len(xmsgs) > 0 and already_output: + f = partial(feed_data, xmsgs, show_input) + callee = loop.call_later(xmsgs[0][0], f) + x = await proc.stdout.read(1024) + if callee: + callee.cancel() + if show_stdout: + show_stdout(x) + already_output = True async def _run(self, cmd, input=None, stdin=None, stdout=None): if cmd.startswith('l2r'): diff --git a/requirements.txt b/requirements.txt index 68d32c4..77e88a2 100755 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ requests jinja2 pyzmq cryptography +asyncssh