From ffa8d28142560ec703880ccacc9feaead31646e2 Mon Sep 17 00:00:00 2001 From: AbelAnaya Date: Tue, 7 Nov 2023 18:43:49 +0100 Subject: [PATCH] refactor: Check both /etc/ssh/ssh_config and ~/.ssh/config to retrieve user under host --- scripts/ssh_session.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/scripts/ssh_session.sh b/scripts/ssh_session.sh index 7d106d1..3673dd4 100755 --- a/scripts/ssh_session.sh +++ b/scripts/ssh_session.sh @@ -39,6 +39,26 @@ get_ssh_user() { fi done + for ssh_config in `awk ' + $1 == "Host" { + gsub("\\\\.", "\\\\.", $2); + gsub("\\\\*", ".*", $2); + host = $2; + next; + } + $1 == "User" { + $1 = ""; + sub( /^[[:space:]]*/, "" ); + printf "%s|%s\n", host, $0; + }' .ssh/config`; do + local host_regex=${ssh_config%|*} + local host_user=${ssh_config#*|} + if [[ "$1" =~ $host_regex ]]; then + ssh_user=$host_user + break + fi + done + echo $ssh_user }