1+ #! /bin/bash
2+
3+ SELF=$( basename " $0 " )
4+ OPTION=" "
5+
6+ if [[ -z " $AWS_PROFILE " ]]; then
7+ echo " set your AWS_PROFILE to SSO_LOGIN"
8+ export AWS_PROFILE=SSO_LOGIN
9+ echo " please sso login!"
10+ echo " aws configure sso --profile SSO_LOGIN"
11+ exit 1
12+ fi
13+
14+ function help() {
15+ echo " " "
16+ choose one of the following options: sshtunnel | sessionmanager
17+
18+ usage: ${SELF}
19+ --sshtunnel - uses ssh tunnel only (needs a public accessible ssh port opened)
20+ --sessionmanager - uses ssh tunnel via sessionmanager websocket (does not need any public accessible port)
21+ " " "
22+ }
23+
24+ function main() {
25+
26+ if [[ " ${OPTION} " == " " ]]; then
27+ help
28+ exit 1
29+ fi
30+
31+ ssh-add -D
32+ SSH_KEY=$( mktemp key.XXXXXX)
33+ ssh-keygen -t rsa -f " $SSH_KEY " -N " " -q <<< y > /dev/null 2>&1
34+ trap " rm $SSH_KEY $SSH_KEY .pub" EXIT
35+
36+ aws ec2-instance-connect send-ssh-public-key \
37+ --profile SSO_LOGIN \
38+ --region eu-central-1 \
39+ --instance-id i-0184585dfbeada713 \
40+ --availability-zone eu-central-1a \
41+ --instance-os-user ec2-user \
42+ --ssh-public-key " file://$SSH_KEY .pub"
43+
44+ if [[ " ${OPTION} " == " sshtunnel" ]]; then
45+ ssh -i " $SSH_KEY " ec2-user@3.71.44.62 -L 3307:rds-dms.cluster-cwdqrgvghbzj.eu-central-1.rds.amazonaws.com:3306
46+ elif [[ " ${OPTION} " == " sessionmanager" ]]; then
47+ CONFIG=~ /.ssh/config
48+ if [[ " $( grep i-0184585dfbeada713 $CONFIG ) " == " " ]]; then
49+ cat << EOF >> ~/.ssh/config
50+ Host i-0184585dfbeada713
51+ ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p' --profile SSO_LOGIN --region eu-central-1"
52+ EOF
53+ fi
54+ ssh -i " $SSH_KEY " ec2-user@i-0184585dfbeada713 -L 3307:rds-dms.cluster-cwdqrgvghbzj.eu-central-1.rds.amazonaws.com:3306
55+ fi
56+
57+ }
58+
59+ while true ;
60+ do
61+ case " $1 " in
62+ -t|--sshtunnel)
63+ OPTION=" sshtunnel"
64+ shift 2
65+ break
66+ ;;
67+ -s|--sessionmanager)
68+ OPTION=" sessionmanager"
69+ shift 2
70+ break
71+ ;;
72+ --)
73+ shift ;
74+ break
75+ ;;
76+ * )
77+ help
78+ exit 1
79+ ;;
80+ esac
81+ done
82+
83+ main
0 commit comments