George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 4 | This module provides many valuable bmc ssh functions such as bmc_execute_command. |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 5 | """ |
| 6 | |
George Keishing | 2104d5f | 2022-01-31 04:33:30 -0600 | [diff] [blame] | 7 | import os |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 8 | import gen_valid as gv |
| 9 | import gen_robot_ssh as grs |
| 10 | from robot.libraries.BuiltIn import BuiltIn |
| 11 | |
| 12 | |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 13 | def bmc_execute_command(cmd_buf, |
| 14 | print_out=0, |
| 15 | print_err=0, |
| 16 | ignore_err=0, |
| 17 | fork=0, |
| 18 | quiet=None, |
Michael Walsh | c39c372 | 2018-08-07 14:50:56 -0500 | [diff] [blame] | 19 | test_mode=None, |
| 20 | time_out=None): |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 21 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 22 | Run the given command in an BMC SSH session and return the stdout, stderr and the return code. |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 23 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 24 | This function will obtain the global values for OPENBMC_HOST, OPENBMC_USERNAME, etc. |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 25 | |
| 26 | Description of arguments: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 27 | cmd_buf The command string to be run in an SSH session. |
| 28 | print_out If this is set, this function will print the stdout/stderr generated by |
| 29 | the shell command. |
| 30 | print_err If show_err is set, this function will print a standardized error report |
| 31 | if the shell command returns non-zero. |
| 32 | ignore_err Indicates that errors encountered on the sshlib.execute_command are to be |
| 33 | ignored. |
| 34 | fork Indicates that sshlib.start is to be used rather than |
| 35 | sshlib.execute_command. |
| 36 | quiet Indicates whether this function should run the pissuing() function prints |
| 37 | an "Issuing: <cmd string>" to stdout. This defaults to the global quiet |
| 38 | value. |
| 39 | test_mode If test_mode is set, this function will not actually run the command. |
| 40 | This defaults to the global test_mode value. |
| 41 | time_out The amount of time to allow for the execution of cmd_buf. A value of |
| 42 | None means that there is no limit to how long the command may take. |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 43 | """ |
| 44 | |
| 45 | # Get global BMC variable values. |
| 46 | openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}", default="") |
Michael Walsh | 046fe22 | 2019-11-08 14:33:53 -0600 | [diff] [blame] | 47 | ssh_port = BuiltIn().get_variable_value("${SSH_PORT}", default="22") |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 48 | openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}", |
| 49 | default="") |
| 50 | openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}", |
| 51 | default="") |
| 52 | |
| 53 | if not gv.valid_value(openbmc_host): |
| 54 | return "", "", 1 |
| 55 | if not gv.valid_value(openbmc_username): |
| 56 | return "", "", 1 |
| 57 | if not gv.valid_value(openbmc_password): |
| 58 | return "", "", 1 |
Michael Walsh | 046fe22 | 2019-11-08 14:33:53 -0600 | [diff] [blame] | 59 | if not gv.valid_value(ssh_port): |
| 60 | return "", "", 1 |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 61 | |
| 62 | open_connection_args = {'host': openbmc_host, 'alias': 'bmc_connection', |
Michael Walsh | 046fe22 | 2019-11-08 14:33:53 -0600 | [diff] [blame] | 63 | 'timeout': '25.0', 'prompt': '# ', 'port': ssh_port} |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 64 | login_args = {'username': openbmc_username, 'password': openbmc_password} |
| 65 | |
George Keishing | 2104d5f | 2022-01-31 04:33:30 -0600 | [diff] [blame] | 66 | openbmc_user_type = os.environ.get('USER_TYPE', "") or \ |
| 67 | BuiltIn().get_variable_value("${USER_TYPE}", default="") |
| 68 | if openbmc_user_type == 'sudo': |
George Keishing | 389e749 | 2022-04-04 09:23:55 -0500 | [diff] [blame] | 69 | cmd_buf = 'sudo -i ' + cmd_buf |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 70 | return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args, |
| 71 | print_out, print_err, ignore_err, fork, |
Michael Walsh | c39c372 | 2018-08-07 14:50:56 -0500 | [diff] [blame] | 72 | quiet, test_mode, time_out) |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 73 | |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 74 | |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 75 | def os_execute_command(cmd_buf, |
| 76 | print_out=0, |
| 77 | print_err=0, |
| 78 | ignore_err=0, |
| 79 | fork=0, |
| 80 | quiet=None, |
Michael Walsh | c39c372 | 2018-08-07 14:50:56 -0500 | [diff] [blame] | 81 | test_mode=None, |
Michael Shepos | 3390c85 | 2021-02-11 00:12:31 -0600 | [diff] [blame] | 82 | time_out=None, |
| 83 | os_host="", |
| 84 | os_username="", |
| 85 | os_password=""): |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 86 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 87 | Run the given command in an OS SSH session and return the stdout, stderr and the return code. |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 88 | |
| 89 | This function will obtain the global values for OS_HOST, OS_USERNAME, etc. |
| 90 | |
| 91 | Description of arguments: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 92 | cmd_buf The command string to be run in an SSH session. |
| 93 | print_out If this is set, this function will print the stdout/stderr generated by |
| 94 | the shell command. |
| 95 | print_err If show_err is set, this function will print a standardized error report |
| 96 | if the shell command returns non-zero. |
| 97 | ignore_err Indicates that errors encountered on the sshlib.execute_command are to be |
| 98 | ignored. |
| 99 | fork Indicates that sshlib.start is to be used rather than |
| 100 | sshlib.execute_command. |
| 101 | quiet Indicates whether this function should run the pissuing() function prints |
| 102 | an "Issuing: <cmd string>" to stdout. This defaults to the global quiet |
| 103 | value. |
| 104 | test_mode If test_mode is set, this function will not actually run the command. |
| 105 | This defaults to the global test_mode value. |
| 106 | time_out The amount of time to allow for the execution of cmd_buf. A value of |
| 107 | None means that there is no limit to how long the command may take. |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 108 | """ |
| 109 | |
| 110 | # Get global OS variable values. |
Michael Shepos | 3390c85 | 2021-02-11 00:12:31 -0600 | [diff] [blame] | 111 | if os_host == "": |
| 112 | os_host = BuiltIn().get_variable_value("${OS_HOST}", default="") |
Michael Shepos | 632f9a4 | 2021-02-24 08:21:59 -0600 | [diff] [blame] | 113 | if os_username == "": |
Michael Shepos | 3390c85 | 2021-02-11 00:12:31 -0600 | [diff] [blame] | 114 | os_username = BuiltIn().get_variable_value("${OS_USERNAME}", default="") |
Michael Shepos | 632f9a4 | 2021-02-24 08:21:59 -0600 | [diff] [blame] | 115 | if os_password == "": |
Michael Shepos | 3390c85 | 2021-02-11 00:12:31 -0600 | [diff] [blame] | 116 | os_password = BuiltIn().get_variable_value("${OS_PASSWORD}", default="") |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 117 | |
| 118 | if not gv.valid_value(os_host): |
| 119 | return "", "", 1 |
| 120 | if not gv.valid_value(os_username): |
| 121 | return "", "", 1 |
| 122 | if not gv.valid_value(os_password): |
| 123 | return "", "", 1 |
| 124 | |
| 125 | open_connection_args = {'host': os_host, 'alias': 'os_connection'} |
| 126 | login_args = {'username': os_username, 'password': os_password} |
| 127 | |
| 128 | return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args, |
| 129 | print_out, print_err, ignore_err, fork, |
Michael Walsh | c39c372 | 2018-08-07 14:50:56 -0500 | [diff] [blame] | 130 | quiet, test_mode, time_out) |
Michael Walsh | 3cc126a | 2017-06-30 17:02:10 -0500 | [diff] [blame] | 131 | |
Michael Walsh | 78fa29a | 2017-07-31 15:35:02 -0500 | [diff] [blame] | 132 | |
Michael Walsh | 78fa29a | 2017-07-31 15:35:02 -0500 | [diff] [blame] | 133 | def xcat_execute_command(cmd_buf, |
| 134 | print_out=0, |
| 135 | print_err=0, |
| 136 | ignore_err=0, |
| 137 | fork=0, |
| 138 | quiet=None, |
| 139 | test_mode=None): |
Michael Walsh | 78fa29a | 2017-07-31 15:35:02 -0500 | [diff] [blame] | 140 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 141 | Run the given command in an XCAT SSH session and return the stdout, stderr and the return code. |
Michael Walsh | 78fa29a | 2017-07-31 15:35:02 -0500 | [diff] [blame] | 142 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 143 | This function will obtain the global values for XCAT_HOST, XCAT_USERNAME, etc. |
Michael Walsh | 78fa29a | 2017-07-31 15:35:02 -0500 | [diff] [blame] | 144 | |
| 145 | Description of arguments: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 146 | cmd_buf The command string to be run in an SSH session. |
| 147 | print_out If this is set, this function will print the stdout/stderr generated by |
| 148 | the shell command. |
| 149 | print_err If show_err is set, this function will print a standardized error report |
| 150 | if the shell command returns non-zero. |
| 151 | ignore_err Indicates that errors encountered on the sshlib.execute_command are to be |
| 152 | ignored. |
| 153 | fork Indicates that sshlib.start is to be used rather than |
| 154 | sshlib.execute_command. |
| 155 | quiet Indicates whether this function should run the pissuing() function prints |
| 156 | an "Issuing: <cmd string>" to stdout. This defaults to the global quiet |
| 157 | value. |
| 158 | test_mode If test_mode is set, this function will not actually run the command. |
| 159 | This defaults to the global test_mode value. |
Michael Walsh | 78fa29a | 2017-07-31 15:35:02 -0500 | [diff] [blame] | 160 | """ |
| 161 | |
| 162 | # Get global XCAT variable values. |
| 163 | xcat_host = BuiltIn().get_variable_value("${XCAT_HOST}", default="") |
| 164 | xcat_username = BuiltIn().get_variable_value("${XCAT_USERNAME}", |
| 165 | default="") |
| 166 | xcat_password = BuiltIn().get_variable_value("${XCAT_PASSWORD}", |
| 167 | default="") |
| 168 | xcat_port = BuiltIn().get_variable_value("${XCAT_PORT}", |
| 169 | default="22") |
| 170 | |
| 171 | if not gv.valid_value(xcat_host): |
| 172 | return "", "", 1 |
| 173 | if not gv.valid_value(xcat_username): |
| 174 | return "", "", 1 |
| 175 | if not gv.valid_value(xcat_password): |
| 176 | return "", "", 1 |
| 177 | if not gv.valid_value(xcat_port): |
| 178 | return "", "", 1 |
| 179 | |
| 180 | open_connection_args = {'host': xcat_host, 'alias': 'xcat_connection', |
| 181 | 'port': xcat_port} |
| 182 | login_args = {'username': xcat_username, 'password': xcat_password} |
| 183 | |
| 184 | return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args, |
| 185 | print_out, print_err, ignore_err, fork, |
| 186 | quiet, test_mode) |
Michael Walsh | 8243ac9 | 2018-05-02 13:47:07 -0500 | [diff] [blame] | 187 | |
| 188 | |
| 189 | def device_write(cmd_buf, |
| 190 | print_out=0, |
| 191 | quiet=None, |
| 192 | test_mode=None): |
| 193 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 194 | Write the given command in a device SSH session and return the stdout, stderr and the return code. |
Michael Walsh | 8243ac9 | 2018-05-02 13:47:07 -0500 | [diff] [blame] | 195 | |
| 196 | This function is useful for writing to a switch. |
| 197 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 198 | This function will obtain the global values for DEVICE_HOST, DEVICE_USERNAME, etc. |
Michael Walsh | 8243ac9 | 2018-05-02 13:47:07 -0500 | [diff] [blame] | 199 | |
| 200 | Description of arguments: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 201 | cmd_buf The command string to be run in an SSH session. |
| 202 | print_out If this is set, this function will print the stdout/stderr generated by |
| 203 | the shell command. |
| 204 | print_err If show_err is set, this function will print a standardized error report |
| 205 | if the shell command returns non-zero. |
| 206 | ignore_err Indicates that errors encountered on the sshlib.execute_command are to be |
| 207 | ignored. |
| 208 | fork Indicates that sshlib.start is to be used rather than |
| 209 | sshlib.execute_command. |
| 210 | quiet Indicates whether this function should run the pissuing() function prints |
| 211 | an "Issuing: <cmd string>" to stdout. This defaults to the global quiet |
| 212 | value. |
| 213 | test_mode If test_mode is set, this function will not actually run the command. |
| 214 | This defaults to the global test_mode value. |
Michael Walsh | 8243ac9 | 2018-05-02 13:47:07 -0500 | [diff] [blame] | 215 | """ |
| 216 | |
| 217 | # Get global DEVICE variable values. |
| 218 | device_host = BuiltIn().get_variable_value("${DEVICE_HOST}", default="") |
| 219 | device_username = BuiltIn().get_variable_value("${DEVICE_USERNAME}", |
| 220 | default="") |
| 221 | device_password = BuiltIn().get_variable_value("${DEVICE_PASSWORD}", |
| 222 | default="") |
| 223 | device_port = BuiltIn().get_variable_value("${DEVICE_PORT}", |
| 224 | default="22") |
| 225 | |
| 226 | if not gv.valid_value(device_host): |
| 227 | return "", "", 1 |
| 228 | if not gv.valid_value(device_username): |
| 229 | return "", "", 1 |
| 230 | if not gv.valid_value(device_password): |
| 231 | return "", "", 1 |
| 232 | if not gv.valid_value(device_port): |
| 233 | return "", "", 1 |
| 234 | |
| 235 | open_connection_args = {'host': device_host, 'alias': 'device_connection', |
| 236 | 'port': device_port} |
| 237 | login_args = {'username': device_username, 'password': device_password} |
| 238 | |
| 239 | return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args, |
| 240 | print_out, print_err=0, ignore_err=1, |
| 241 | fork=0, quiet=quiet, test_mode=test_mode) |