Last active
December 7, 2019 01:42
-
-
Save jeremygaither/fd19752b5cefe1854bae1c4afe8ef6cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash -e | |
| ## Connects to a Windows AWS EC2 instance with clipboard and drive redirection | |
| ## automatically using the password encrypted with EC2 keys. Copy the instance | |
| ## ID to your clipboard, and execute the script. The password will be placed in | |
| ## your clipboard, and your Remote Desktop client will be used to connect. | |
| if [[ -z "$1" ]]; then | |
| INSTANCE=$(pbpaste) | |
| else | |
| INSTANCE=$1 | |
| fi | |
| echo "INSTANCE: $INSTANCE" | |
| METADATA=$(aws ec2 describe-instances --instance-ids "$INSTANCE") | |
| KEY=$(echo -n "$METADATA" | jq -r '. | .Reservations[0].Instances[0].KeyName') | |
| echo "KEY: $KEY" | |
| IPADDRESS=$(echo -n "$METADATA" | jq -r '. | .Reservations[0].Instances[0].PrivateIpAddress') | |
| echo "IPADDRESS: $IPADDRESS" | |
| INSTANCE_NAME=$(echo -n "$METADATA" | jq -r '.Reservations[].Instances[] | (.Tags[] | select(.Key == "Name") | .Value)') | |
| echo "INSTANCE_NAME: $INSTANCE_NAME" | |
| KEYFILE="$HOME/.ssh/AWS-${KEY}.pem" | |
| if [[ ! -f "$KEYFILE" ]]; then | |
| echo "unable to find SSH key at $KEYFILE" | |
| exit 1 | |
| fi | |
| PASSWORD=$(aws ec2 get-password-data --instance-id "$INSTANCE" \ | |
| --priv-launch-key "$KEYFILE" \ | |
| | jq -r '. | .PasswordData') | |
| echo -n "$PASSWORD" | pbcopy | |
| echo $'\e[92mPassword saved to your clipboard!\e[0m' | |
| LINK="rdp://Administrator@$IPADDRESS" | |
| echo "LINK: $LINK" | |
| RDP_CONTENT=$(cat <<EOF | |
| auto connect:i:1 | |
| full address:s:$IPADDRESS | |
| username:s:Administrator | |
| redirectclipboard:i:1 | |
| drivestoredirect:s:* | |
| connection type:i:7 | |
| networkautodetect:i:0 | |
| EOF | |
| ) | |
| TMP_NAME=$(basename "$0") | |
| TMP_FOLDER=$(mktemp -d "${TMP_NAME}.XXXXXX") | |
| if [ $? -ne 0 ]; then | |
| echo "Cannot write rdp file" | |
| exit 1 | |
| fi | |
| echo "$RDP_CONTENT" >"${TMP_FOLDER}/${IPADDRESS}.rdp" | |
| echo "Connecting to instance $INSTANCE_NAME..." | |
| open "${TMP_FOLDER}/${IPADDRESS}.rdp" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment