'How to run two curl command sequentially in python?

Hi I'm a budding programmer and been trying to run the below curl command in a python environment but keep getting errors - unsure of how the curl command is run sequentially. How can i fix this please ! ''' TOKEN=curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"
&& curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/dynamic/instance-identity/document ''' I need to run the curl request inside the instance to get metadata JSON output.

  • firstly I will need to run the PUT request curl command to get the token and the value will be stored in the "TOKEN" variable.
  • secondly i will run the get metadata curl command which uses the token created.

My code looks like this '''

from .shell import run_command_str
import json

@staticmethod
def get_instance_metadata():

CMD = "TOKEN=curl -s -X PUT http://169.254.169.254/latest/api/token -H X-aws-ec2-metadata-token-ttl-seconds:21600" && "curl -s -H X-aws-ec2-metadata-token:$TOKEN http://169.254.169.254/latest/dynamic/instance-identity/document"
output = run_command_str(command=CMD, shell_command=True)
print("session token: ", output)
if not output:
  AwsClient.logger.info("ERROR: {}".format(error))
  raise Exception("Unable to get the AWS instance metadata : {}".format(error))
AwsClient.logger.info("Instance metadata : {}".format(output))
try:
   output = json.loads(output)
except Exception as error:
   AwsClient.logger.info("ERROR: {}".format(error))
   raise Exception("Error while decoding instance metadata: {}".format(error))
return output.

The output i get is :

session token: ('', '/bin/sh: 1: -s: not found\n') Traceback (most recent call last): output = json.loads(output) File "/usr/lib/python3.8/json/init.py", line 341, in loads raise TypeError(f'the JSON object must be str, bytes or bytearray, ' TypeError: the JSON object must be str, bytes or bytearray, not tuple



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source