1

I use the following command in cli as below,

  [mbelagali@mbelagali-vm naggappan]$ aws ec2 create-vpc --cidr-block 172.35.0.0/24 --no-verify-ssl --endpoint-url https://10.34.172.145:8787

/usr/local/aws/lib/python2.6/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:769: 
InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html

"Vpc": {
    "InstanceTenancy": "default",
    "State": "pending",
    "VpcId": "vpc-ebb1608e",
    "CidrBlock": "172.35.0.0/24",
    "DhcpOptionsId": "dopt-a24e51c0"
}

And now I redirect the warnings using "2>/dev/null" so that i get only the json response.

Now I need to implement this using the python subprocess and hence tried the following option,

cmd = "aws ec2 create-vpc --cidr-block " + cidr_block + " --no-verify-ssl --endpoint-url " + endpoint_url
cmd_arg = shlex.split(cmd.encode('utf-8'))
p1 = subprocess.Popen(
    cmd_arg,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT)
output, error = p1.communicate()

Now in output variable I get is complete output including the warning messages how can I ignore the warning message as I do it in the shell script

1
  • Tangentially, have you considered using a tool like Ansible or Salt to do your Amazon provisioning more declaratively? Commented May 15, 2015 at 19:51

3 Answers 3

2

If you don't want the stderr messages you should not have the flag stderr=subprocess.STDOUT which does the equivalent of 2>&1. If you just remove that I suspect you'll get what you want. If you want to redirect stderr to /dev/null you can follow this answer: How to hide output of subprocess in Python 2.7

Sign up to request clarification or add additional context in comments.

Comments

0

To separate stderr and stdout simply create two independent pipes.

p1 = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

To ignore stderr completely simply open devnull and redirect stderr there.

with open(os.devnull) as devnull:
    p1 = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=devnull)

os.devnull The file path of the null device. For example: '/dev/null' for POSIX, 'nul' for Windows. Also available via os.path.

1 Comment

you should open the file for writing.
0

To get json data that the subprocess prints to its stdout while ignoring warnings on its stderr:

from subprocess import check_output

json_data = check_output(cmd, stderr=DEVNULL)

where DEVNULL is defined here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.