Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 314dee6

Browse files
committed
First version of aws ec2 instance connect proxy
0 parents  commit 314dee6

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ProxyCommand helper for AWS EC2 Instance connect
2+
3+
Usage
4+
5+
```
6+
.ssh/aws-proxy.sh [--profile profile] [--region region] [--key key] [--filter filterkey] user host [port]
7+
```
8+
9+
Here `filterkey` is the name of the filter to the [DescribeInstances](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstances.html)
10+
API call - it defaults to private-ip-address
11+
12+
e.g. if all your instances in the 10.1.0.0 subnet are in ap-southeast-2, you can use the following `~/.ssh/config`
13+
14+
```
15+
Host 10.1.0.*
16+
User ec2-user
17+
ProxyCommand sh ~/.ssh/aws-proxy.sh --profile test-account --region ap-southeast-2 --key ~/.ssh/test-aws %r %h %p
18+
19+
Host ip-10-1-*
20+
User ec2-user
21+
ProxyCommand sh ~/.ssh/aws-proxy.sh --profile test-account --region ap-southeast-2 --filter private-dns-name --key ~/.ssh/test-aws %r %h %p
22+
```

aws-proxy.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
if [ $# -lt 2 ] ; then
6+
echo "Usage: $0 [--profile profile] [--region region] [--key key] [--filter filterkey] user host [port]"
7+
exit
8+
fi
9+
10+
while true; do
11+
case $1 in
12+
--profile)
13+
PROFILE="--profile $2"
14+
shift 2
15+
;;
16+
--region)
17+
REGION="--region $2"
18+
shift 2
19+
;;
20+
--key)
21+
KEY=$2
22+
shift 2
23+
;;
24+
--filter)
25+
FILTER=$2
26+
shift 2
27+
;;
28+
*)
29+
break
30+
;;
31+
esac
32+
done
33+
34+
FILTER="${FILTER:-private-ip-address}"
35+
ARGS="--ssh-public-key file://$KEY.pub --instance-os-user $1"
36+
PORT="${3:-22}"
37+
LOGIN=$1
38+
DESTHOST=$2
39+
read -r instance_id availability_zone <<< $(aws ec2 describe-instances $PROFILE $REGION --filter "Name=$FILTER,Values=$DESTHOST" --query 'Reservations[*].Instances[*].[InstanceId,Placement.AvailabilityZone]' --output text)
40+
41+
aws ec2-instance-connect send-ssh-public-key $REGION $PROFILE --instance-id $instance_id --availability-zone $availability_zone $ARGS
42+
43+
nc $DESTHOST $PORT

0 commit comments

Comments
 (0)