cancel
Showing results for 
Search instead for 
Did you mean: 

Adding DASH to Python Applications

Ganesh_Kamath
5 0 3,292

Adding DASH to Python Applications

 

DASH CLI 2.2 supports new options to enable developers to send request query in JSON format to DASH systems and get the output in JSON format. The output can be interpreted by any programming language supporting JSON format. The output JSON is modeled similar to Redfish Schema published by DMTF.

 

In typical usage, DASH CLI is launched with request JSON. DASH CLI processes the request and forms the response in JSON format. This response is sent back to the launching application.

 

JSON is supported by almost all modern programming languages. Hence it will be easier to add DASH capability into any application or tool with few simple steps. Since the application will be launching DASH CLI, the integration with the application is therefore ‘loosely coupled’. DASH communication, DASH standards compliance, DASH security & stability are encapsulated within DASH CLI. The calling application doesn’t have to know anything about DASH standard.

 

There are two new options in DASH CLI:

  • jdo option: With this flag, DASH request input is taken from command-line and output DASH response is written to console.
  • ji/jo option: With this flag, DASH request input is taken from file and output DASH response is written file.
  • jdo option is faster since it doesn’t involve any file operation.

 

Before starting

  • Download and install DASH CLI 2.2 version. In this blog, DASH CLI is installed in default path, “C:\Program Files (x86)\DASH CLI 2.2\”
  • Examples are illustrated with Notepad++ Editor. Any other IDE or editor can be used as well.
  • Refer DASH CLI Developer Guide, which is available in DASH CLI installation folder: “C:\Program Files (x86)\DASH CLI 2.2\docs\”, for JSON request & response formats for supported DASH profiles.
  • This blog provides the steps for Python language. Sample code for using -jdo, -ji & -jo options is attached.

 

JDO Flag usage

Using via direct command line

dashcli -jdo

When this command is run, DASH CLI waits for input in JSON format. Once the input is provided, it will be executed by DASH CLI and output of which is written back in JSON format, as shown in the screenshot below.

 

Image1_JDO.png

Using JDO programmatically

Create a sample Python application by following the steps below:

 

Step 1: Create a text file with the name “PythonSample_JDO.py” in Notepad++. Add the import statements from subprocess python library.

 

from subprocess import Popen, PIPE

 

Step 2: Add the two constant members InputFileName and dashCliPath.

 

InputFileName = 'input_json.txt'

dashCliPath = r'C:\Program Files (x86)\DASH CLI 2.2\bin\dashcli.exe'

 

Step 3: Declare a variable named cmd which has the executable path and flag to be passed (-jdo).

 

cmd = [dashCliPath, '-jdo']

 

Step 4: Use one ‘with’ statement to create a handle to DashCLI application, and one more ‘with’ statement to create a file handle to input_json.txt to read the input from file and pass the input to DashCLI application.

 

with Popen(cmd, stdin = PIPE) as dashCliProcessHandle:

    with open(InputFileName, 'r') as myfile:

        input=myfile.read().replace('\n', '')

        print("Input:")

        print(input)

        input_as_bytes = str.encode(input)

        dashCliProcessHandle.stdin.write(input_as_bytes)

    

 

Step 5: Within the outer ‘with’ statement, declare a variable named output to read the output or error messages from the application if any. Then print out the output of the execution.          

    output = ""

    if dashCliProcessHandle.stdout is not None:

        output += dashCliProcessHandle.stdout.read()

    if dashCliProcessHandle.stderr is not None:

        output += dashCliProcessHandle.stderr.read()

    print("Output:")

    print (output)

 

 

Step 6: Surround steps 3 through 5 with commands to indicate program execution with print messages. The entire code at this stage should look like so:

 

from subprocess import Popen, PIPE

InputFileName = 'input_json.txt'

dashCliPath = r'C:\Program Files (x86)\DASH CLI 2.2\bin\dashcli.exe'

 

print("Trying JDO Example...")

cmd = [dashCliPath, '-jdo']

with Popen(cmd, stdin = PIPE) as dashCliProcessHandle:

    with open(InputFileName, 'r') as myfile:

        input=myfile.read().replace('\n', '')

        print("Input:")

        print(input)

        input_as_bytes = str.encode(input)

        dashCliProcessHandle.stdin.write(input_as_bytes)

    

    output = ""

    if dashCliProcessHandle.stdout is not None:

        output += dashCliProcessHandle.stdout.read()

    if dashCliProcessHandle.stderr is not None:

        output += dashCliProcessHandle.stderr.read()

    print("Output:")

    print (output)

 

print("JDO Example Done.")

 

Step 7: Put the following JSON test in input_json.txt file:

 

{"h":"10.138.135.145","u":"Administrator","P":"********","Commands":["enumerate","computersystem"]}

 

 

Step 8: Run the PythonSample_JDO.py script from the console.

 Image2_Step8.png

 

Ji JO Flag usage

Using via direct command line

dashcli -ji input_json.txt -jo output_json.txt

Here, the file ‘input_json.txt’ has the DASH command in JSON format. DASH CLI executes this command and writes the output in JSON format to file specified by -jo option, which is output_json.txt. Usage is shown in the screenshot below.

 

Image3_JIJO.png

Using JI/JO programmatically

Create a sample Python application by following the steps below:

 

Step 1: Create a text file with the name “PythonSample_JIJO.py” in Notepad++. Add the import statements from subprocess python library.

 

from subprocess import Popen, PIPE

 

Step 2: Add the two constant members “InputFileName”, “OutputFileName” and “dashCliPath”.

 

InputFileName = 'input_json.txt'

OutputFileName = 'output_json.txt'

 

dashCliPath = r'C:\Program Files (x86)\DASH CLI 2.2\bin\dashcli.exe'

 

Step 3: Declare a variable named “cmd” which has the executable path and flag to be passed (-ji and -jo), along with the input and output files.

 

cmd = [dashCliPath, '-ji', InputFileName, '-jo', OutputFileName]

 

Step 4: Make a call to the command using “Popen” which executes the command stores output in the file “Output_json.txt”.

 

process = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)

 

Step 5: Surround steps 3 and 4 with commands to indicate program execution with print messages. The entire code at this stage should look like so:

 

from subprocess import Popen, PIPE

InputFileName = 'input_json.txt'

OutputFileName = 'output_json.txt'

dashCliPath = r'C:\Program Files (x86)\DASH CLI 2.2\bin\dashcli.exe'

 

print("Trying Ji Jo Example...")

cmd = [dashCliPath, '-ji', InputFileName, '-jo', OutputFileName]

process = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)

print("Ji Jo Example Done.")

 

Step 6: Put the following JSON test in input_json.txt file:

 

{"h":"10.138.135.145","u":"Administrator","P":"********","Commands":["enumerate","computersystem"]}

 

Step 7: run the PythonSample_JIJO.py on the console. Then use the type statements to view the input commands as well as the output of execution

 

Image4_Step7.png

 

In this blog, ‘DASH Computer System Enumerate’ is illustrated. Similarly, other DASH profiles can be accessed by the application by framing the required JSON request. See the ‘DASH CLI Developer Guide’ for JSON format for other supported DASH profiles.

 

Attachments:

  1. PythonSample_JDO script
  2. PythonSample_JIJO script

 

For any further query, drop a note below or contact via mail dashsupport@amd.com

 

Reference:

  • DASH CLI Developer Guide (Available in DASH CLI installation folder: “C:\Program Files (x86)\DASH CLI 2.2\docs\”)

 

Useful links:

Related Blogs:

About the Author
Providing solutions to DMTF DASH client manageability solutions