From 293b5aad125d40a9660e50f87fbb109aef64d5ff Mon Sep 17 00:00:00 2001 From: Jeffrey Serio <23226432+hyperreal64@users.noreply.github.com> Date: Fri, 25 Aug 2023 16:34:47 -0500 Subject: [PATCH] Just use host --- gumssh | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/gumssh b/gumssh index dcaa4bc..b0e5819 100755 --- a/gumssh +++ b/gumssh @@ -1,51 +1,32 @@ #!/usr/bin/env bash -# This script parses the ssh config at ~/.ssh/config to find user@host -# entries, then uses gum choose to select one user@host to ssh into. - -# Check for gum. if ! test -x "$(command -v gum)"; then echo "Missing dependency: gum" echo "See github.com/charmbracelet/gum" exit 1 fi -SSH_CONFIG="${HOME}/.ssh/config" -usern_array=() -hostn_array=() -logins=() +if ! test -x "$(command -v autossh)"; then + echo "Missing dependency: autossh" + exit 1 +fi -# Read Users from ssh config and store them in usern_array. -while IFS= read -r line; do - if [[ "$line" == *"User"* ]]; then - usern=$(echo "$line" | awk '{ print $2 }') - usern_array+=("$usern") - fi -done < "${SSH_CONFIG}" +SSH_CONFIG="${HOME}/.ssh/config" +hostn_array=() # Read HostNames from ssh config and store them in hostn_array. while IFS= read -r line; do if [[ "$line" == *"Host "* ]]; then - hostn=$(echo $line | awk '{ print $2 }') + hostn=$(echo "$line" | awk '{ print $2 }') hostn_array+=("$hostn") fi done < "${SSH_CONFIG}" -# Set the array_len to the size of usern_array - 1. -let "array_len = ${#usern_array[@]} - 1" - -# Iterate through usern_array and hostn_array and match user and host -# pairs, then store them in the logins array in "user@host" format. -for i in $(seq 0 $array_len); do - userhost=$(printf "%s@%s" "${usern_array[i]}" "${hostn_array[i]}") - logins+=("$userhost") -done - # Print each member of logins array on a new line and pipe to gum choose. # Store selection. -selection=$(printf "%s\n" "${logins[@]}" | gum choose --limit=1) +selection=$(printf "%s\n" "${hostn_array[@]}" | gum choose --limit=1) # If $selection is non-empty, pass it to the ssh -X command. if test -n "${selection}"; then - autossh -M 0 "${selection}" -X + autossh -M 0 "${selection}" fi