From 09f8470d347a8b413231b2c5f85d6e5caa2b3262 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:21:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=B9=E5=AF=86=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=8F=AF=E8=BF=9E=E6=8E=A5=E6=80=A7=E5=A4=B1=E8=B4=A5=20(#1196?= =?UTF-8?q?4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> --- .../ansible/modules_utils/custom_common.py | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/apps/ops/ansible/modules_utils/custom_common.py b/apps/ops/ansible/modules_utils/custom_common.py index 920ba6942..f903fab25 100644 --- a/apps/ops/ansible/modules_utils/custom_common.py +++ b/apps/ops/ansible/modules_utils/custom_common.py @@ -26,6 +26,9 @@ def common_argument_spec(): class SSHClient: + SLEEP_INTERVAL = 0.3 + COMPLETE_FLAG = 'complete' + def __init__(self, module): self.module = module self.channel = None @@ -85,7 +88,10 @@ class SSHClient: su_output, err_msg = self.execute(commands) if err_msg: return err_msg - i_output, err_msg = self.execute(['whoami'], delay_time=1) + i_output, err_msg = self.execute( + [f'whoami && echo "{self.COMPLETE_FLAG}"'], + validate_output=True + ) if err_msg: return err_msg @@ -153,15 +159,21 @@ class SSHClient: output = self.channel.recv(size).decode(encoding) return output - def execute(self, commands, delay_time=0.3): + def execute(self, commands, validate_output=False): if not self.is_connect: self.connect() output, error_msg = '', '' try: for command in commands: self.channel.send(command + '\n') - time.sleep(delay_time) - output = self._get_recv() + if not validate_output: + time.sleep(self.SLEEP_INTERVAL) + output += self._get_recv() + continue + while self.COMPLETE_FLAG not in output: + time.sleep(self.SLEEP_INTERVAL) + received_output = self._get_recv().replace(f'"{self.COMPLETE_FLAG}"', '') + output += received_output except Exception as e: error_msg = str(e) return output, error_msg