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