Python Linux Command executor

mu

Muthu

Posted on March 19, 2019

Python Linux Command executor
# IMPORTING LIBRARIES 
import subprocess
import shlex

def cmdExecutor(cliCmd):
    # COLLECT THE COMMAND AND SPLIT USING SHLEX
    cmd = shlex.split(cliCmd)
    # EXECUTE THE SUBPROCESS WITH COMMUNICATE()
    process,error = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr= subprocess.PIPE).communicate()
    print("Command executed successfully")
    # DECODE THE RESPONSE FROM THE SUBPROCESS TO UTF-8
    error = error.decode("utf-8")
    if(str(error)==None or str(error)==""):
        output="SUCCESS"
    else:
        output = "ERROR => "+str(error)
    return output
💖 💪 🙅 🚩
mu
Muthu

Posted on March 19, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Python Linux Command executor
utility Python Linux Command executor

March 19, 2019