blob: f9aaeb6f5749b71b7a6a58150c1c8f0b9db614b6 [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,
77 time_out=None):
Michael Walsh3cc126a2017-06-30 17:02:10 -050078 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050079 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 -050080
81 This function will obtain the global values for OS_HOST, OS_USERNAME, etc.
82
83 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -050084 cmd_buf The command string to be run in an SSH session.
85 print_out If this is set, this function will print the stdout/stderr generated by
86 the shell command.
87 print_err If show_err is set, this function will print a standardized error report
88 if the shell command returns non-zero.
89 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
90 ignored.
91 fork Indicates that sshlib.start is to be used rather than
92 sshlib.execute_command.
93 quiet Indicates whether this function should run the pissuing() function prints
94 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
95 value.
96 test_mode If test_mode is set, this function will not actually run the command.
97 This defaults to the global test_mode value.
98 time_out The amount of time to allow for the execution of cmd_buf. A value of
99 None means that there is no limit to how long the command may take.
Michael Walsh3cc126a2017-06-30 17:02:10 -0500100 """
101
102 # Get global OS variable values.
103 os_host = BuiltIn().get_variable_value("${OS_HOST}", default="")
104 os_username = BuiltIn().get_variable_value("${OS_USERNAME}",
105 default="")
106 os_password = BuiltIn().get_variable_value("${OS_PASSWORD}",
107 default="")
108
109 if not gv.valid_value(os_host):
110 return "", "", 1
111 if not gv.valid_value(os_username):
112 return "", "", 1
113 if not gv.valid_value(os_password):
114 return "", "", 1
115
116 open_connection_args = {'host': os_host, 'alias': 'os_connection'}
117 login_args = {'username': os_username, 'password': os_password}
118
119 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
120 print_out, print_err, ignore_err, fork,
Michael Walshc39c3722018-08-07 14:50:56 -0500121 quiet, test_mode, time_out)
Michael Walsh3cc126a2017-06-30 17:02:10 -0500122
Michael Walsh78fa29a2017-07-31 15:35:02 -0500123
Michael Walsh78fa29a2017-07-31 15:35:02 -0500124def xcat_execute_command(cmd_buf,
125 print_out=0,
126 print_err=0,
127 ignore_err=0,
128 fork=0,
129 quiet=None,
130 test_mode=None):
Michael Walsh78fa29a2017-07-31 15:35:02 -0500131 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500132 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 -0500133
Michael Walsh410b1782019-10-22 15:56:18 -0500134 This function will obtain the global values for XCAT_HOST, XCAT_USERNAME, etc.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500135
136 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500137 cmd_buf The command string to be run in an SSH session.
138 print_out If this is set, this function will print the stdout/stderr generated by
139 the shell command.
140 print_err If show_err is set, this function will print a standardized error report
141 if the shell command returns non-zero.
142 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
143 ignored.
144 fork Indicates that sshlib.start is to be used rather than
145 sshlib.execute_command.
146 quiet Indicates whether this function should run the pissuing() function prints
147 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
148 value.
149 test_mode If test_mode is set, this function will not actually run the command.
150 This defaults to the global test_mode value.
Michael Walsh78fa29a2017-07-31 15:35:02 -0500151 """
152
153 # Get global XCAT variable values.
154 xcat_host = BuiltIn().get_variable_value("${XCAT_HOST}", default="")
155 xcat_username = BuiltIn().get_variable_value("${XCAT_USERNAME}",
156 default="")
157 xcat_password = BuiltIn().get_variable_value("${XCAT_PASSWORD}",
158 default="")
159 xcat_port = BuiltIn().get_variable_value("${XCAT_PORT}",
160 default="22")
161
162 if not gv.valid_value(xcat_host):
163 return "", "", 1
164 if not gv.valid_value(xcat_username):
165 return "", "", 1
166 if not gv.valid_value(xcat_password):
167 return "", "", 1
168 if not gv.valid_value(xcat_port):
169 return "", "", 1
170
171 open_connection_args = {'host': xcat_host, 'alias': 'xcat_connection',
172 'port': xcat_port}
173 login_args = {'username': xcat_username, 'password': xcat_password}
174
175 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
176 print_out, print_err, ignore_err, fork,
177 quiet, test_mode)
Michael Walsh8243ac92018-05-02 13:47:07 -0500178
179
180def device_write(cmd_buf,
181 print_out=0,
182 quiet=None,
183 test_mode=None):
184 r"""
Michael Walsh410b1782019-10-22 15:56:18 -0500185 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 -0500186
187 This function is useful for writing to a switch.
188
Michael Walsh410b1782019-10-22 15:56:18 -0500189 This function will obtain the global values for DEVICE_HOST, DEVICE_USERNAME, etc.
Michael Walsh8243ac92018-05-02 13:47:07 -0500190
191 Description of arguments:
Michael Walsh410b1782019-10-22 15:56:18 -0500192 cmd_buf The command string to be run in an SSH session.
193 print_out If this is set, this function will print the stdout/stderr generated by
194 the shell command.
195 print_err If show_err is set, this function will print a standardized error report
196 if the shell command returns non-zero.
197 ignore_err Indicates that errors encountered on the sshlib.execute_command are to be
198 ignored.
199 fork Indicates that sshlib.start is to be used rather than
200 sshlib.execute_command.
201 quiet Indicates whether this function should run the pissuing() function prints
202 an "Issuing: <cmd string>" to stdout. This defaults to the global quiet
203 value.
204 test_mode If test_mode is set, this function will not actually run the command.
205 This defaults to the global test_mode value.
Michael Walsh8243ac92018-05-02 13:47:07 -0500206 """
207
208 # Get global DEVICE variable values.
209 device_host = BuiltIn().get_variable_value("${DEVICE_HOST}", default="")
210 device_username = BuiltIn().get_variable_value("${DEVICE_USERNAME}",
211 default="")
212 device_password = BuiltIn().get_variable_value("${DEVICE_PASSWORD}",
213 default="")
214 device_port = BuiltIn().get_variable_value("${DEVICE_PORT}",
215 default="22")
216
217 if not gv.valid_value(device_host):
218 return "", "", 1
219 if not gv.valid_value(device_username):
220 return "", "", 1
221 if not gv.valid_value(device_password):
222 return "", "", 1
223 if not gv.valid_value(device_port):
224 return "", "", 1
225
226 open_connection_args = {'host': device_host, 'alias': 'device_connection',
227 'port': device_port}
228 login_args = {'username': device_username, 'password': device_password}
229
230 return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
231 print_out, print_err=0, ignore_err=1,
232 fork=0, quiet=quiet, test_mode=test_mode)