Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kmcquade/a536053660a1933640b142f15c8a8db4 to your computer and use it in GitHub Desktop.
Save kmcquade/a536053660a1933640b142f15c8a8db4 to your computer and use it in GitHub Desktop.
to get decoded user data from ec2 instances
#/bin/bash
trap exit INT
INSTANCES=$( aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId[]' | sed -e 's/\[//g' -e 's/\]//g')
SUM=0
echo $INSTANCES
for i in $( echo $INSTANCES | sed -e 's/"//g' -e 's/,//g' -e 's/\[//g' -e 's/\]//g' ) ;do
echo "---------------------------$i-------------------------------\n"
aws ec2 describe-instances --instance-ids $i --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value' --output text
aws ec2 describe-instance-attribute --instance-id $( echo $i | sed -e 's/"//g' -e 's/,//' -e 's/\[//g' -e 's/\]//g' ) --attribute userData \
| jq '.UserData.Value' | sed 's/"//g' | base64 --decode
((SUM += 1))
echo "\n"
done
echo "Total Number of Servers: $SUM"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment