Home > Back-end >  Python : Function issue with subprocess.run
Python : Function issue with subprocess.run

Time:12-14

Good morning

I use python 3.10.4 I created the python function below

def generate():
    subprocess.run(
            [
            "titi",
            "toto",
            f"generate {element['fullElmName']}",
            "--env ENV",
            "--sys SYS", 
            "--sub SUB",
            "--sn 1",
            "--type TYP", 
            "-i XXXX"
            ],
            shell=True
        )

When I execute it, I have the issue below because titi is not on the command

Command Error: Arguments inconnus : env ENV, sys SYS, sub SUB, sn 1, type TYP, i, generate element Command failed due to improper syntax Did you mean: xxxx generate ele?

Command entered: "toto generate element --env ENV --sys SYY --sub SUB --sn 1 --type TYP -i XXXX"

I'm waiting something like that:

titi toto generate element --env ENV --sys SYY --sub SUB --sn 1 --type TYP -i XXXX

I tried to merge the titi and toto like below

def generate():
    subprocess.run(
            [
            "titi toto",
            f"generate {element['fullElmName']}",
            "--env ENV",
            "--sys SYS", 
            "--sub SUB",
            "--sn 1",
            "--type TYP", 
            "-i XXXX"
            ],
            shell=True
        )

but the function starts with titi toto but don't continue after Do you have any idea how I can solve my problem? Thanks by advance for your help Best Regards

CodePudding user response:

Ask Mark said, you need to split the arguments. Your shell automatically splits on spaces (unless quoted or escaped) when you type it in the console, and you should do the same when passing parameters through Python.

The correct code is:

def generate():
    subprocess.run(
        [
            "titi",
            "toto",
            "generate", element["fullElmName"],
            "--env", "ENV",
            "--sys", "SYS", 
            "--sub", "SUB",
            "--sn", "1",
            "--type", "TYP", 
            "-i", "XXXX"
        ]
    )

Note that I've removed the shell=True because this makes your application vulnerable to shell injection, and changes the handling of parameters. Your arguments were actually being passed to the shell (/bin/sh) instead of titi.

CodePudding user response:

I tried to launch a zowe command with variable include in it to generate a Cobol program on the mainframe side in Endevor exactly. So, I modified the code as below and I had the result I need ;)

def generate():
    subprocess.run(
            [
            'zowe',
            'endevor',
            'generate',
            'element',f"{element['fullElmName']}",
            '--env',f"{element['envName']}",
            '--sys',f"{element['sysName']}",
            '--sub',f"{element['sbsName']}",
            '--ccid',f"{element['lastActCcid']}",
            '--sn',f"{element['stgNum']}",
            '--type',f"{element['typeName']}",
            '-i ENDEVOR'
            ],
            shell=True
        )

Thanks a lot Mark and Hack5 for your help I really appreciate your support

  • Related