Extracting system info in one go from ec2-instances

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 ec2-user@2.2.2.2 & for some its centos@3.3.3.3.

ssh -i "prod-key-pair.pem" ec2-user@ec2-1-9-1-2.compute-1.amazonaws.com ssh -i "sandbox-key-pair.pem" centos@1.1.4.2 '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?

Prashast

Posted 2018-05-04T19:10:42.563

Reputation: 211

Please note that https://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 – 2018-05-04T20:12:02.633

@DavidPostill : I have updated the question accordingly – Prashast – 2018-05-04T20:51:04.710

Answers

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

supreet

Posted 2018-05-04T19:10:42.563

Reputation: 11