| skipped 22 lines |
23 | 23 | | if version_info >= (3,5): |
24 | 24 | | #import subprocess as sub |
25 | 25 | | from subprocess import run, PIPE |
26 | | - | compatmode = 0 # newer version of python, no need for compatibility mode |
| 26 | + | # loop through dictionary, execute the commands, store the results, return updated dict |
| 27 | + | def execCmd(cmdDict): |
| 28 | + | ''' for versions >= 3.5 ''' |
| 29 | + | for item in cmdDict: |
| 30 | + | cmd = cmdDict[item]["cmd"] |
| 31 | + | try: |
| 32 | + | process = run(cmd, stdout=PIPE, stderr=PIPE, shell=True) |
| 33 | + | results = process.stdout.decode().split('\n') |
| 34 | + | except Exception as e: |
| 35 | + | results = ['[-] failed: {}'.format(e)] |
| 36 | + | cmdDict[item]["results"]=results |
| 37 | + | return cmdDict |
| 38 | + | |
27 | 39 | | elif version_info >= (3,): |
28 | 40 | | #import os # older version of python, need to use ### instead |
29 | 41 | | from subprocess import check_output, PIPE |
30 | | - | compatmode = 1 |
| 42 | + | # loop through dictionary, execute the commands, store the results, return updated dict |
| 43 | + | def execCmd(cmdDict): |
| 44 | + | ''' for version < 3.5 ''' |
| 45 | + | for item in cmdDict: |
| 46 | + | cmd = cmdDict[item]["cmd"] |
| 47 | + | try: |
| 48 | + | echo_stdout = check_output(cmd, shell=True) |
| 49 | + | results = echo_stdout.decode().split('\n') |
| 50 | + | except Exception as e: |
| 51 | + | results = ['[-] failed: {}'.format(e)] |
| 52 | + | cmdDict[item]["results"]=results |
| 53 | + | return cmdDict |
31 | 54 | | else: |
32 | 55 | | print("Error: please run in python3 only.") |
| 56 | + | exit(1) |
33 | 57 | | |
34 | 58 | | # title / formatting |
35 | 59 | | bigline = "=" * 80 |
| skipped 7 lines |
43 | 67 | | print(bigline) |
44 | 68 | | print("") |
45 | 69 | | |
46 | | - | # loop through dictionary, execute the commands, store the results, return updated dict |
47 | | - | def execCmd(cmdDict): |
48 | | - | for item in cmdDict: |
49 | | - | cmd = cmdDict[item]["cmd"] |
50 | | - | try: |
51 | | - | if compatmode == 0: # newer version of python, use preferred subprocess |
52 | | - | process = run(cmd, stdout=PIPE, stderr=PIPE, shell=True) |
53 | | - | results = process.stdout.decode().split('\n') |
54 | | - | else: # older version of python, use os.popen |
55 | | - | echo_stdout = check_output(cmd, shell=True) |
56 | | - | results = stdout.decode().split('\n') |
57 | | - | except Exception as e: |
58 | | - | results = ['[-] failed: {}'.format(e)] |
59 | | - | cmdDict[item]["results"]=results |
60 | 70 | | |
61 | | - | return cmdDict |
62 | 71 | | |
63 | 72 | | # print results for each previously executed command, no return value |
64 | 73 | | def printResults(cmdDict): |
| skipped 196 lines |