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