Connecting to an embedded device Using paramiko and pyyaml #2

The second and last part containing the two sections of connecting to embedded device will be seen here. I am going to get the support of yaml to store the commands.

Before going into how we are going to do it, lets see a bit about what yaml is.

yaml or YAML Ain’t Markup Language 🙂 is a human friendly data serialization standard. It has APIs in all programming languages and we are going to use the python version of yaml called the PyYaml.

In my words using pyyaml we are going to store the set of commands and their expected output in a human readable format. We will see how.

PyYaml has a specific format for storage of the information. We are going to make use of the nesting facility in yaml syntax to nest both a command and its subsequent result together. To learn more about pyyaml and its syntax please visit

The file is stored as command.yaml and contains information like below.

– command: command_to_be_exec
result: result_of_cmd
– command: second_cmd
result: res_of_sec_cmd

The above format actually corresponds to Python dictionary format where “command” is key and “command_to_be_exec” is the value. This recognition will be important as it will be useful when you look at the parsed output. so using pyyaml we are going to parse and get the commands in pythonic format for easy execution.

import yaml
conts = yaml.load(open("text.yaml"))

We parse the contents given above using the load method in pyyaml. The method loads and generates a list of dictionaries where the command to be executed will be value and the “command” word will be key. Exactly as given in the yaml file.

print conts
[{'command': 'command_to_be_exec', 'result': 'result_of_cmd'}, {'command': 'second_cmd', 'result': 'res_of_sec_cmd'}]

Now it will be easy to create a small python module to combine both.

import devconnect

def parse_yaml(yaml_file):
    Method to parse the yaml file provided and give the contents in pythonic format.
    :param yaml_file: yaml file to be parsed
    :return: contents of the file
    conts = yaml.load(open(yaml_file))
    return conts

def executeCommands(hostname, username, password):
    Method to execute the commands in yaml by integrating with the devconnect
    file and creating a object for DevConnect class.
    cmdfile = "commands.yaml"
    dcon = devconnect.DevConnect(hostname, username, password)
    data = parse_yaml(cmdfile)
    for test_no, cmd in enumerate(data):
        print "Executing test number: %d and command: %s" % (test_no+1, cmd["command"])
        rc, out, err = dcon.runCommand(cmd["command"])
        print "out"

Now the above code just prints the output executed in the device. But you guys can use these modules in more efficient way and create an entire test suite for the device.

That’s all folks. Please dont forget to like my posts if its good and informative. And dont forget to look into the documentation of paramiko and pyyaml. I have provided them in the references.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s