bin/gumssh

33 lines
904 B
Plaintext
Raw Permalink 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
2024-08-19 03:29:54 +02:00
if [[ "$line" == *"Host "* ]] && [[ "$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
2024-08-13 00:30:34 +02:00
done <"${SSH_CONFIG}"
2022-12-15 05:47:24 +01:00
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.
2024-05-13 09:36:48 +02:00
selection=$(printf "%s\n" "${hostn_array[@]}" | gum choose --header="SSH Host" --limit=1)
2022-12-15 05:53:37 +01:00
2024-05-13 09:36:48 +02:00
# If $selection is not aborted by user, pass it to the `autossh -M 0` command.
2024-08-13 00:30:34 +02:00
if [ -n "${selection}" ]; then
2023-08-25 23:34:47 +02:00
autossh -M 0 "${selection}"
2022-12-15 05:47:24 +01:00
fi