Execute Command (execute_cmd) loop through dictionary, execute the commands, store the results, return updated dict :param cmddict: Dictionary of commands to execute and results :return: The command Dictionary with the commands results included
(cmddict)
| 36 | |
| 37 | |
| 38 | def execute_cmd(cmddict): |
| 39 | """ |
| 40 | Execute Command (execute_cmd) |
| 41 | loop through dictionary, execute the commands, store the results, return updated dict |
| 42 | |
| 43 | :param cmddict: Dictionary of commands to execute and results |
| 44 | :return: The command Dictionary with the commands results included |
| 45 | """ |
| 46 | |
| 47 | for item in cmddict: |
| 48 | cmd = cmddict[item]["cmd"] |
| 49 | if compatmode == 0: # newer version of python, use preferred subprocess |
| 50 | out, error = sub.Popen([cmd], stdout=sub.PIPE, stderr=sub.PIPE, shell=True).communicate() |
| 51 | results = out.split('\n') |
| 52 | else: # older version of python, use os.popen |
| 53 | echo_stdout = os.popen(cmd, 'r') |
| 54 | results = echo_stdout.read().split('\n') |
| 55 | |
| 56 | # write the results to the command Dictionary for each command run |
| 57 | cmddict[item]["results"] = results |
| 58 | |
| 59 | return cmddict |
| 60 | |
| 61 | |
| 62 | def print_results(cmddict): |
no outgoing calls
no test coverage detected