Search code examples
pythoncmdsubprocesspopen

Passing Windows cmd to the Python subprocess


I'm trying to execute aws cli command using Pyton's subprocess

windows cmd:

aws --profile some_profile --region some_region ec2 describe-instances --filters Name=tag:some_tag,Values=some_value --query "Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='Name']|[0].Value}" --output=table

and that's how I try to do it:

profile = "some_profile"
region = "some_region"
ec2_filters = "Name=tag:some_tag,Values=some_value"
ec2_query = "Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='Name']|[0].Value}"
ec2_output_type = "table"

proc = subprocess.Popen(["aws", "--profile", profile, "--region", region, "ec2", "describe-instances", "--filters", ec2_filters, "--query", ec2_query, "--output", ec2_output_type], stdout=subprocess.PIPE, shell=True)

This is the error message:

'[0].Value}' is not recognized as an internal or external command, operable program or batch file.


Solution

  • I don't have aws installed, so I created a mock batch file to spit back what it received. I did try my initial guesses and you're right, it often makes it difficult, but I figured it out. Sorry for not testing what I asked you to try.

    Anyway, aws.bat contains a single line, echo %*, which prints back whatever the batch file receives as arguments, so we know it's working.

    Then, I tried to use your command. I got the same error you got, so I modified it to:

    .\aws.bat --profile some_profile --region some_region ec2 describe-instances --filters Name=tag:some_tag,Values=some_value --query '"Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='Name']|[0].Value}"' --output=table

    This outputted the command back, meaning it got executed correctly.

    Then, I modified your code to make sure there's quotes over all the query. I used simple string concatenation to do that.

    import subprocess
    profile = "some_profile"
    region = "some_region"
    ec2_filters = "Name=tag:some_tag,Values=some_value"
    ec2_query = (
        '"Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='
        "'Name'" 
        ']|[0].Value}"'
    )
    ec2_output_type = "table"
    
    proc = subprocess.Popen(["aws.bat", "--profile", profile, "--region", region, "ec2", "describe-instances", "--filters", ec2_filters, "--query", ec2_query, "--output", ec2_output_type])
    

    This worked. Funnily, if I used triple quotes in an unorthodox manner, it worked as well.

    ec2_query = ' '''"Reservations[*].Instances[*].{AvailabilityZone:Placement.AvailabilityZone,Status:State.Name,Name:Tags[?Key=='Name']|[0].Value}"' '''
    

    Note the start, ' '''". I don't really know what's going on.

    Anyway, the easier solution is to break up your string so the quotes don't get confusing.