Skip to content

Instantly share code, notes, and snippets.

@dylanmtaylor
Created October 27, 2021 02:44
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 dylanmtaylor/4d9154373537e0d2e5262b57e7aa250e to your computer and use it in GitHub Desktop.
Save dylanmtaylor/4d9154373537e0d2e5262b57e7aa250e to your computer and use it in GitHub Desktop.
SSH RDP Connect
#!/usr/bin/env sh
## USAGE: ./ssm_rdp_connect.sh [instance name] OR ./ssm_rdp_connect.sh [instance ID]
## Then connect your RDP client - e.g. Remmina or Microsoft Remote Desktop to localhost:3389.
## You must be authenticated to the correct AWS profile before running.
echo "Attempting to find instance $1 ..."
QUERY_RESULT="$(aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[] | [0], InstanceId]' --output text | grep -i "$1" | head -n1)"
INSTANCE_NAME=$(echo "$QUERY_RESULT" | cut -f1)
INSTANCE_ID=$(echo "$QUERY_RESULT" | rev | cut -f1 | rev)
if [ -z "$INSTANCE_ID" ]; then
echo "The specified instance was not found. :-("
echo "Are you authenticated to the right account?"
exit 1
else
echo "Instance ID: $INSTANCE_ID"
if [ -n "$INSTANCE_NAME" ]; then # Not all instances are named; we don't error if we don't find a name.
echo "Instance Name: $INSTANCE_NAME"
fi
fi
echo "Attempting to start port forwarding on port 3389, connect the RDP client to localhost on this port."
aws ssm start-session --target "$INSTANCE_ID" --document-name AWS-StartPortForwardingSession --parameters "localPortNumber=3389,portNumber=3389"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment