fix: 改密校验可连接性失败 (#11964)

Co-authored-by: feng <1304903146@qq.com>
pull/11968/head
fit2bot 2023-10-25 16:21:45 +08:00 committed by GitHub
parent fdb3f6409c
commit 09f8470d34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 4 deletions

View File

@ -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