mirror of
				https://github.com/ARM-software/workload-automation.git
				synced 2025-10-31 15:12:25 +00:00 
			
		
		
		
	ssh: Back-port ssh_get_shell from devlib
This back-ports ssh_get_shell implementation from devlib. It includes the following changes: - original_prompt for Telnet verison of the connection can now be passed as an argument. - Multiple attempts to connect with a timeout. - Some additional implementation to the tty, including setting the size.
This commit is contained in:
		| @@ -22,6 +22,7 @@ import re | ||||
| import threading | ||||
| import tempfile | ||||
| import shutil | ||||
| import time | ||||
|  | ||||
| from pexpect import EOF, TIMEOUT, spawn, pxssh | ||||
|  | ||||
| @@ -36,21 +37,34 @@ sshpass = None | ||||
| logger = logging.getLogger('ssh') | ||||
|  | ||||
|  | ||||
| def ssh_get_shell(host, username, password=None, keyfile=None, port=None, timeout=10, telnet=False): | ||||
| def ssh_get_shell(host, username, password=None, keyfile=None, port=None, timeout=10, telnet=False, original_prompt=None): | ||||
|     _check_env() | ||||
|     if telnet: | ||||
|         if keyfile: | ||||
|             raise ConfigError('keyfile may not be used with a telnet connection.') | ||||
|         conn = TelnetConnection() | ||||
|     else:  # ssh | ||||
|         conn = pxssh.pxssh()  # pylint: disable=redefined-variable-type | ||||
|     try: | ||||
|         if keyfile: | ||||
|             conn.login(host, username, ssh_key=keyfile, port=port, login_timeout=timeout) | ||||
|         else: | ||||
|             conn.login(host, username, password, port=port, login_timeout=timeout) | ||||
|     except EOF: | ||||
|         raise DeviceError('Could not connect to {}; is the host name correct?'.format(host)) | ||||
|     start_time = time.time() | ||||
|     while True: | ||||
|         if telnet: | ||||
|             if keyfile: | ||||
|                 raise ValueError('keyfile may not be used with a telnet connection.') | ||||
|             conn = TelnetPxssh(original_prompt=original_prompt) | ||||
|         else:  # ssh | ||||
|             conn = pxssh.pxssh() | ||||
|  | ||||
|         try: | ||||
|             if keyfile: | ||||
|                 conn.login(host, username, ssh_key=keyfile, port=port, login_timeout=timeout) | ||||
|             else: | ||||
|                 conn.login(host, username, password, port=port, login_timeout=timeout) | ||||
|             break | ||||
|         except EOF: | ||||
|             timeout -= time.time() - start_time | ||||
|             if timeout <= 0: | ||||
|                 message = 'Could not connect to {}; is the host name correct?' | ||||
|                 raise TargetError(message.format(host)) | ||||
|             time.sleep(5) | ||||
|  | ||||
|     conn.setwinsize(500,200) | ||||
|     conn.sendline('') | ||||
|     conn.prompt() | ||||
|     conn.setecho(False) | ||||
|     return conn | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user