blob: b9ead59019c4685eb9296f40ca6740065ee204d6 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Michael Walsh3cc126a2017-06-30 17:02:10 -05002
3r"""
Michael Walsh410b1782019-10-22 15:56:18 -05004This module provides many valuable bmc ssh functions such as bmc_execute_command.
Michael Walsh3cc126a2017-06-30 17:02:10 -05005"""
6
George Keishing2104d5f2022-01-31 04:33:30 -06007import os
Patrick Williams20f38712022-12-08 06:18:26 -06008
George Keishinge635ddc2022-12-08 07:38:02 -06009import gen_robot_ssh as grs
Patrick Williams20f38712022-12-08 06:18:26 -060010import gen_valid as gv
Michael Walsh3cc126a2017-06-30 17:02:10 -050011from robot.libraries.BuiltIn import BuiltIn
12
13
Patrick Williams20f38712022-12-08 06:18:26 -060014def bmc_execute_command(
15 cmd_buf,
16 print_out=0,
17 print_err=0,
18 ignore_err=0,
19 fork=0,
20 quiet=None,
21 test_mode=None,
22 time_out=None,
23):
Michael Walsh3cc126a2017-06-30 17:02:10 -050024 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050025 Run the given command in an BMC SSH session and return the stdout, stderr and the return code.
Michael Walsh3cc126a2017-06-30 17:02:10 -050026
Michael Walsh410b1782019-10-22 15:56:18 -050027 This function will obtain the global values for OPENBMC_HOST, OPENBMC_USERNAME, etc.
Michael Walsh3cc126a2017-06-30 17:02:10 -050028
29 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -050030 cmd_buf The command string to be run in an SSH session.
31 print_out If this is set, this function will print the stdout/stderr generated by
32 the shell command.
33 print_err If show_err is set, this function will print a standardized error report
34 if the shell command returns non-zero.
35 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
36 ignored.
37 fork Indicates that sshlib.start is to be used rather than
38 sshlib.execute_command.
39 quiet Indicates whether this function should run the pissuing() function prints
40 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
41 value.
42 test_mode If test_mode is set, this function will not actually run the command.
43 This defaults to the global test_mode value.
44 time_out The amount of time to allow for the execution of cmd_buf. A value of
45 None means that there is no limit to how long the command may take.
Michael Walsh3cc126a2017-06-30 17:02:10 -050046 """
47
48 # Get global BMC variable values.
49 openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}", default="")
Michael Walsh046fe222019-11-08 14:33:53 -060050 ssh_port = BuiltIn().get_variable_value("${SSH_PORT}", default="22")
Patrick Williams20f38712022-12-08 06:18:26 -060051 openbmc_username = BuiltIn().get_variable_value(
52 "${OPENBMC_USERNAME}", default=""
53 )
54 openbmc_password = BuiltIn().get_variable_value(
55 "${OPENBMC_PASSWORD}", default=""
56 )
Michael Walsh3cc126a2017-06-30 17:02:10 -050057
58 if not gv.valid_value(openbmc_host):
59 return "", "", 1
60 if not gv.valid_value(openbmc_username):
61 return "", "", 1
62 if not gv.valid_value(openbmc_password):
63 return "", "", 1
Michael Walsh046fe222019-11-08 14:33:53 -060064 if not gv.valid_value(ssh_port):
65 return "", "", 1
Michael Walsh3cc126a2017-06-30 17:02:10 -050066
Patrick Williams20f38712022-12-08 06:18:26 -060067 open_connection_args = {
68 "host": openbmc_host,
69 "alias": "bmc_connection",
70 "timeout": "25.0",
71 "prompt": "# ",
72 "port": ssh_port,
73 }
74 login_args = {"username": openbmc_username, "password": openbmc_password}
Michael Walsh3cc126a2017-06-30 17:02:10 -050075
Patrick Williams20f38712022-12-08 06:18:26 -060076 openbmc_user_type = os.environ.get(
77 "USER_TYPE", ""
78 ) or BuiltIn().get_variable_value("${USER_TYPE}", default="")
79 if openbmc_user_type == "sudo":
80 cmd_buf = "sudo -i " + cmd_buf
81 return grs.execute_ssh_command(
82 cmd_buf,
83 open_connection_args,
84 login_args,
85 print_out,
86 print_err,
87 ignore_err,
88 fork,
89 quiet,
90 test_mode,
91 time_out,
92 )
Michael Walsh3cc126a2017-06-30 17:02:10 -050093
Michael Walsh3cc126a2017-06-30 17:02:10 -050094
Patrick Williams20f38712022-12-08 06:18:26 -060095def os_execute_command(
96 cmd_buf,
97 print_out=0,
98 print_err=0,
99 ignore_err=0,
100 fork=0,
101 quiet=None,
102 test_mode=None,
103 time_out=None,
104 os_host="",
105 os_username="",
106 os_password="",
107):
Michael Walsh3cc126a2017-06-30 17:02:10 -0500108 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500109 Run the given command in an OS SSH session and return the stdout, stderr and the return code.
Michael Walsh3cc126a2017-06-30 17:02:10 -0500110
111 This function will obtain the global values for OS_HOST, OS_USERNAME, etc.
112
113 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500114 cmd_buf The command string to be run in an SSH session.
115 print_out If this is set, this function will print the stdout/stderr generated by
116 the shell command.
117 print_err If show_err is set, this function will print a standardized error report
118 if the shell command returns non-zero.
119 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
120 ignored.
121 fork Indicates that sshlib.start is to be used rather than
122 sshlib.execute_command.
123 quiet Indicates whether this function should run the pissuing() function prints
124 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
125 value.
126 test_mode If test_mode is set, this function will not actually run the command.
127 This defaults to the global test_mode value.
128 time_out The amount of time to allow for the execution of cmd_buf. A value of
129 None means that there is no limit to how long the command may take.
Michael Walsh3cc126a2017-06-30 17:02:10 -0500130 """
131
132 # Get global OS variable values.
Michael Shepos3390c852021-02-11 00:12:31 -0600133 if os_host == "":
134 os_host = BuiltIn().get_variable_value("${OS_HOST}", default="")
Michael Shepos632f9a42021-02-24 08:21:59 -0600135 if os_username == "":
Patrick Williams20f38712022-12-08 06:18:26 -0600136 os_username = BuiltIn().get_variable_value(
137 "${OS_USERNAME}", default=""
138 )
Michael Shepos632f9a42021-02-24 08:21:59 -0600139 if os_password == "":
Patrick Williams20f38712022-12-08 06:18:26 -0600140 os_password = BuiltIn().get_variable_value(
141 "${OS_PASSWORD}", default=""
142 )
Michael Walsh3cc126a2017-06-30 17:02:10 -0500143
144 if not gv.valid_value(os_host):
145 return "", "", 1
146 if not gv.valid_value(os_username):
147 return "", "", 1
148 if not gv.valid_value(os_password):
149 return "", "", 1
150
Patrick Williams20f38712022-12-08 06:18:26 -0600151 open_connection_args = {"host": os_host, "alias": "os_connection"}
152 login_args = {"username": os_username, "password": os_password}
Michael Walsh3cc126a2017-06-30 17:02:10 -0500153
Patrick Williams20f38712022-12-08 06:18:26 -0600154 return grs.execute_ssh_command(
155 cmd_buf,
156 open_connection_args,
157 login_args,
158 print_out,
159 print_err,
160 ignore_err,
161 fork,
162 quiet,
163 test_mode,
164 time_out,
165 )
Michael Walsh3cc126a2017-06-30 17:02:10 -0500166
Michael Walsh78fa29a2017-07-31 15:35:02 -0500167
Patrick Williams20f38712022-12-08 06:18:26 -0600168def xcat_execute_command(
169 cmd_buf,
170 print_out=0,
171 print_err=0,
172 ignore_err=0,
173 fork=0,
174 quiet=None,
175 test_mode=None,
176):
Michael Walsh78fa29a2017-07-31 15:35:02 -0500177 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500178 Run the given command in an XCAT SSH session and return the stdout, stderr and the return code.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500179
Michael Walsh410b1782019-10-22 15:56:18 -0500180 This function will obtain the global values for XCAT_HOST, XCAT_USERNAME, etc.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500181
182 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500183 cmd_buf The command string to be run in an SSH session.
184 print_out If this is set, this function will print the stdout/stderr generated by
185 the shell command.
186 print_err If show_err is set, this function will print a standardized error report
187 if the shell command returns non-zero.
188 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
189 ignored.
190 fork Indicates that sshlib.start is to be used rather than
191 sshlib.execute_command.
192 quiet Indicates whether this function should run the pissuing() function prints
193 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
194 value.
195 test_mode If test_mode is set, this function will not actually run the command.
196 This defaults to the global test_mode value.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500197 """
198
199 # Get global XCAT variable values.
200 xcat_host = BuiltIn().get_variable_value("${XCAT_HOST}", default="")
Patrick Williams20f38712022-12-08 06:18:26 -0600201 xcat_username = BuiltIn().get_variable_value(
202 "${XCAT_USERNAME}", default=""
203 )
204 xcat_password = BuiltIn().get_variable_value(
205 "${XCAT_PASSWORD}", default=""
206 )
207 xcat_port = BuiltIn().get_variable_value("${XCAT_PORT}", default="22")
Michael Walsh78fa29a2017-07-31 15:35:02 -0500208
209 if not gv.valid_value(xcat_host):
210 return "", "", 1
211 if not gv.valid_value(xcat_username):
212 return "", "", 1
213 if not gv.valid_value(xcat_password):
214 return "", "", 1
215 if not gv.valid_value(xcat_port):
216 return "", "", 1
217
Patrick Williams20f38712022-12-08 06:18:26 -0600218 open_connection_args = {
219 "host": xcat_host,
220 "alias": "xcat_connection",
221 "port": xcat_port,
222 }
223 login_args = {"username": xcat_username, "password": xcat_password}
Michael Walsh78fa29a2017-07-31 15:35:02 -0500224
Patrick Williams20f38712022-12-08 06:18:26 -0600225 return grs.execute_ssh_command(
226 cmd_buf,
227 open_connection_args,
228 login_args,
229 print_out,
230 print_err,
231 ignore_err,
232 fork,
233 quiet,
234 test_mode,
235 )
Michael Walsh8243ac92018-05-02 13:47:07 -0500236
237
Patrick Williams20f38712022-12-08 06:18:26 -0600238def device_write(cmd_buf, print_out=0, quiet=None, test_mode=None):
Michael Walsh8243ac92018-05-02 13:47:07 -0500239 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500240 Write the given command in a device SSH session and return the stdout, stderr and the return code.
Michael Walsh8243ac92018-05-02 13:47:07 -0500241
242 This function is useful for writing to a switch.
243
Michael Walsh410b1782019-10-22 15:56:18 -0500244 This function will obtain the global values for DEVICE_HOST, DEVICE_USERNAME, etc.
Michael Walsh8243ac92018-05-02 13:47:07 -0500245
246 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500247 cmd_buf The command string to be run in an SSH session.
248 print_out If this is set, this function will print the stdout/stderr generated by
249 the shell command.
250 print_err If show_err is set, this function will print a standardized error report
251 if the shell command returns non-zero.
252 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
253 ignored.
254 fork Indicates that sshlib.start is to be used rather than
255 sshlib.execute_command.
256 quiet Indicates whether this function should run the pissuing() function prints
257 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
258 value.
259 test_mode If test_mode is set, this function will not actually run the command.
260 This defaults to the global test_mode value.
Michael Walsh8243ac92018-05-02 13:47:07 -0500261 """
262
263 # Get global DEVICE variable values.
264 device_host = BuiltIn().get_variable_value("${DEVICE_HOST}", default="")
Patrick Williams20f38712022-12-08 06:18:26 -0600265 device_username = BuiltIn().get_variable_value(
266 "${DEVICE_USERNAME}", default=""
267 )
268 device_password = BuiltIn().get_variable_value(
269 "${DEVICE_PASSWORD}", default=""
270 )
271 device_port = BuiltIn().get_variable_value("${DEVICE_PORT}", default="22")
Michael Walsh8243ac92018-05-02 13:47:07 -0500272
273 if not gv.valid_value(device_host):
274 return "", "", 1
275 if not gv.valid_value(device_username):
276 return "", "", 1
277 if not gv.valid_value(device_password):
278 return "", "", 1
279 if not gv.valid_value(device_port):
280 return "", "", 1
281
Patrick Williams20f38712022-12-08 06:18:26 -0600282 open_connection_args = {
283 "host": device_host,
284 "alias": "device_connection",
285 "port": device_port,
286 }
287 login_args = {"username": device_username, "password": device_password}
Michael Walsh8243ac92018-05-02 13:47:07 -0500288
Patrick Williams20f38712022-12-08 06:18:26 -0600289 return grs.execute_ssh_command(
290 cmd_buf,
291 open_connection_args,
292 login_args,
293 print_out,
294 print_err=0,
295 ignore_err=1,
296 fork=0,
297 quiet=quiet,
298 test_mode=test_mode,
299 )