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