Assorted shell and Python scripts
1#!/usr/bin/env bash
2
3if ! test -x "$(command -v gum)"; then
4 echo "Missing dependency: gum"
5 echo "See github.com/charmbracelet/gum"
6 exit 1
7fi
8
9if ! test -x "$(command -v autossh)"; then
10 echo "Missing dependency: autossh"
11 exit 1
12fi
13
14SSH_CONFIG="${HOME}/.ssh/config"
15hostn_array=()
16
17# Read HostNames from ssh config and store them in hostn_array.
18while IFS= read -r line; do
19 if [[ "$line" == *"Host "* ]] && [[ "$line" != *"Host *" ]]; then
20 hostn=$(echo "$line" | awk '{ print $2 }')
21 hostn_array+=("$hostn")
22 fi
23done <"${SSH_CONFIG}"
24
25# Print each member of logins array on a new line and pipe to gum choose.
26# Store selection.
27selection=$(printf "%s\n" "${hostn_array[@]}" | gum choose --header="SSH Host" --limit=1)
28
29# If $selection is not aborted by user, pass it to the `autossh -M 0` command.
30if [ -n "${selection}" ]; then
31 autossh -M 0 "${selection}"
32fi