Search code examples
pythonlistcmdsubprocessextract

Run command in CMD via python and extract the data


I am trying to use the below code to run a command and extract the data from the cmd.

the file with the commands and data is a txt file. (let me know if I should change it or use an excel if better).

the commands look something like this: ping "host name" which would result in some data in the cmd.there is list of these in the file. so it would ping "hostname1" then line two ping "hostname2"..etc

THE QUESTION: I want it to run every line individually and extract the results from the cmd and store them in a txt file or excel file - Ideally I want all the results in the same file. is this possible? and how?

here is the code so far:

root_dir = pathlib.Path(r"path to file here")
cmds_file = root_dir.joinpath('actual file here with commands and data') 
#fail = []
cmds = cmds_file.read_text().splitlines()

try:

for cmd in cmds:
    args = cmd.split() 
    print(f"\nRunning: {args[0]}")
    output = subprocess.check_output(args)


    print(output.decode("utf-8"))
    out_file = root_dir.joinpath(f"Name of file where I want results printed in")
    out_file.write_text(output.decode("utf-8"))
except:
pass

Solution

  • My plan is simple:

    1. Open input, output file
    2. Read input file line by line
    3. Execute the command and direct the output to the output file
    #!/usr/bin/env python3
    import pathlib
    import shlex
    import subprocess
    
    cmds_file = pathlib.Path(__file__).with_name("cmds.txt")
    output_file = pathlib.Path(__file__).with_name("out.txt")
    with open(cmds_file, encoding="utf-8") as commands, open(output_file, "w", encoding="utf-8") as output:
        for command in commands:
            command = shlex.split(command)
            output.write(f"\n# {shlex.join(command)}\n")
            output.flush()
            subprocess.run(command, stdout=output, stderr=subprocess.STDOUT, encoding="utf-8")
    

    Notes

    • Use shlex.split() to simulate the bash shell's command split
    • The line output.write(...) is optional. You can remove it
    • With subprocess.run(...), the stdout=output will redirect the command's output to the file. You don't have to do anything.

    Update

    I updated the subprocess.run line to redirect stderr to stdout, so error will show.