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