Skip to content

Instantly share code, notes, and snippets.

Last active Oct 8, 2021
What would you like to do?
"gcloud compute ssh" wrapper to figure out the zone automatically
if [[ $HOST = *"@"* ]] ; then
USER=$(echo $HOST | cut -d'@' -f1)
HOST=$(echo $HOST | cut -d'@' -f2)
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 "$@"
Copy link

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