-
-
Save iamzhout/0a7c6956f896118a7379884dd850caca to your computer and use it in GitHub Desktop.
Shell script to start/stop an EC2 instance to use as a ssh tunnel
This file contains 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 | |
# Start/stop an EC2 instance to use as a ssh tunnel | |
# requires: 1. the aws package locally -- sudo apt-get install awscli | |
# 2. run `aws configure` to config aws environment beforehand | |
# | |
# usage: ./tunnel.sh start (spin up EC2 and create the tunnel) | |
# ./tunnel.sh stop (stop the EC2 instance to save money) | |
# ./tunnel.sh status (show EC2 instance detail) | |
# ./tunnel.sh resume (in case your tunnel is interrupted but the EC2 instance is still running) | |
# ./tunnel.sh terminate (terminate the EC2 instance to save money) | |
# CHANGE THE PARAMETERS BELOW | |
imageid="ami-ab77d4c5" # this is an Ubuntu AMI (Ubuntu Server 16.04 LTS (HVM), SSD Volume Type), but you can change it to whatever you want | |
instance_type="t2.nano" | |
key_name="myawskeypairname" # your keypair name -- http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html | |
security_group="my-security-group" # your security group -- http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html | |
wait_seconds="30" # seconds between polls for the public IP to populate (keeps it from hammering their API) | |
port="5222" # the SSH tunnel port you want | |
key_location="/home/aws/keypair.pem" # your private key -- http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html#having-ec2-create-your-key-pair | |
user="ubuntu" # the EC2 linux user name | |
# END SETTINGS | |
# --------------------- you shouldn't have to change much below this --------------------- | |
# private | |
connect () | |
{ | |
ssh -oStrictHostKeyChecking=no -ND $port -i $key_location $user@$ip | |
} | |
# private | |
getip () | |
{ | |
ip=$(aws ec2 describe-instances --filters "Name=image-id,Values=${imageid}" "Name=instance-state-code,Values=16" | grep PublicIpAddress | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}") | |
} | |
# private | |
getinstance () | |
{ | |
instance=$(aws ec2 describe-instances --filters "Name=image-id,Values=${imageid}" "Name=instance-state-name,Values=running,stopped" | grep InstanceId | grep -E -o "i\-[0-9A-Za-z]+") | |
if [ "x$instance" == "x" ]; then | |
instance="i-nonexist" | |
fi | |
} | |
# public | |
status () | |
{ | |
getinstance | |
aws ec2 describe-instances --instance-ids $instance | |
} | |
# public | |
start () | |
{ | |
echo "Starting instance..." | |
aws ec2 run-instances --image-id $imageid --count 1 --instance-type $instance_type --key-name $key_name --security-groups $security_group > /dev/null 2>&1 | |
# wait for a public ip | |
while true; do | |
echo "Waiting $wait_seconds seconds for IP..." | |
sleep $wait_seconds | |
getip | |
if [ ! -z "$ip" ]; then | |
break | |
else | |
echo "Not found yet. Waiting for $wait_seconds more seconds." | |
sleep $wait_seconds | |
fi | |
done | |
echo "Found IP $ip - Starting tunnel on port $port" | |
connect | |
} | |
# public | |
stop () | |
{ | |
getinstance | |
aws ec2 stop-instances --instance-ids $instance | |
} | |
# public | |
terminate () | |
{ | |
getinstance | |
aws ec2 terminate-instances --instance-ids $instance | |
} | |
# public | |
resume () | |
{ | |
getip | |
connect | |
} | |
# public | |
instruct () | |
{ | |
echo "Please provide an argument: start, stop, resume" | |
} | |
#------------------------------------------------------- | |
# "main" | |
case "$1" in | |
status) | |
status | |
;; | |
start) | |
start | |
;; | |
resume) | |
resume | |
;; | |
stop) | |
stop | |
;; | |
terminate) | |
terminate | |
;; | |
help|*) | |
instruct | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment