Skip to content

Instantly share code, notes, and snippets.

@omar-yassin
Last active February 7, 2024 17:36
Show Gist options
  • Save omar-yassin/c698d73d28aeba261be8 to your computer and use it in GitHub Desktop.
Save omar-yassin/c698d73d28aeba261be8 to your computer and use it in GitHub Desktop.
AWS EC2 GET WINDOWS PASSWORD + AUTO RDP
#!/bin/bash
#ASSUMES KeyPairs are stored in ~/.ssh
# Also make sure to install jq and have in your path to run below
# JQ: https://stedolan.github.io/jq/
AWS_BIN="/usr/local/bin/aws"
AWS_PROFILE="omar-dev"
KP_DIR="~/.ssh"
servers=`$AWS_BIN --profile $AWS_PROFILE --output json ec2 describe-instances | jq -r '.Reservations[].Instances[] | { computer_name: .Tags[] | select(.Key == "computername") | .Value, id: .InstanceId, ip: .PrivateIpAddress, kp: .KeyName, os: .Platform } | to_entries | map([.key, .value]|join(":"))|join(" ")' | grep "os:windows" | sort | awk '{print NR, "\t", $0}' | column -t `
echo "$servers"
echo ""
echo "** NOTE: This will only show windows instances with the TAG 'computername'**"
read -p "Enter Line # you wish to retrieve the password for instance: " choice
name="`echo "$servers" | grep "^$choice " | awk '{print $2}' | awk -F":" '{print $2}'`"
kp="$KP_DIR/`echo "$servers" | grep "^$choice " | awk '{print $5}' | awk -F":" '{print $2}'`"
ip="`echo "$servers" | grep "^$choice " | awk '{print $4}' | awk -F":" '{print $2}'`"
instance="`echo "$servers" | grep "^$choice " | awk '{print $3}' | awk -F":" '{print $2}'`"
echo "Fetching password for (2nd column): $name $kp $ip $instance"
#aws ec2 get-password-data --instance-id i-5203422c --priv-launch-key C:\Keys\MyKeyPair.pem
$AWS_BIN --profile $AWS_PROFILE --output text ec2 get-password-data --instance-id ${instance} --priv-launch-key ${kp}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment