blob: f5759fe2729d621c81fe7b9ba20e94e2184424dc [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="")
46 openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}",
47 default="")
48 openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}",
49 default="")
50
51 if not gv.valid_value(openbmc_host):
52 return "", "", 1
53 if not gv.valid_value(openbmc_username):
54 return "", "", 1
55 if not gv.valid_value(openbmc_password):
56 return "", "", 1
57
58 open_connection_args = {'host': openbmc_host, 'alias': 'bmc_connection',
59 'timeout': '25.0', 'prompt': '# '}
60 login_args = {'username': openbmc_username, 'password': openbmc_password}
61
62 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
63 print_out, print_err, ignore_err, fork,
Michael Walshc39c3722018-08-07 14:50:56 -050064 quiet, test_mode, time_out)
Michael Walsh3cc126a2017-06-30 17:02:10 -050065
Michael Walsh3cc126a2017-06-30 17:02:10 -050066
Michael Walsh3cc126a2017-06-30 17:02:10 -050067def os_execute_command(cmd_buf,
68 print_out=0,
69 print_err=0,
70 ignore_err=0,
71 fork=0,
72 quiet=None,
Michael Walshc39c3722018-08-07 14:50:56 -050073 test_mode=None,
74 time_out=None):
Michael Walsh3cc126a2017-06-30 17:02:10 -050075 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050076 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 -050077
78 This function will obtain the global values for OS_HOST, OS_USERNAME, etc.
79
80 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -050081 cmd_buf The command string to be run in an SSH session.
82 print_out If this is set, this function will print the stdout/stderr generated by
83 the shell command.
84 print_err If show_err is set, this function will print a standardized error report
85 if the shell command returns non-zero.
86 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
87 ignored.
88 fork Indicates that sshlib.start is to be used rather than
89 sshlib.execute_command.
90 quiet Indicates whether this function should run the pissuing() function prints
91 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
92 value.
93 test_mode If test_mode is set, this function will not actually run the command.
94 This defaults to the global test_mode value.
95 time_out The amount of time to allow for the execution of cmd_buf. A value of
96 None means that there is no limit to how long the command may take.
Michael Walsh3cc126a2017-06-30 17:02:10 -050097 """
98
99 # Get global OS variable values.
100 os_host = BuiltIn().get_variable_value("${OS_HOST}", default="")
101 os_username = BuiltIn().get_variable_value("${OS_USERNAME}",
102 default="")
103 os_password = BuiltIn().get_variable_value("${OS_PASSWORD}",
104 default="")
105
106 if not gv.valid_value(os_host):
107 return "", "", 1
108 if not gv.valid_value(os_username):
109 return "", "", 1
110 if not gv.valid_value(os_password):
111 return "", "", 1
112
113 open_connection_args = {'host': os_host, 'alias': 'os_connection'}
114 login_args = {'username': os_username, 'password': os_password}
115
116 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
117 print_out, print_err, ignore_err, fork,
Michael Walshc39c3722018-08-07 14:50:56 -0500118 quiet, test_mode, time_out)
Michael Walsh3cc126a2017-06-30 17:02:10 -0500119
Michael Walsh78fa29a2017-07-31 15:35:02 -0500120
Michael Walsh78fa29a2017-07-31 15:35:02 -0500121def xcat_execute_command(cmd_buf,
122 print_out=0,
123 print_err=0,
124 ignore_err=0,
125 fork=0,
126 quiet=None,
127 test_mode=None):
Michael Walsh78fa29a2017-07-31 15:35:02 -0500128 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500129 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 -0500130
Michael Walsh410b1782019-10-22 15:56:18 -0500131 This function will obtain the global values for XCAT_HOST, XCAT_USERNAME, etc.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500132
133 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500134 cmd_buf The command string to be run in an SSH session.
135 print_out If this is set, this function will print the stdout/stderr generated by
136 the shell command.
137 print_err If show_err is set, this function will print a standardized error report
138 if the shell command returns non-zero.
139 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
140 ignored.
141 fork Indicates that sshlib.start is to be used rather than
142 sshlib.execute_command.
143 quiet Indicates whether this function should run the pissuing() function prints
144 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
145 value.
146 test_mode If test_mode is set, this function will not actually run the command.
147 This defaults to the global test_mode value.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500148 """
149
150 # Get global XCAT variable values.
151 xcat_host = BuiltIn().get_variable_value("${XCAT_HOST}", default="")
152 xcat_username = BuiltIn().get_variable_value("${XCAT_USERNAME}",
153 default="")
154 xcat_password = BuiltIn().get_variable_value("${XCAT_PASSWORD}",
155 default="")
156 xcat_port = BuiltIn().get_variable_value("${XCAT_PORT}",
157 default="22")
158
159 if not gv.valid_value(xcat_host):
160 return "", "", 1
161 if not gv.valid_value(xcat_username):
162 return "", "", 1
163 if not gv.valid_value(xcat_password):
164 return "", "", 1
165 if not gv.valid_value(xcat_port):
166 return "", "", 1
167
168 open_connection_args = {'host': xcat_host, 'alias': 'xcat_connection',
169 'port': xcat_port}
170 login_args = {'username': xcat_username, 'password': xcat_password}
171
172 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
173 print_out, print_err, ignore_err, fork,
174 quiet, test_mode)
Michael Walsh8243ac92018-05-02 13:47:07 -0500175
176
177def device_write(cmd_buf,
178 print_out=0,
179 quiet=None,
180 test_mode=None):
181 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500182 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 -0500183
184 This function is useful for writing to a switch.
185
Michael Walsh410b1782019-10-22 15:56:18 -0500186 This function will obtain the global values for DEVICE_HOST, DEVICE_USERNAME, etc.
Michael Walsh8243ac92018-05-02 13:47:07 -0500187
188 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500189 cmd_buf The command string to be run in an SSH session.
190 print_out If this is set, this function will print the stdout/stderr generated by
191 the shell command.
192 print_err If show_err is set, this function will print a standardized error report
193 if the shell command returns non-zero.
194 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
195 ignored.
196 fork Indicates that sshlib.start is to be used rather than
197 sshlib.execute_command.
198 quiet Indicates whether this function should run the pissuing() function prints
199 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
200 value.
201 test_mode If test_mode is set, this function will not actually run the command.
202 This defaults to the global test_mode value.
Michael Walsh8243ac92018-05-02 13:47:07 -0500203 """
204
205 # Get global DEVICE variable values.
206 device_host = BuiltIn().get_variable_value("${DEVICE_HOST}", default="")
207 device_username = BuiltIn().get_variable_value("${DEVICE_USERNAME}",
208 default="")
209 device_password = BuiltIn().get_variable_value("${DEVICE_PASSWORD}",
210 default="")
211 device_port = BuiltIn().get_variable_value("${DEVICE_PORT}",
212 default="22")
213
214 if not gv.valid_value(device_host):
215 return "", "", 1
216 if not gv.valid_value(device_username):
217 return "", "", 1
218 if not gv.valid_value(device_password):
219 return "", "", 1
220 if not gv.valid_value(device_port):
221 return "", "", 1
222
223 open_connection_args = {'host': device_host, 'alias': 'device_connection',
224 'port': device_port}
225 login_args = {'username': device_username, 'password': device_password}
226
227 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
228 print_out, print_err=0, ignore_err=1,
229 fork=0, quiet=quiet, test_mode=test_mode)