Skip to content

Instantly share code, notes, and snippets.

@tomcatzh
Created September 5, 2016 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomcatzh/d0f1ce1e602d35dbc736bfbec159a9e4 to your computer and use it in GitHub Desktop.
Save tomcatzh/d0f1ce1e602d35dbc736bfbec159a9e4 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ -z $1 ]; then
FILTER='Name=instance-state-code,Values=16'
else
FILTER=$1
fi
IDs=`aws ec2 describe-instances --filters "$FILTER" | jq '.Reservations[].Instances[].InstanceId' -r`
for id in $IDs
do
name=`aws ec2 describe-instances --filters "Name=instance-id,Values=$id" | jq '.Reservations[].Instances[].Tags[]?|select(.Key=="Name")|.Value' -r`
ip=`aws ec2 describe-instances --filters "Name=instance-id,Values=$id" | jq '.Reservations[].Instances[].PrivateIpAddress' -r`
if [ -z $name ]; then
echo "Host $ip"
else
echo "Host $name"
fi
echo " Hostname $ip"
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment