From ceb983279db9e239b079864379817bf1dd0b3140 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Tue, 4 Aug 2020 03:03:35 +0000 Subject: [PATCH] Import known host parser from @uwerler. --- completions/ssh.ksh | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/completions/ssh.ksh b/completions/ssh.ksh index 4e7b104..6ceafa2 100755 --- a/completions/ssh.ksh +++ b/completions/ssh.ksh @@ -1,14 +1,29 @@ #: | ssh | ssh known hosts | -set -A SSH_KNOWN_HOSTS ~/.ssh/known_hosts +read_known_hosts() { + local _file=$1 _line _host -if [ -f /etc/ssh/ssh_known_hosts ]; then - SSH_KNOWN_HOSTS="${SSH_KNOWN_HOSTS[@]} /etc/ssh/ssh_known_hosts" -fi + while read _line ; do + _line=${_line%%#*} # delete comments + _line=${_line%%@*} # ignore markers + _line=${_line%% *} # keep only host field -HOST_LIST=$(awk \ - '{split($1,a,","); gsub("].*", "", a[1]); gsub("\\[", "", a[1]); print a[1] " root@" a[1]}' \ - $SSH_KNOWN_HOSTS | sort | uniq) + [[ -z $_line ]] && continue -set -A complete_ssh -- $HOST_LIST -set -A complete_scp -- $HOST_LIST -set -A complete_mosh -- $HOST_LIST + local IFS=, + for _host in $_line; do + _host=${_host#\[} + _host=${_host%%\]*} + for i in ${HOST_LIST[*]}; do + [[ $_host == $i ]] && continue 2 + done + set -s -A HOST_LIST ${HOST_LIST[*]} $_host + done + done <$_file +} + +[[ -s /etc/ssh/ssh_known_hosts ]] && read_known_hosts /etc/ssh/ssh_known_hosts +[[ -s ~/.ssh/known_hosts ]] && read_known_hosts ~/.ssh/known_hosts + +set -A complete_ssh -- ${HOST_LIST[*]} +set -A complete_scp -- ${HOST_LIST[*]} +set -A complete_mosh -- ${HOST_LIST[*]}