0

I have almost 150 ec2-instances in an account. I want to extract some system info from each instance. I am aware that I can individually login into each system and get the information using the particular command. But I want all the information in one go. I can write a simple script, but the problem is while I have a bastion host and then I need to ssh into each instance & the username is different. Example for some instance its [email protected] & for some its [email protected].

ssh -i "prod-key-pair.pem" [email protected] ssh -i "sandbox-key-pair.pem" [email protected] 'bash -s' << EOF
    cat /etc/*-release|grep -m 1 NAME >>os_detail.txt
    ifconfig | grep inet |head -1 |awk -F ' ' '{print $2}' >>os_detail.txt
EOF

My question is what is the way where it can pick both centos & ec2-user one by one & if it fails using centos then it should try ec2-user. Secondly, the two commands when executed are creating the files in the server but I want those files in my local system from where I am running the script.

Any pointers on how I can solve this issue?

2
  • Please note that superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.
    – DavidPostill
    May 4, 2018 at 20:12
  • @DavidPostill : I have updated the question accordingly
    – Prashast
    May 4, 2018 at 20:51

1 Answer 1

0

Around the 2017 re:invent event, AWS launched launched Amazon EC2 Systems Manager. This service can be used to run scripts(SSM rundocument)/commands(SSM runcommand) remotely through the API's provided by Amazon.

By default the latest Windows instances and Amazon Linux instances have the SSM Agent installed in them by default. The others need manual installation which is easy and one time.

They have documents where we can create or use existing documents and run them on the machines we have listed. We get the necessary output generated from these commands through the API response.

To enable this service we have to ensure that the IAM Role assigned to the EC2 instance has the SSM Permission enabled.

Here are some links for further help:

  1. https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html
  2. https://docs.aws.amazon.com/systems-manager/latest/userguide/ssm-agent.html

To get the instance metadata you can use this command from the instance.

wget -q -O - http://169.254.169.254/latest/meta-data/instance-id

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .