Skip to content

Instantly share code, notes, and snippets.

@scjody
Last active Oct 8, 2021
Embed
What would you like to do?
"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