Add ipmi_cmd_type support to get_lan_print_dict

Support ipmi_cmd_tyep of either 'inband' or 'external' for function
get_lan_print_dict.  The value defaults to external.

Updated 'Test Inband IPMI Factory Reset' to call 'Get Lan Print Dict'
with ipmi_cmd_type=inband to maintain prior functionality.

Change-Id: I3676e8c86bb9464d1067f309252a7904df993ed2
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/ipmi_utils.py b/lib/ipmi_utils.py
index 9f30e29..0dd7f5b 100644
--- a/lib/ipmi_utils.py
+++ b/lib/ipmi_utils.py
@@ -7,6 +7,7 @@
 import re
 import gen_print as gp
 import gen_misc as gm
+import gen_cmd as gc
 import gen_robot_keyword as grk
 import gen_robot_utils as gru
 import bmc_ssh_utils as bsu
@@ -78,7 +79,52 @@
     return status
 
 
-def get_lan_print_dict():
+def execute_ipmi_cmd(cmd_string,
+                     ipmi_cmd_type='inband',
+                     print_output=1,
+                     ignore_err=0):
+    r"""
+    Run the given command string as an IPMI command and return the stdout,
+    stderr and the return code.
+
+    Description of argument(s):
+    cmd_string                      The command string to be run as an IPMI
+                                    command.
+    ipmi_cmd_type                   'inband' or 'external'.
+    print_output                    If this is set, this function will print
+                                    the stdout/stderr generated by
+                                    the IPMI command.
+    ignore_err                      Ignore error means that a failure
+                                    encountered by running the command
+                                    string will not be raised as a python
+                                    exception.
+    """
+
+    if ipmi_cmd_type == 'inband':
+        IPMI_INBAND_CMD = BuiltIn().get_variable_value("${IPMI_INBAND_CMD}")
+        cmd_buf = IPMI_INBAND_CMD + " " + cmd_string
+        return bsu.os_execute_command(cmd_buf,
+                                      print_out=print_output,
+                                      ignore_err=ignore_err)
+
+    if ipmi_cmd_type == 'external':
+        cmd_buf = BuiltIn().get_variable_value("${IPMI_EXT_CMD}")
+        IPMI_USER_OPTIONS =\
+            BuiltIn().get_variable_value("${IPMI_USER_OPTIONS}")
+        if IPMI_USER_OPTIONS != "":
+            cmd_buf += " " + IPMI_USER_OPTIONS
+        cmd_buf += " -P " + BuiltIn().get_variable_value("${IPMI_PASSWORD}")
+        cmd_buf += " " + BuiltIn().get_variable_value("${HOST}")
+        cmd_buf += " " + BuiltIn().get_variable_value("${OPENBMC_HOST}")
+        cmd_buf += " " + cmd_string
+        rc, stdout, stderr = gc.shell_cmd(cmd_buf,
+                                          print_output=print_output,
+                                          ignore_err=ignore_err,
+                                          return_stderr=1)
+        return stdout, stderr, rc
+
+
+def get_lan_print_dict(ipmi_cmd_type='external'):
     r"""
     Get IPMI 'lan print' output and return it as a dictionary.
 
@@ -120,22 +166,25 @@
       [Cipher Suite Priv Max]:                        Not Available
       [Bad Password Threshold]:                       Not Available
 
+    Description of argument(s):
+    ipmi_cmd_type                   The type of ipmi command to use (e.g.
+                                    'inband', 'external').
     """
 
-    IPMI_INBAND_CMD = BuiltIn().get_variable_value("${IPMI_INBAND_CMD}")
-
     # Notice in the example of data above that 'Auth Type Enable' needs some
     # special processing.  We essentially want to isolate its data and remove
     # the 'Auth Type Enable' string so that key_value_outbuf_to_dict can
     # process it as a sub-dictionary.
-    cmd_buf = IPMI_INBAND_CMD + " lan print | grep -E '^(Auth Type Enable)" +\
+    cmd_buf = "lan print | grep -E '^(Auth Type Enable)" +\
         "?[ ]+: ' | sed -re 's/^(Auth Type Enable)?[ ]+: //g'"
-    stdout1, stderr, rc = bsu.os_execute_command(cmd_buf)
+    stdout1, stderr, rc = execute_ipmi_cmd(cmd_buf, ipmi_cmd_type,
+                                           print_output=0)
 
     # Now get the remainder of the data and exclude the lines with no field
     # names (i.e. the 'Auth Type Enable' sub-fields).
-    cmd_buf = IPMI_INBAND_CMD + " lan print | grep -E -v '^[ ]+: '"
-    stdout2, stderr, rc = bsu.os_execute_command(cmd_buf)
+    cmd_buf = "lan print | grep -E -v '^[ ]+: '"
+    stdout2, stderr, rc = execute_ipmi_cmd(cmd_buf, ipmi_cmd_type,
+                                           print_output=0)
 
     # Make auth_type_enable_dict sub-dictionary...
     auth_type_enable_dict = vf.key_value_outbuf_to_dict(stdout1, to_lower=0,
diff --git a/openpower/test_inband_factory_reset.robot b/openpower/test_inband_factory_reset.robot
index fd3344a..5983331 100644
--- a/openpower/test_inband_factory_reset.robot
+++ b/openpower/test_inband_factory_reset.robot
@@ -18,7 +18,7 @@
 
     REST Power On  stack_mode=skip
 
-    ${network_info}=  Get Lan Print Dict
+    ${network_info}=  Get Lan Print Dict  ipmi_cmd_type=inband
     Should Not Be Empty  ${network_info}
 
     # Call reset method.