Created
February 25, 2025 19:38
-
-
Save jjtroberts/1f68958e0a4c982c445ff6a84e8ff9dc to your computer and use it in GitHub Desktop.
Dynamic GCP IAP SSH Tunnel
This file contains hidden or 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 | |
| ## Example: ./get_gcp_compute_instance.sh hostname gcp_project_name | |
| HOSTNAME=$1 | |
| PROJECT_ID=$2 | |
| INSTANCE_INFO=$(gcloud compute instances list \ | |
| --filter="name:$HOSTNAME" \ | |
| --format="value(name,zone)" \ | |
| --project="$PROJECT_ID") | |
| if [[ -n "$INSTANCE_INFO" ]]; then | |
| IFS=$'\t' read -r GCE_INSTANCE_NAME GCE_INSTANCE_ZONE <<< "$INSTANCE_INFO" | |
| echo "Instance Name: $GCE_INSTANCE_NAME" | |
| echo "Instance Zone: $GCE_INSTANCE_ZONE" | |
| else | |
| echo "Instance not found." | |
| fi | |
| ~/.config/gcloud/virtenv/bin/python3 ~/projects/google-cloud-sdk/lib/gcloud.py \ | |
| compute start-iap-tunnel $GCE_INSTANCE_NAME \ | |
| 22 \ | |
| --listen-on-stdin \ | |
| --project $PROJECT_ID \ | |
| --zone=$GCE_INSTANCE_ZONE \ | |
| --verbosity=warning |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment