Skip to content

Instantly share code, notes, and snippets.

@gswallow
Created October 11, 2023 19:56
Show Gist options
  • Save gswallow/20475ac1094aa602ac15e9eb9226e7eb to your computer and use it in GitHub Desktop.
Save gswallow/20475ac1094aa602ac15e9eb9226e7eb to your computer and use it in GitHub Desktop.
quick and dirty inventory of EC2 instances in response to CVE-2023-38545
#!/bin/bash
myaccount=$(eval echo $(aws sts get-caller-identity --query 'Account'))
aws ec2 describe-instances --region us-east-1 --query 'Reservations[].Instances[].{"KeyName": KeyName, "ImageId": ImageId, "PrivateIpAddress": PrivateIpAddress, "Name": Tags[?Key == `Name`]|[0].Value, "Region": `us-east-1` }' --output text | tr -s '\t' '|' >> .$myaccount.csv.$$
aws ec2 describe-instances --region us-east-2 --query 'Reservations[].Instances[].{"KeyName": KeyName, "ImageId": ImageId, "930604620050PrivateIpAddress": PrivateIpAddress, "Name": Tags[?Key == `Name`]|[0].Value, "Region": `us-east-2` }' --output text | tr -s '\t' '|' >> .$myaccount.csv.$$
aws ec2 describe-instances --region us-west-1 --query 'Reservations[].Instances[].{"KeyName": KeyName, "ImageId": ImageId, "PrivateIpAddress": PrivateIpAddress, "Name": Tags[?Key == `Name`]|[0].Value, "Region": `us-west-1` }' --output text | tr -s '\t' '|' >> .$myaccount.csv.$$
aws ec2 describe-instances --region us-west-2 --query 'Reservations[].Instances[].{"KeyName": KeyName, "ImageId": ImageId, "PrivateIpAddress": PrivateIpAddress, "Name": Tags[?Key == `Name`]|[0].Value, "Region": `us-west-2` }' --output text | tr -s '\t' '|' >> .$myaccount.csv.$$
echo "Source AMI|SSH Key|Name Tag|Private IP|Region|Platform|Description|Location" > $myaccount.csv
while read l; do
ami_id=$(echo $l | cut -d '|' -f 1)
if [[ $ami_id =~ "ami-" ]]; then
region=$(echo $l | cut -d '|' -f 5)
img_loc=$(eval echo '$(aws ec2 describe-images --image-ids $ami_id --region $region --query 'Images[].ImageLocation' --output text)')
img_desc=$(eval echo '$(aws ec2 describe-images --image-ids $ami_id --region $region --query 'Images[].Description' --output text)')
img_platform=$(eval echo '$(aws ec2 describe-images --image-ids $ami_id --region $region --query 'Images[].PlatformDetails' --output text)')
echo "$l|$img_platform|$img_desc|$img_loc" | tee -a $myaccount.csv
else
echo "$ami_id does not match 'ami-'"
fi
done < .$myaccount.csv.$$
rm .$myaccount.csv.$$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment