mirror of https://github.com/jumpserver/jumpserver
parent
fdb3f6409c
commit
09f8470d34
|
@ -26,6 +26,9 @@ def common_argument_spec():
|
||||||
|
|
||||||
|
|
||||||
class SSHClient:
|
class SSHClient:
|
||||||
|
SLEEP_INTERVAL = 0.3
|
||||||
|
COMPLETE_FLAG = 'complete'
|
||||||
|
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
self.module = module
|
self.module = module
|
||||||
self.channel = None
|
self.channel = None
|
||||||
|
@ -85,7 +88,10 @@ class SSHClient:
|
||||||
su_output, err_msg = self.execute(commands)
|
su_output, err_msg = self.execute(commands)
|
||||||
if err_msg:
|
if err_msg:
|
||||||
return 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:
|
if err_msg:
|
||||||
return err_msg
|
return err_msg
|
||||||
|
|
||||||
|
@ -153,15 +159,21 @@ class SSHClient:
|
||||||
output = self.channel.recv(size).decode(encoding)
|
output = self.channel.recv(size).decode(encoding)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def execute(self, commands, delay_time=0.3):
|
def execute(self, commands, validate_output=False):
|
||||||
if not self.is_connect:
|
if not self.is_connect:
|
||||||
self.connect()
|
self.connect()
|
||||||
output, error_msg = '', ''
|
output, error_msg = '', ''
|
||||||
try:
|
try:
|
||||||
for command in commands:
|
for command in commands:
|
||||||
self.channel.send(command + '\n')
|
self.channel.send(command + '\n')
|
||||||
time.sleep(delay_time)
|
if not validate_output:
|
||||||
output = self._get_recv()
|
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:
|
except Exception as e:
|
||||||
error_msg = str(e)
|
error_msg = str(e)
|
||||||
return output, error_msg
|
return output, error_msg
|
||||||
|
|
Loading…
Reference in New Issue