George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 4 | This module provides many valuable ssh functions such as sprint_connection, execute_ssh_command, etc. |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | import sys |
Michael Walsh | d971a7a | 2018-11-16 15:28:19 -0600 | [diff] [blame] | 8 | import traceback |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 9 | import re |
Michael Walsh | 2575cd6 | 2017-11-01 17:03:45 -0500 | [diff] [blame] | 10 | import socket |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 11 | import paramiko |
George Keishing | 20a34c0 | 2018-09-24 11:26:04 -0500 | [diff] [blame] | 12 | try: |
| 13 | import exceptions |
| 14 | except ImportError: |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 15 | import builtins as exceptions |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 16 | |
| 17 | import gen_print as gp |
Michael Walsh | a48eb1a | 2018-08-07 14:48:13 -0500 | [diff] [blame] | 18 | import func_timer as ft |
| 19 | func_timer = ft.func_timer_class() |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 20 | |
| 21 | from robot.libraries.BuiltIn import BuiltIn |
| 22 | from SSHLibrary import SSHLibrary |
| 23 | sshlib = SSHLibrary() |
| 24 | |
| 25 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 26 | def sprint_connection(connection, |
| 27 | indent=0): |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 28 | r""" |
| 29 | sprint data from the connection object to a string and return it. |
| 30 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 31 | connection A connection object which is created by the SSHlibrary open_connection() |
| 32 | function. |
| 33 | indent The number of characters to indent the output. |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 34 | """ |
| 35 | |
| 36 | buffer = gp.sindent("", indent) |
| 37 | buffer += "connection:\n" |
| 38 | indent += 2 |
| 39 | buffer += gp.sprint_varx("index", connection.index, 0, indent) |
| 40 | buffer += gp.sprint_varx("host", connection.host, 0, indent) |
| 41 | buffer += gp.sprint_varx("alias", connection.alias, 0, indent) |
| 42 | buffer += gp.sprint_varx("port", connection.port, 0, indent) |
| 43 | buffer += gp.sprint_varx("timeout", connection.timeout, 0, indent) |
| 44 | buffer += gp.sprint_varx("newline", connection.newline, 0, indent) |
| 45 | buffer += gp.sprint_varx("prompt", connection.prompt, 0, indent) |
| 46 | buffer += gp.sprint_varx("term_type", connection.term_type, 0, indent) |
| 47 | buffer += gp.sprint_varx("width", connection.width, 0, indent) |
| 48 | buffer += gp.sprint_varx("height", connection.height, 0, indent) |
| 49 | buffer += gp.sprint_varx("path_separator", connection.path_separator, 0, |
| 50 | indent) |
| 51 | buffer += gp.sprint_varx("encoding", connection.encoding, 0, indent) |
| 52 | |
| 53 | return buffer |
| 54 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 55 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 56 | def sprint_connections(connections=None, |
| 57 | indent=0): |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 58 | r""" |
| 59 | sprint data from the connections list to a string and return it. |
| 60 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 61 | connections A list of connection objects which are created by the SSHlibrary |
| 62 | open_connection function. If this value is null, this function will |
| 63 | populate with a call to the SSHlibrary get_connections() function. |
| 64 | indent The number of characters to indent the output. |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 65 | """ |
| 66 | |
| 67 | if connections is None: |
| 68 | connections = sshlib.get_connections() |
| 69 | |
| 70 | buffer = "" |
| 71 | for connection in connections: |
| 72 | buffer += sprint_connection(connection, indent) |
| 73 | |
| 74 | return buffer |
| 75 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 76 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 77 | def find_connection(open_connection_args={}): |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 78 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 79 | Find connection that matches the given connection arguments and return connection object. Return False |
| 80 | if no matching connection is found. |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 81 | |
| 82 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 83 | open_connection_args A dictionary of arg names and values which are legal to pass to the |
| 84 | SSHLibrary open_connection function as parms/args. For a match to occur, |
| 85 | the value for each item in open_connection_args must match the |
| 86 | corresponding value in the connection being examined. |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 87 | """ |
| 88 | |
| 89 | global sshlib |
| 90 | |
| 91 | for connection in sshlib.get_connections(): |
| 92 | # Create connection_dict from connection object. |
| 93 | connection_dict = dict((key, str(value)) for key, value in |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 94 | connection._config.items()) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 95 | if dict(connection_dict, **open_connection_args) == connection_dict: |
| 96 | return connection |
| 97 | |
| 98 | return False |
| 99 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 100 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 101 | def login_ssh(login_args={}, |
| 102 | max_login_attempts=5): |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 103 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 104 | Login on the latest open SSH connection. Retry on failure up to max_login_attempts. |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 105 | |
| 106 | The caller is responsible for making sure there is an open SSH connection. |
| 107 | |
| 108 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 109 | login_args A dictionary containing the key/value pairs which are acceptable to the |
| 110 | SSHLibrary login function as parms/args. At a minimum, this should |
| 111 | contain a 'username' and a 'password' entry. |
| 112 | max_login_attempts The max number of times to try logging in (in the event of login |
| 113 | failures). |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 114 | """ |
| 115 | |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 116 | gp.lprint_executing() |
| 117 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 118 | global sshlib |
| 119 | |
| 120 | # Get connection data for debug output. |
| 121 | connection = sshlib.get_connection() |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 122 | gp.lprintn(sprint_connection(connection)) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 123 | for login_attempt_num in range(1, max_login_attempts + 1): |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 124 | gp.lprint_timen("Logging in to " + connection.host + ".") |
| 125 | gp.lprint_var(login_attempt_num) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 126 | try: |
| 127 | out_buf = sshlib.login(**login_args) |
Michael Walsh | 4344ac2 | 2019-08-21 16:14:10 -0500 | [diff] [blame] | 128 | except Exception: |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 129 | # Login will sometimes fail if the connection is new. |
| 130 | except_type, except_value, except_traceback = sys.exc_info() |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 131 | gp.lprint_var(except_type) |
| 132 | gp.lprint_varx("except_value", str(except_value)) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 133 | if except_type is paramiko.ssh_exception.SSHException and\ |
| 134 | re.match(r"No existing session", str(except_value)): |
| 135 | continue |
| 136 | else: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 137 | # We don't tolerate any other error so break from loop and re-raise exception. |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 138 | break |
| 139 | # If we get to this point, the login has worked and we can return. |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 140 | gp.lprint_var(out_buf) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 141 | return |
| 142 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 143 | # If we get to this point, the login has failed on all attempts so the exception will be raised again. |
Michael Walsh | 4344ac2 | 2019-08-21 16:14:10 -0500 | [diff] [blame] | 144 | raise(except_value) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 145 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 146 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 147 | def execute_ssh_command(cmd_buf, |
| 148 | open_connection_args={}, |
| 149 | login_args={}, |
| 150 | print_out=0, |
| 151 | print_err=0, |
| 152 | ignore_err=1, |
| 153 | fork=0, |
| 154 | quiet=None, |
Michael Walsh | a48eb1a | 2018-08-07 14:48:13 -0500 | [diff] [blame] | 155 | test_mode=None, |
| 156 | time_out=None): |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 157 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 158 | Run the given command in an SSH session and return the stdout, stderr and the return code. |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 159 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 160 | If there is no open SSH connection, this function will connect and login. Likewise, if the caller has |
| 161 | not yet logged in to the connection, this function will do the login. |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 162 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 163 | NOTE: There is special handling when open_connection_args['alias'] equals "device_connection". |
Michael Walsh | 8243ac9 | 2018-05-02 13:47:07 -0500 | [diff] [blame] | 164 | - A write, rather than an execute_command, is done. |
| 165 | - Only stdout is returned (no stderr or rc). |
| 166 | - print_err, ignore_err and fork are not supported. |
| 167 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 168 | Description of arguments: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 169 | cmd_buf The command string to be run in an SSH session. |
| 170 | open_connection_args A dictionary of arg names and values which are legal to pass to the |
| 171 | SSHLibrary open_connection function as parms/args. At a minimum, this |
| 172 | should contain a 'host' entry. |
| 173 | login_args A dictionary containing the key/value pairs which are acceptable to the |
| 174 | SSHLibrary login function as parms/args. At a minimum, this should |
| 175 | contain a 'username' and a 'password' entry. |
| 176 | print_out If this is set, this function will print the stdout/stderr generated by |
| 177 | the shell command. |
| 178 | print_err If show_err is set, this function will print a standardized error report |
| 179 | if the shell command returns non-zero. |
| 180 | ignore_err Indicates that errors encountered on the sshlib.execute_command are to be |
| 181 | ignored. |
| 182 | fork Indicates that sshlib.start is to be used rather than |
| 183 | sshlib.execute_command. |
| 184 | quiet Indicates whether this function should run the pissuing() function which |
| 185 | prints an "Issuing: <cmd string>" to stdout. This defaults to the global |
| 186 | quiet value. |
| 187 | test_mode If test_mode is set, this function will not actually run the command. |
| 188 | This defaults to the global test_mode value. |
| 189 | time_out The amount of time to allow for the execution of cmd_buf. A value of |
| 190 | None means that there is no limit to how long the command may take. |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 191 | """ |
| 192 | |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 193 | gp.lprint_executing() |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 194 | |
| 195 | # Obtain default values. |
| 196 | quiet = int(gp.get_var_value(quiet, 0)) |
| 197 | test_mode = int(gp.get_var_value(test_mode, 0)) |
| 198 | |
| 199 | if not quiet: |
| 200 | gp.pissuing(cmd_buf, test_mode) |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 201 | gp.lpissuing(cmd_buf, test_mode) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 202 | |
| 203 | if test_mode: |
| 204 | return "", "", 0 |
| 205 | |
| 206 | global sshlib |
| 207 | |
Michael Walsh | 019817d | 2018-07-24 16:00:06 -0500 | [diff] [blame] | 208 | max_exec_cmd_attempts = 2 |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 209 | # Look for existing SSH connection. |
| 210 | # Prepare a search connection dictionary. |
| 211 | search_connection_args = open_connection_args.copy() |
| 212 | # Remove keys that don't work well for searches. |
| 213 | search_connection_args.pop("timeout", None) |
| 214 | connection = find_connection(search_connection_args) |
| 215 | if connection: |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 216 | gp.lprint_timen("Found the following existing connection:") |
| 217 | gp.lprintn(sprint_connection(connection)) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 218 | if connection.alias == "": |
| 219 | index_or_alias = connection.index |
| 220 | else: |
| 221 | index_or_alias = connection.alias |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 222 | gp.lprint_timen("Switching to existing connection: \"" |
| 223 | + str(index_or_alias) + "\".") |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 224 | sshlib.switch_connection(index_or_alias) |
| 225 | else: |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 226 | gp.lprint_timen("Connecting to " + open_connection_args['host'] + ".") |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 227 | cix = sshlib.open_connection(**open_connection_args) |
Michael Walsh | 019817d | 2018-07-24 16:00:06 -0500 | [diff] [blame] | 228 | try: |
| 229 | login_ssh(login_args) |
Michael Walsh | 4344ac2 | 2019-08-21 16:14:10 -0500 | [diff] [blame] | 230 | except Exception: |
Michael Walsh | 019817d | 2018-07-24 16:00:06 -0500 | [diff] [blame] | 231 | except_type, except_value, except_traceback = sys.exc_info() |
| 232 | rc = 1 |
| 233 | stderr = str(except_value) |
| 234 | stdout = "" |
| 235 | max_exec_cmd_attempts = 0 |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 236 | |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 237 | for exec_cmd_attempt_num in range(1, max_exec_cmd_attempts + 1): |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 238 | gp.lprint_var(exec_cmd_attempt_num) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 239 | try: |
| 240 | if fork: |
| 241 | sshlib.start_command(cmd_buf) |
| 242 | else: |
Michael Walsh | 8243ac9 | 2018-05-02 13:47:07 -0500 | [diff] [blame] | 243 | if open_connection_args['alias'] == "device_connection": |
| 244 | stdout = sshlib.write(cmd_buf) |
| 245 | stderr = "" |
| 246 | rc = 0 |
| 247 | else: |
| 248 | stdout, stderr, rc = \ |
Michael Walsh | a48eb1a | 2018-08-07 14:48:13 -0500 | [diff] [blame] | 249 | func_timer.run(sshlib.execute_command, |
| 250 | cmd_buf, |
| 251 | return_stdout=True, |
| 252 | return_stderr=True, |
| 253 | return_rc=True, |
| 254 | time_out=time_out) |
Michael Walsh | 4344ac2 | 2019-08-21 16:14:10 -0500 | [diff] [blame] | 255 | except Exception: |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 256 | except_type, except_value, except_traceback = sys.exc_info() |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 257 | gp.lprint_var(except_type) |
| 258 | gp.lprint_varx("except_value", str(except_value)) |
Michael Walsh | a24de03 | 2018-11-01 14:03:30 -0500 | [diff] [blame] | 259 | # This may be our last time through the retry loop, so setting |
| 260 | # return variables. |
| 261 | rc = 1 |
| 262 | stderr = str(except_value) |
| 263 | stdout = "" |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 264 | |
| 265 | if except_type is exceptions.AssertionError and\ |
| 266 | re.match(r"Connection not open", str(except_value)): |
Michael Walsh | 3e79262 | 2018-08-20 11:17:41 -0500 | [diff] [blame] | 267 | try: |
| 268 | login_ssh(login_args) |
| 269 | # Now we must continue to next loop iteration to retry the |
| 270 | # execute_command. |
| 271 | continue |
Michael Walsh | 4344ac2 | 2019-08-21 16:14:10 -0500 | [diff] [blame] | 272 | except Exception: |
Michael Walsh | 3e79262 | 2018-08-20 11:17:41 -0500 | [diff] [blame] | 273 | except_type, except_value, except_traceback =\ |
| 274 | sys.exc_info() |
| 275 | rc = 1 |
| 276 | stderr = str(except_value) |
| 277 | stdout = "" |
| 278 | break |
| 279 | |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 280 | if (except_type is paramiko.ssh_exception.SSHException |
| 281 | and re.match(r"SSH session not active", str(except_value))) or\ |
Michael Walsh | 575f58b | 2019-10-22 13:36:27 -0500 | [diff] [blame] | 282 | ((except_type is socket.error |
| 283 | or except_type is ConnectionResetError) |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 284 | and re.match(r"\[Errno 104\] Connection reset by peer", |
Michael Walsh | e2bcab8 | 2018-10-02 15:16:42 -0500 | [diff] [blame] | 285 | str(except_value))) or\ |
| 286 | (except_type is paramiko.ssh_exception.SSHException |
| 287 | and re.match(r"Timeout opening channel\.", |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 288 | str(except_value))): |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 289 | # Close and re-open a connection. |
Michael Walsh | 2575cd6 | 2017-11-01 17:03:45 -0500 | [diff] [blame] | 290 | # Note: close_connection() doesn't appear to get rid of the |
| 291 | # connection. It merely closes it. Since there is a concern |
| 292 | # about over-consumption of resources, we use |
| 293 | # close_all_connections() which also gets rid of all |
| 294 | # connections. |
Michael Walsh | a03a331 | 2017-12-01 16:47:44 -0600 | [diff] [blame] | 295 | gp.lprint_timen("Closing all connections.") |
Michael Walsh | 2575cd6 | 2017-11-01 17:03:45 -0500 | [diff] [blame] | 296 | sshlib.close_all_connections() |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 297 | gp.lprint_timen("Connecting to " |
| 298 | + open_connection_args['host'] + ".") |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 299 | cix = sshlib.open_connection(**open_connection_args) |
| 300 | login_ssh(login_args) |
| 301 | continue |
| 302 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 303 | # We do not handle any other RuntimeErrors so we will raise the exception again. |
Michael Walsh | e77585a | 2017-12-14 11:02:28 -0600 | [diff] [blame] | 304 | sshlib.close_all_connections() |
Michael Walsh | d971a7a | 2018-11-16 15:28:19 -0600 | [diff] [blame] | 305 | gp.lprintn(traceback.format_exc()) |
Michael Walsh | 4344ac2 | 2019-08-21 16:14:10 -0500 | [diff] [blame] | 306 | raise(except_value) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 307 | |
| 308 | # If we get to this point, the command was executed. |
| 309 | break |
| 310 | |
| 311 | if fork: |
| 312 | return |
| 313 | |
| 314 | if rc != 0 and print_err: |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 315 | gp.print_var(rc, gp.hexa()) |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 316 | if not print_out: |
| 317 | gp.print_var(stderr) |
| 318 | gp.print_var(stdout) |
| 319 | |
| 320 | if print_out: |
| 321 | gp.printn(stderr + stdout) |
| 322 | |
| 323 | if not ignore_err: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 324 | message = gp.sprint_error("The prior SSH" |
| 325 | + " command returned a non-zero return" |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 326 | + " code:\n" |
| 327 | + gp.sprint_var(rc, gp.hexa()) + stderr |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 328 | + "\n") |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 329 | BuiltIn().should_be_equal(rc, 0, message) |
| 330 | |
Michael Walsh | 8243ac9 | 2018-05-02 13:47:07 -0500 | [diff] [blame] | 331 | if open_connection_args['alias'] == "device_connection": |
| 332 | return stdout |
Michael Walsh | bd1bb60 | 2017-06-30 17:01:25 -0500 | [diff] [blame] | 333 | return stdout, stderr, rc |