add vm bin script

This commit is contained in:
Tomas Mirchev 2025-09-15 07:05:21 +02:00
parent 97ed2db0f7
commit 616ba97325

71
config/macos/bin/vm Executable file
View File

@ -0,0 +1,71 @@
#!/usr/bin/env bash
# ~/bin/vm
#
# SSH helper script for connecting to VMs or workstations.
# Usage:
# vm [--no-tmux] <host>
#
# Host patterns:
# personal-orb -> ssh $USER@personal@orb
# personal-utm -> ssh $USER@personal.utm.local
# personal-workstation -> ssh $USER@personal.workstation.lan
#
# Optional:
# --no-tmux -> skips attaching to tmux session
skip_tmux=0
# Check for --no-tmux flag
if [[ "$1" == "--no-tmux" ]]; then
skip_tmux=1
shift
fi
full="$1"
user="$USER"
host="$full"
# Split user@host if explicitly specified
if [[ "$full" == *@* ]]; then
user="${full%@*}"
host="${full#*@}"
fi
# Map host patterns to SSH target and tmux session
case "$host" in
*-orb)
ssh_host="${host%-orb}@orb"
tmux_session="$host"
;;
*-utm)
ssh_host="${host%-utm}.utm.local"
tmux_session="$host"
ssh_identity="$HOME/.ssh/id_ed25519_internal"
;;
*-workstation)
ssh_host="${host%-workstation}.workstation.lan"
tmux_session="$host"
ssh_identity="$HOME/.ssh/id_ed25519_internal"
;;
*)
echo "Error: unknown host pattern '$host'" >&2
exit 1
;;
esac
# Build SSH command safely using an array
ssh_cmd=(ssh -t)
# Include identity file if needed
[[ -n "$ssh_identity" ]] && ssh_cmd+=(-i "$ssh_identity" -o IdentitiesOnly=yes)
# Add target user and host
ssh_cmd+=("$user@$ssh_host")
# Add tmux session unless skipped
[[ $skip_tmux -eq 0 ]] && ssh_cmd+=("tmux" "new" "-A" "-s" "$tmux_session")
# Replace current shell with SSH command
# echo "Executing: ${ssh_cmd[*]}" >&2
exec "${ssh_cmd[@]}"