Skip to content

Instantly share code, notes, and snippets.

@sloppycoder
Created August 30, 2025 09:54
Show Gist options
  • Select an option

  • Save sloppycoder/d7d666677f57d9e48b4ad1ffde47f13f to your computer and use it in GitHub Desktop.

Select an option

Save sloppycoder/d7d666677f57d9e48b4ad1ffde47f13f to your computer and use it in GitHub Desktop.
ssh tmux command into GCP VM
#!/bin/bash
VM_NAME=$1
if [ "$VM_NAME" = "" ]; then
echo please specify vm name
exit 0
fi
USER=$2
if [ "$USER" = "" ]; then
USER=aidev
fi
# Get zone and IP in one shot
read ZONE IP <<< $(gcloud compute instances list \
--filter="name=($VM_NAME)" \
--format="value(zone, networkInterfaces[0].accessConfigs[0].natIP)")
if [ -z "$IP" ]; then
echo "VM $VM_NAME not found or has no external IP."
exit 1
fi
echo "Connecting to $VM_NAME ($ZONE) at $IP..."
ssh -t "$USER@$IP" 'tmux -CC new -A -s main'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment