xref: /openbmc/openbmc-test-automation/lib/bmc_ssh_utils.py (revision 3cc126a30076ed1f9f04690b39d098d40e79faf8)
1#!/usr/bin/env python
2
3r"""
4This module provides many valuable bmc ssh functions such as
5bmc_execute_command.
6"""
7
8import sys
9import exceptions
10import re
11import gen_print as gp
12import gen_valid as gv
13import gen_robot_ssh as grs
14from robot.libraries.BuiltIn import BuiltIn
15
16
17###############################################################################
18def bmc_execute_command(cmd_buf,
19                        print_out=0,
20                        print_err=0,
21                        ignore_err=0,
22                        fork=0,
23                        quiet=None,
24                        test_mode=None):
25
26    r"""
27    Run the given command in an BMC SSH session and return the stdout, stderr
28    and the return code.
29
30    This function will obtain the global values for OPENBMC_HOST,
31    OPENBMC_USERNAME, etc.
32
33    Description of arguments:
34    cmd_buf                         The command string to be run in an SSH
35                                    session.
36    print_out                       If this is set, this function will print
37                                    the stdout/stderr generated by the shell
38                                    command.
39    print_err                       If show_err is set, this function will
40                                    print a standardized error report if the
41                                    shell command returns non-zero.
42    ignore_err                      Indicates that errors encountered on the
43                                    sshlib.execute_command are to be ignored.
44    fork                            Indicates that sshlib.start is to be used
45                                    rather than sshlib.execute_command.
46    quiet                           Indicates whether this function should run
47                                    the pissuing() function prints an
48                                    "Issuing: <cmd string>" to stdout.  This
49                                    defaults to the global quiet value.
50    test_mode                       If test_mode is set, this function will
51                                    not actually run the command.  This
52                                    defaults to the global test_mode value.
53    """
54
55    # Get global BMC variable values.
56    openbmc_host = BuiltIn().get_variable_value("${OPENBMC_HOST}", default="")
57    openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}",
58                                                    default="")
59    openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}",
60                                                    default="")
61
62    if not gv.valid_value(openbmc_host):
63        return "", "", 1
64    if not gv.valid_value(openbmc_username):
65        return "", "", 1
66    if not gv.valid_value(openbmc_password):
67        return "", "", 1
68
69    open_connection_args = {'host': openbmc_host, 'alias': 'bmc_connection',
70                            'timeout': '25.0', 'prompt': '# '}
71    login_args = {'username': openbmc_username, 'password': openbmc_password}
72
73    return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
74                                   print_out, print_err, ignore_err, fork,
75                                   quiet, test_mode)
76
77###############################################################################
78
79
80###############################################################################
81def os_execute_command(cmd_buf,
82                       print_out=0,
83                       print_err=0,
84                       ignore_err=0,
85                       fork=0,
86                       quiet=None,
87                       test_mode=None):
88
89    r"""
90    Run the given command in an OS SSH session and return the stdout, stderr
91    and the return code.
92
93    This function will obtain the global values for OS_HOST, OS_USERNAME, etc.
94
95    Description of arguments:
96    cmd_buf                         The command string to be run in an SSH
97                                    session.
98    print_out                       If this is set, this function will print
99                                    the stdout/stderr generated by the shell
100                                    command.
101    print_err                       If show_err is set, this function will
102                                    print a standardized error report if the
103                                    shell command returns non-zero.
104    ignore_err                      Indicates that errors encountered on the
105                                    sshlib.execute_command are to be ignored.
106    fork                            Indicates that sshlib.start is to be used
107                                    rather than sshlib.execute_command.
108    quiet                           Indicates whether this function should run
109                                    the pissuing() function prints an
110                                    "Issuing: <cmd string>" to stdout.  This
111                                    defaults to the global quiet value.
112    test_mode                       If test_mode is set, this function will
113                                    not actually run the command.  This
114                                    defaults to the global test_mode value.
115    """
116
117    # Get global OS variable values.
118    os_host = BuiltIn().get_variable_value("${OS_HOST}", default="")
119    os_username = BuiltIn().get_variable_value("${OS_USERNAME}",
120                                               default="")
121    os_password = BuiltIn().get_variable_value("${OS_PASSWORD}",
122                                               default="")
123
124    if not gv.valid_value(os_host):
125        return "", "", 1
126    if not gv.valid_value(os_username):
127        return "", "", 1
128    if not gv.valid_value(os_password):
129        return "", "", 1
130
131    open_connection_args = {'host': os_host, 'alias': 'os_connection'}
132    login_args = {'username': os_username, 'password': os_password}
133
134    return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
135                                   print_out, print_err, ignore_err, fork,
136                                   quiet, test_mode)
137
138###############################################################################
139