bin/gumssh

33 lines
904 B
Plaintext
Raw Normal View History

2022-12-14 22:47:24 -06: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 16:34:47 -05:00
if ! test -x "$(command -v autossh)"; then
echo "Missing dependency: autossh"
exit 1
fi
2022-12-14 22:47:24 -06:00
SSH_CONFIG="${HOME}/.ssh/config"
hostn_array=()
2022-12-14 22:53:37 -06:00
# Read HostNames from ssh config and store them in hostn_array.
2022-12-14 22:47:24 -06:00
while IFS= read -r line; do
2024-08-18 20:29:54 -05:00
if [[ "$line" == *"Host "* ]] && [[ "$line" != *"Host *" ]]; then
2023-08-25 16:34:47 -05:00
hostn=$(echo "$line" | awk '{ print $2 }')
2023-07-12 23:49:23 -05:00
hostn_array+=("$hostn")
2022-12-14 22:47:24 -06:00
fi
2024-08-12 17:30:34 -05:00
done <"${SSH_CONFIG}"
2022-12-14 22:47:24 -06:00
2022-12-14 22:53:37 -06:00
# Print each member of logins array on a new line and pipe to gum choose.
# Store selection.
2024-05-13 02:36:48 -05:00
selection=$(printf "%s\n" "${hostn_array[@]}" | gum choose --header="SSH Host" --limit=1)
2022-12-14 22:53:37 -06:00
2024-05-13 02:36:48 -05:00
# If $selection is not aborted by user, pass it to the `autossh -M 0` command.
2024-08-12 17:30:34 -05:00
if [ -n "${selection}" ]; then
2023-08-25 16:34:47 -05:00
autossh -M 0 "${selection}"
2022-12-14 22:47:24 -06:00
fi