Skip to content

Instantly share code, notes, and snippets.

@mvanholsteijn
Last active October 26, 2022 05:49
Show Gist options
  • Save mvanholsteijn/fee96ea9baf23449ca9641f08f79814d to your computer and use it in GitHub Desktop.
Save mvanholsteijn/fee96ea9baf23449ca9641f08f79814d to your computer and use it in GitHub Desktop.
simple script to rdp into an EC2 Windows server
#!/bin/bash
#
# Copyright 2022 - Binx.io B.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e -o pipefail
[[ $# -ne 1 ]] && (echo "usage: $(basename $0) ec2-instance-name" >&1 && exit 1)
instance_name=$1
instance_id=$(aws ec2 describe-instances \
--query 'join(`\n`, Reservations[].Instances[].InstanceId)' \
--output text \
--filter "Name=tag:Name,Values=$instance_name" "Name=instance-state-name,Values=running")
key_name=$(aws ec2 describe-instances \
--instance-id $instance_id \
--query Reservations[0].Instances[0].KeyName \
--output text)
key_id=$(aws ec2 describe-key-pairs \
--key-names $key_name \
--query KeyPairs[0].KeyPairId \
--output text)
private_key=$(mktemp)
chmod 0600 $private_key
aws ssm get-parameter --name /ec2/keypair/$key_id \
--with-decryption --query Parameter.Value \
--output text > $private_key
password=$(aws ec2 get-password-data \
--priv-launch-key $private_key --instance-id $instance_id \
--query PasswordData \
--output text)
rm -f $private_key
ip_address=$(aws ec2 describe-instances \
--instance-ids $instance_id \
--query 'join(`\n`, Reservations[].Instances[].PublicIpAddress)' \
--output text)
xfreerdp /u:administrator /p:$password /v:$ip_address /cert:ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment