From 6de1ad9d3dd4fdddf327f6201617409a8697dd43 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 5 Jul 2018 22:29:39 -0400 Subject: [PATCH] [completion] Filter out non-hostnames in SSH config file (#1329) * Correctly exclude SSH config options with Host SSH config files have 14 options containing 'Host'. Previously The zsh and bash completion scripts would include lines containing these options when doing command-line completion of SSH hosts with `ssh **`. This commit fixes that problem by only including lines with 'host '. * Don't autocomplete SSH hostnames using ? SSH config files support ? as well as * for wildcards in Host lines. This commit excludes lines containing ? for zsh/bash command line completeion using `ssh **` --- shell/completion.bash | 2 +- shell/completion.zsh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/completion.bash b/shell/completion.bash index 86b78526..93369954 100644 --- a/shell/completion.bash +++ b/shell/completion.bash @@ -241,7 +241,7 @@ _fzf_complete_telnet() { _fzf_complete_ssh() { _fzf_complete '+m' "$@" < <( - cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host' | command grep -v '*' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}') \ + cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host ' | command grep -v '[*?]' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}') \ <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \ <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') | awk '{if (length($2) > 0) {print $2}}' | sort -u diff --git a/shell/completion.zsh b/shell/completion.zsh index 1e3ced8e..4df21b33 100644 --- a/shell/completion.zsh +++ b/shell/completion.zsh @@ -114,7 +114,7 @@ _fzf_complete_telnet() { _fzf_complete_ssh() { _fzf_complete '+m' "$@" < <( - command cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host' | command grep -v '*' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}') \ + command cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host ' | command grep -v '[*?]' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}') \ <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \ <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') | awk '{if (length($2) > 0) {print $2}}' | sort -u