Skip to content

Instantly share code, notes, and snippets.

@knksmith57
Created February 8, 2017 06:38
Show Gist options
  • Save knksmith57/4bb8036eb8c65ff15c36f885341cdeea to your computer and use it in GitHub Desktop.
Save knksmith57/4bb8036eb8c65ff15c36f885341cdeea to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash -e
instance_name="throw-away-$(date +%s)"
region=us-west1-b
ssh_key_dir="$(mktemp -d)"
cleanup() {
echo "removing temp dir: $ssh_key_dir"
rm -rf "$ssh_key_dir"
echo "killing instance"
gcloud compute instances delete --zone us-west1-b --quiet $instance_name
}
trap cleanup EXIT
echo "generating ssh key pair in: $ssh_key_dir"
ssh-keygen -t rsa -b 4096 -C '' -N '' -f "$ssh_key_dir/id_rsa" &>/dev/null
pub_key="$(< "$ssh_key_dir/id_rsa.pub")"
echo "creating instance: $instance_name"
gcloud compute instances create $instance_name \
--zone $region \
--machine-type f1-micro \
--metadata "ssh-keys=foobar:$pub_key" \
--maintenance-policy TERMINATE \
--image debian-8-jessie-v20170124 \
--image-project debian-cloud \
--boot-disk-size 10
echo "discovering instance IP address"
ip_address="$(\
gcloud compute instances describe $instance_name \
--zone $region \
--format json \
| jq -r .networkInterfaces[].accessConfigs[].natIP \
)"
echo "creating SOCKS5 tunnel from localhost:5555 => $ip_address"
ssh -D 5555 -C -q -N \
-i "$ssh_key_dir/id_rsa" \
-o StrictHostKeyChecking=no \
foobar@$ip_address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment