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