blob: fdd376fb6ba27d86d670abea44315018cd5aaba0 [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
Michael Walsh3cc126a2017-06-30 17:02:10 -05008import gen_valid as gv
9import gen_robot_ssh as grs
10from robot.libraries.BuiltIn import BuiltIn
11
12
Michael Walsh3cc126a2017-06-30 17:02:10 -050013def bmc_execute_command(cmd_buf,
14 print_out=0,
15 print_err=0,
16 ignore_err=0,
17 fork=0,
18 quiet=None,
Michael Walshc39c3722018-08-07 14:50:56 -050019 test_mode=None,
20 time_out=None):
Michael Walsh3cc126a2017-06-30 17:02:10 -050021 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050022 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 -050023
Michael Walsh410b1782019-10-22 15:56:18 -050024 This function will obtain the global values for OPENBMC_HOST, OPENBMC_USERNAME, etc.
Michael Walsh3cc126a2017-06-30 17:02:10 -050025
26 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -050027 cmd_buf The command string to be run in an SSH session.
28 print_out If this is set, this function will print the stdout/stderr generated by
29 the shell command.
30 print_err If show_err is set, this function will print a standardized error report
31 if the shell command returns non-zero.
32 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
33 ignored.
34 fork Indicates that sshlib.start is to be used rather than
35 sshlib.execute_command.
36 quiet Indicates whether this function should run the pissuing() function prints
37 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
38 value.
39 test_mode If test_mode is set, this function will not actually run the command.
40 This defaults to the global test_mode value.
41 time_out The amount of time to allow for the execution of cmd_buf. A value of
42 None means that there is no limit to how long the command may take.
Michael Walsh3cc126a2017-06-30 17:02:10 -050043 """
44
45 # Get global BMC variable values.
46 openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}", default="")
Michael Walsh046fe222019-11-08 14:33:53 -060047 ssh_port = BuiltIn().get_variable_value("${SSH_PORT}", default="22")
Michael Walsh3cc126a2017-06-30 17:02:10 -050048 openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}",
49 default="")
50 openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}",
51 default="")
52
53 if not gv.valid_value(openbmc_host):
54 return "", "", 1
55 if not gv.valid_value(openbmc_username):
56 return "", "", 1
57 if not gv.valid_value(openbmc_password):
58 return "", "", 1
Michael Walsh046fe222019-11-08 14:33:53 -060059 if not gv.valid_value(ssh_port):
60 return "", "", 1
Michael Walsh3cc126a2017-06-30 17:02:10 -050061
62 open_connection_args = {'host': openbmc_host, 'alias': 'bmc_connection',
Michael Walsh046fe222019-11-08 14:33:53 -060063 'timeout': '25.0', 'prompt': '# ', 'port': ssh_port}
Michael Walsh3cc126a2017-06-30 17:02:10 -050064 login_args = {'username': openbmc_username, 'password': openbmc_password}
65
George Keishing2104d5f2022-01-31 04:33:30 -060066 openbmc_user_type = os.environ.get('USER_TYPE', "") or \
67 BuiltIn().get_variable_value("${USER_TYPE}", default="")
68 if openbmc_user_type == 'sudo':
George Keishing389e7492022-04-04 09:23:55 -050069 cmd_buf = 'sudo -i ' + cmd_buf
Michael Walsh3cc126a2017-06-30 17:02:10 -050070 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
71 print_out, print_err, ignore_err, fork,
Michael Walshc39c3722018-08-07 14:50:56 -050072 quiet, test_mode, time_out)
Michael Walsh3cc126a2017-06-30 17:02:10 -050073
Michael Walsh3cc126a2017-06-30 17:02:10 -050074
Michael Walsh3cc126a2017-06-30 17:02:10 -050075def os_execute_command(cmd_buf,
76 print_out=0,
77 print_err=0,
78 ignore_err=0,
79 fork=0,
80 quiet=None,
Michael Walshc39c3722018-08-07 14:50:56 -050081 test_mode=None,
Michael Shepos3390c852021-02-11 00:12:31 -060082 time_out=None,
83 os_host="",
84 os_username="",
85 os_password=""):
Michael Walsh3cc126a2017-06-30 17:02:10 -050086 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050087 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 -050088
89 This function will obtain the global values for OS_HOST, OS_USERNAME, etc.
90
91 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -050092 cmd_buf The command string to be run in an SSH session.
93 print_out If this is set, this function will print the stdout/stderr generated by
94 the shell command.
95 print_err If show_err is set, this function will print a standardized error report
96 if the shell command returns non-zero.
97 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
98 ignored.
99 fork Indicates that sshlib.start is to be used rather than
100 sshlib.execute_command.
101 quiet Indicates whether this function should run the pissuing() function prints
102 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
103 value.
104 test_mode If test_mode is set, this function will not actually run the command.
105 This defaults to the global test_mode value.
106 time_out The amount of time to allow for the execution of cmd_buf. A value of
107 None means that there is no limit to how long the command may take.
Michael Walsh3cc126a2017-06-30 17:02:10 -0500108 """
109
110 # Get global OS variable values.
Michael Shepos3390c852021-02-11 00:12:31 -0600111 if os_host == "":
112 os_host = BuiltIn().get_variable_value("${OS_HOST}", default="")
Michael Shepos632f9a42021-02-24 08:21:59 -0600113 if os_username == "":
Michael Shepos3390c852021-02-11 00:12:31 -0600114 os_username = BuiltIn().get_variable_value("${OS_USERNAME}", default="")
Michael Shepos632f9a42021-02-24 08:21:59 -0600115 if os_password == "":
Michael Shepos3390c852021-02-11 00:12:31 -0600116 os_password = BuiltIn().get_variable_value("${OS_PASSWORD}", default="")
Michael Walsh3cc126a2017-06-30 17:02:10 -0500117
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,
Michael Walshc39c3722018-08-07 14:50:56 -0500130 quiet, test_mode, time_out)
Michael Walsh3cc126a2017-06-30 17:02:10 -0500131
Michael Walsh78fa29a2017-07-31 15:35:02 -0500132
Michael Walsh78fa29a2017-07-31 15:35:02 -0500133def 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 Walsh78fa29a2017-07-31 15:35:02 -0500140 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500141 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 -0500142
Michael Walsh410b1782019-10-22 15:56:18 -0500143 This function will obtain the global values for XCAT_HOST, XCAT_USERNAME, etc.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500144
145 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500146 cmd_buf The command string to be run in an SSH session.
147 print_out If this is set, this function will print the stdout/stderr generated by
148 the shell command.
149 print_err If show_err is set, this function will print a standardized error report
150 if the shell command returns non-zero.
151 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
152 ignored.
153 fork Indicates that sshlib.start is to be used rather than
154 sshlib.execute_command.
155 quiet Indicates whether this function should run the pissuing() function prints
156 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
157 value.
158 test_mode If test_mode is set, this function will not actually run the command.
159 This defaults to the global test_mode value.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500160 """
161
162 # Get global XCAT variable values.
163 xcat_host = BuiltIn().get_variable_value("${XCAT_HOST}", default="")
164 xcat_username = BuiltIn().get_variable_value("${XCAT_USERNAME}",
165 default="")
166 xcat_password = BuiltIn().get_variable_value("${XCAT_PASSWORD}",
167 default="")
168 xcat_port = BuiltIn().get_variable_value("${XCAT_PORT}",
169 default="22")
170
171 if not gv.valid_value(xcat_host):
172 return "", "", 1
173 if not gv.valid_value(xcat_username):
174 return "", "", 1
175 if not gv.valid_value(xcat_password):
176 return "", "", 1
177 if not gv.valid_value(xcat_port):
178 return "", "", 1
179
180 open_connection_args = {'host': xcat_host, 'alias': 'xcat_connection',
181 'port': xcat_port}
182 login_args = {'username': xcat_username, 'password': xcat_password}
183
184 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
185 print_out, print_err, ignore_err, fork,
186 quiet, test_mode)
Michael Walsh8243ac92018-05-02 13:47:07 -0500187
188
189def device_write(cmd_buf,
190 print_out=0,
191 quiet=None,
192 test_mode=None):
193 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500194 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 -0500195
196 This function is useful for writing to a switch.
197
Michael Walsh410b1782019-10-22 15:56:18 -0500198 This function will obtain the global values for DEVICE_HOST, DEVICE_USERNAME, etc.
Michael Walsh8243ac92018-05-02 13:47:07 -0500199
200 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500201 cmd_buf The command string to be run in an SSH session.
202 print_out If this is set, this function will print the stdout/stderr generated by
203 the shell command.
204 print_err If show_err is set, this function will print a standardized error report
205 if the shell command returns non-zero.
206 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
207 ignored.
208 fork Indicates that sshlib.start is to be used rather than
209 sshlib.execute_command.
210 quiet Indicates whether this function should run the pissuing() function prints
211 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
212 value.
213 test_mode If test_mode is set, this function will not actually run the command.
214 This defaults to the global test_mode value.
Michael Walsh8243ac92018-05-02 13:47:07 -0500215 """
216
217 # Get global DEVICE variable values.
218 device_host = BuiltIn().get_variable_value("${DEVICE_HOST}", default="")
219 device_username = BuiltIn().get_variable_value("${DEVICE_USERNAME}",
220 default="")
221 device_password = BuiltIn().get_variable_value("${DEVICE_PASSWORD}",
222 default="")
223 device_port = BuiltIn().get_variable_value("${DEVICE_PORT}",
224 default="22")
225
226 if not gv.valid_value(device_host):
227 return "", "", 1
228 if not gv.valid_value(device_username):
229 return "", "", 1
230 if not gv.valid_value(device_password):
231 return "", "", 1
232 if not gv.valid_value(device_port):
233 return "", "", 1
234
235 open_connection_args = {'host': device_host, 'alias': 'device_connection',
236 'port': device_port}
237 login_args = {'username': device_username, 'password': device_password}
238
239 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
240 print_out, print_err=0, ignore_err=1,
241 fork=0, quiet=quiet, test_mode=test_mode)