bin/gumssh

33 lines
838 B
Plaintext
Raw Normal View History

2022-12-15 05:47:24 +01:00
#!/usr/bin/env bash
if ! test -x "$(command -v gum)"; then
echo "Missing dependency: gum"
echo "See github.com/charmbracelet/gum"
exit 1
fi
2023-08-25 23:34:47 +02:00
if ! test -x "$(command -v autossh)"; then
echo "Missing dependency: autossh"
exit 1
fi
2022-12-15 05:47:24 +01:00
SSH_CONFIG="${HOME}/.ssh/config"
hostn_array=()
2022-12-15 05:53:37 +01:00
# Read HostNames from ssh config and store them in hostn_array.
2022-12-15 05:47:24 +01:00
while IFS= read -r line; do
2023-07-13 06:49:23 +02:00
if [[ "$line" == *"Host "* ]]; then
2023-08-25 23:34:47 +02:00
hostn=$(echo "$line" | awk '{ print $2 }')
2023-07-13 06:49:23 +02:00
hostn_array+=("$hostn")
2022-12-15 05:47:24 +01:00
fi
done < "${SSH_CONFIG}"
2022-12-15 05:53:37 +01:00
# Print each member of logins array on a new line and pipe to gum choose.
# Store selection.
2023-08-25 23:34:47 +02:00
selection=$(printf "%s\n" "${hostn_array[@]}" | gum choose --limit=1)
2022-12-15 05:53:37 +01:00
# If $selection is non-empty, pass it to the ssh -X command.
2022-12-15 05:47:24 +01:00
if test -n "${selection}"; then
2023-08-25 23:34:47 +02:00
autossh -M 0 "${selection}"
2022-12-15 05:47:24 +01:00
fi