blob: c82788f9fcb28ab44ab7bf2df4334da7d7d16639 [file] [log] [blame]
Michael Walsh3cc126a2017-06-30 17:02:10 -05001#!/usr/bin/env python
2
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
Michael Walsh3cc126a2017-06-30 17:02:10 -05007import gen_valid as gv
8import gen_robot_ssh as grs
9from robot.libraries.BuiltIn import BuiltIn
10
11
Michael Walsh3cc126a2017-06-30 17:02:10 -050012def bmc_execute_command(cmd_buf,
13 print_out=0,
14 print_err=0,
15 ignore_err=0,
16 fork=0,
17 quiet=None,
Michael Walshc39c3722018-08-07 14:50:56 -050018 test_mode=None,
19 time_out=None):
Michael Walsh3cc126a2017-06-30 17:02:10 -050020 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050021 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 -050022
Michael Walsh410b1782019-10-22 15:56:18 -050023 This function will obtain the global values for OPENBMC_HOST, OPENBMC_USERNAME, etc.
Michael Walsh3cc126a2017-06-30 17:02:10 -050024
25 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -050026 cmd_buf The command string to be run in an SSH session.
27 print_out If this is set, this function will print the stdout/stderr generated by
28 the shell command.
29 print_err If show_err is set, this function will print a standardized error report
30 if the shell command returns non-zero.
31 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
32 ignored.
33 fork Indicates that sshlib.start is to be used rather than
34 sshlib.execute_command.
35 quiet Indicates whether this function should run the pissuing() function prints
36 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
37 value.
38 test_mode If test_mode is set, this function will not actually run the command.
39 This defaults to the global test_mode value.
40 time_out The amount of time to allow for the execution of cmd_buf. A value of
41 None means that there is no limit to how long the command may take.
Michael Walsh3cc126a2017-06-30 17:02:10 -050042 """
43
44 # Get global BMC variable values.
45 openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}", default="")
Michael Walsh046fe222019-11-08 14:33:53 -060046 ssh_port = BuiltIn().get_variable_value("${SSH_PORT}", default="22")
Michael Walsh3cc126a2017-06-30 17:02:10 -050047 openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}",
48 default="")
49 openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}",
50 default="")
51
52 if not gv.valid_value(openbmc_host):
53 return "", "", 1
54 if not gv.valid_value(openbmc_username):
55 return "", "", 1
56 if not gv.valid_value(openbmc_password):
57 return "", "", 1
Michael Walsh046fe222019-11-08 14:33:53 -060058 if not gv.valid_value(ssh_port):
59 return "", "", 1
Michael Walsh3cc126a2017-06-30 17:02:10 -050060
61 open_connection_args = {'host': openbmc_host, 'alias': 'bmc_connection',
Michael Walsh046fe222019-11-08 14:33:53 -060062 'timeout': '25.0', 'prompt': '# ', 'port': ssh_port}
Michael Walsh3cc126a2017-06-30 17:02:10 -050063 login_args = {'username': openbmc_username, 'password': openbmc_password}
64
65 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
66 print_out, print_err, ignore_err, fork,
Michael Walshc39c3722018-08-07 14:50:56 -050067 quiet, test_mode, time_out)
Michael Walsh3cc126a2017-06-30 17:02:10 -050068
Michael Walsh3cc126a2017-06-30 17:02:10 -050069
Michael Walsh3cc126a2017-06-30 17:02:10 -050070def os_execute_command(cmd_buf,
71 print_out=0,
72 print_err=0,
73 ignore_err=0,
74 fork=0,
75 quiet=None,
Michael Walshc39c3722018-08-07 14:50:56 -050076 test_mode=None,
Michael Shepos3390c852021-02-11 00:12:31 -060077 time_out=None,
78 os_host="",
79 os_username="",
80 os_password=""):
81
Michael Walsh3cc126a2017-06-30 17:02:10 -050082 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050083 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 -050084
85 This function will obtain the global values for OS_HOST, OS_USERNAME, etc.
86
87 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -050088 cmd_buf The command string to be run in an SSH session.
89 print_out If this is set, this function will print the stdout/stderr generated by
90 the shell command.
91 print_err If show_err is set, this function will print a standardized error report
92 if the shell command returns non-zero.
93 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
94 ignored.
95 fork Indicates that sshlib.start is to be used rather than
96 sshlib.execute_command.
97 quiet Indicates whether this function should run the pissuing() function prints
98 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
99 value.
100 test_mode If test_mode is set, this function will not actually run the command.
101 This defaults to the global test_mode value.
102 time_out The amount of time to allow for the execution of cmd_buf. A value of
103 None means that there is no limit to how long the command may take.
Michael Walsh3cc126a2017-06-30 17:02:10 -0500104 """
105
106 # Get global OS variable values.
Michael Shepos3390c852021-02-11 00:12:31 -0600107 if os_host == "":
108 os_host = BuiltIn().get_variable_value("${OS_HOST}", default="")
Michael Shepos632f9a42021-02-24 08:21:59 -0600109 if os_username == "":
Michael Shepos3390c852021-02-11 00:12:31 -0600110 os_username = BuiltIn().get_variable_value("${OS_USERNAME}", default="")
Michael Shepos632f9a42021-02-24 08:21:59 -0600111 if os_password == "":
Michael Shepos3390c852021-02-11 00:12:31 -0600112 os_password = BuiltIn().get_variable_value("${OS_PASSWORD}", default="")
Michael Walsh3cc126a2017-06-30 17:02:10 -0500113
114 if not gv.valid_value(os_host):
115 return "", "", 1
116 if not gv.valid_value(os_username):
117 return "", "", 1
118 if not gv.valid_value(os_password):
119 return "", "", 1
120
121 open_connection_args = {'host': os_host, 'alias': 'os_connection'}
122 login_args = {'username': os_username, 'password': os_password}
123
124 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
125 print_out, print_err, ignore_err, fork,
Michael Walshc39c3722018-08-07 14:50:56 -0500126 quiet, test_mode, time_out)
Michael Walsh3cc126a2017-06-30 17:02:10 -0500127
Michael Walsh78fa29a2017-07-31 15:35:02 -0500128
Michael Walsh78fa29a2017-07-31 15:35:02 -0500129def xcat_execute_command(cmd_buf,
130 print_out=0,
131 print_err=0,
132 ignore_err=0,
133 fork=0,
134 quiet=None,
135 test_mode=None):
Michael Walsh78fa29a2017-07-31 15:35:02 -0500136 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500137 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 -0500138
Michael Walsh410b1782019-10-22 15:56:18 -0500139 This function will obtain the global values for XCAT_HOST, XCAT_USERNAME, etc.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500140
141 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500142 cmd_buf The command string to be run in an SSH session.
143 print_out If this is set, this function will print the stdout/stderr generated by
144 the shell command.
145 print_err If show_err is set, this function will print a standardized error report
146 if the shell command returns non-zero.
147 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
148 ignored.
149 fork Indicates that sshlib.start is to be used rather than
150 sshlib.execute_command.
151 quiet Indicates whether this function should run the pissuing() function prints
152 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
153 value.
154 test_mode If test_mode is set, this function will not actually run the command.
155 This defaults to the global test_mode value.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500156 """
157
158 # Get global XCAT variable values.
159 xcat_host = BuiltIn().get_variable_value("${XCAT_HOST}", default="")
160 xcat_username = BuiltIn().get_variable_value("${XCAT_USERNAME}",
161 default="")
162 xcat_password = BuiltIn().get_variable_value("${XCAT_PASSWORD}",
163 default="")
164 xcat_port = BuiltIn().get_variable_value("${XCAT_PORT}",
165 default="22")
166
167 if not gv.valid_value(xcat_host):
168 return "", "", 1
169 if not gv.valid_value(xcat_username):
170 return "", "", 1
171 if not gv.valid_value(xcat_password):
172 return "", "", 1
173 if not gv.valid_value(xcat_port):
174 return "", "", 1
175
176 open_connection_args = {'host': xcat_host, 'alias': 'xcat_connection',
177 'port': xcat_port}
178 login_args = {'username': xcat_username, 'password': xcat_password}
179
180 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
181 print_out, print_err, ignore_err, fork,
182 quiet, test_mode)
Michael Walsh8243ac92018-05-02 13:47:07 -0500183
184
185def device_write(cmd_buf,
186 print_out=0,
187 quiet=None,
188 test_mode=None):
189 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500190 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 -0500191
192 This function is useful for writing to a switch.
193
Michael Walsh410b1782019-10-22 15:56:18 -0500194 This function will obtain the global values for DEVICE_HOST, DEVICE_USERNAME, etc.
Michael Walsh8243ac92018-05-02 13:47:07 -0500195
196 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500197 cmd_buf The command string to be run in an SSH session.
198 print_out If this is set, this function will print the stdout/stderr generated by
199 the shell command.
200 print_err If show_err is set, this function will print a standardized error report
201 if the shell command returns non-zero.
202 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
203 ignored.
204 fork Indicates that sshlib.start is to be used rather than
205 sshlib.execute_command.
206 quiet Indicates whether this function should run the pissuing() function prints
207 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
208 value.
209 test_mode If test_mode is set, this function will not actually run the command.
210 This defaults to the global test_mode value.
Michael Walsh8243ac92018-05-02 13:47:07 -0500211 """
212
213 # Get global DEVICE variable values.
214 device_host = BuiltIn().get_variable_value("${DEVICE_HOST}", default="")
215 device_username = BuiltIn().get_variable_value("${DEVICE_USERNAME}",
216 default="")
217 device_password = BuiltIn().get_variable_value("${DEVICE_PASSWORD}",
218 default="")
219 device_port = BuiltIn().get_variable_value("${DEVICE_PORT}",
220 default="22")
221
222 if not gv.valid_value(device_host):
223 return "", "", 1
224 if not gv.valid_value(device_username):
225 return "", "", 1
226 if not gv.valid_value(device_password):
227 return "", "", 1
228 if not gv.valid_value(device_port):
229 return "", "", 1
230
231 open_connection_args = {'host': device_host, 'alias': 'device_connection',
232 'port': device_port}
233 login_args = {'username': device_username, 'password': device_password}
234
235 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
236 print_out, print_err=0, ignore_err=1,
237 fork=0, quiet=quiet, test_mode=test_mode)