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