diff --git a/devlib/utils/ssh.py b/devlib/utils/ssh.py index c985ed2..f07b67b 100644 --- a/devlib/utils/ssh.py +++ b/devlib/utils/ssh.py @@ -67,9 +67,17 @@ from devlib.connection import (ConnectionBase, ParamikoBackgroundCommand, PopenB DEFAULT_SSH_SUDO_COMMAND = "sudo -k -p ' ' -S -- sh -c {}" -ssh = None -scp = None -sshpass = None +# Lazy init of some globals +def __getattr__(attr): + if attr in {'ssh', 'scp', 'sshpass'}: + path = which(attr) + if path: + globals()[attr] = path + return path + else: + raise HostError(f'OpenSSH must be installed on the host: could not find {attr} command') + else: + raise AttributeError(f"Module '{__name__}' has no attribute '{attr}'") logger = logging.getLogger('ssh') @@ -176,7 +184,6 @@ def telnet_get_shell(host, port=None, timeout=10, original_prompt=None): - _check_env() start_time = time.time() while True: conn = TelnetPxssh(original_prompt=original_prompt) @@ -792,7 +799,6 @@ class TelnetConnection(SshConnectionBase): strict_host_check=strict_host_check, ) - _check_env() self.options = self._get_default_options() self.lock = threading.Lock() @@ -1607,16 +1613,6 @@ def _give_password(password, command): return (pass_string + command, redacted_string + command) -def _check_env(): - global ssh, scp, sshpass # pylint: disable=global-statement - if not ssh: - ssh = which('ssh') - scp = which('scp') - sshpass = which('sshpass') - if not (ssh and scp): - raise HostError('OpenSSH must be installed on the host.') - - def process_backspaces(text): chars = [] for c in text: