Skip to content

Instantly share code, notes, and snippets.

@scjody
Last active October 8, 2021 01:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scjody/29ad8c003a1bd5ff15e10b54926eb110 to your computer and use it in GitHub Desktop.
Save scjody/29ad8c003a1bd5ff15e10b54926eb110 to your computer and use it in GitHub Desktop.
"gcloud compute ssh" wrapper to figure out the zone automatically
#!/bin/bash
HOST="$1"
if [[ $HOST = *"@"* ]] ; then
USER=$(echo $HOST | cut -d'@' -f1)
HOST=$(echo $HOST | cut -d'@' -f2)
fi
gcloud config list | grep 'Your active'
ZONE=$(gcloud compute instances list |grep -E "^$HOST[[:space:]]" | awk '{print $2}')
echo zone: $ZONE
gcloud compute ssh --zone=$ZONE "$@"
@scjody
Copy link
Author

scjody commented Feb 5, 2018

Put this in your path and you'll be able to connect to an instance without figuring out its zone in advance:
gssh jody-imageserver-test

If you use multiple configurations (gcloud config) to deal with more than one project, you'll need to be sure you've selected the correct configuration. This wrapper makes that easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment