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