From 8b2ce98578da743fbc4a208285f33744d6abd3cf Mon Sep 17 00:00:00 2001 From: Frank Dana Date: Tue, 30 Jan 2024 05:43:03 -0500 Subject: feat(ssh): add plugin (#12186) Closes #2707 Co-authored-by: Frank Wittig Co-authored-by: Frank Wittig --- plugins/ssh/ssh.plugin.zsh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 plugins/ssh/ssh.plugin.zsh (limited to 'plugins/ssh/ssh.plugin.zsh') diff --git a/plugins/ssh/ssh.plugin.zsh b/plugins/ssh/ssh.plugin.zsh new file mode 100644 index 000000000..085e71fa1 --- /dev/null +++ b/plugins/ssh/ssh.plugin.zsh @@ -0,0 +1,46 @@ +############################################################ +# Take all host sections in .ssh/config and offer them for +# completion as hosts (e.g. for ssh, rsync, scp and the like) +# Filter out wildcard host sections. +_ssh_configfile="$HOME/.ssh/config" +if [[ -f "$_ssh_configfile" ]]; then + _hosts=($(egrep '^Host.*' "$_ssh_configfile" | awk '{print $2}' | grep -v '^*' | sed -e 's/\.*\*$//')) + zstyle ':completion:*:hosts' hosts $_hosts + unset _hosts +fi +unset _ssh_configfile + +############################################################ +# Remove host key from known hosts based on a host section +# name from .ssh/config +function ssh_rmhkey { + local ssh_configfile="$HOME/.ssh/config" + local ssh_host="$1" + if [[ -z "$ssh_host" ]]; then return; fi + ssh-keygen -R $(grep -A10 "$ssh_host" "$ssh_configfile" | grep -i HostName | head -n 1 | awk '{print $2}') +} +compctl -k hosts ssh_rmhkey + +############################################################ +# Load SSH key into agent +function ssh_load_key() { + local key="$1" + if [[ -z "$key" ]]; then return; fi + local keyfile="$HOME/.ssh/$key" + local keysig=$(ssh-keygen -l -f "$keyfile") + if ( ! ssh-add -l | grep -q "$keysig" ); then + ssh-add "$keyfile" + fi +} + +############################################################ +# Remove SSH key from agent +function ssh_unload_key { + local key="$1" + if [[ -z "$key" ]]; then return; fi + local keyfile="$HOME/.ssh/$key" + local keysig=$(ssh-keygen -l -f "$keyfile") + if ( ssh-add -l | grep -q "$keysig" ); then + ssh-add -d "$keyfile" + fi +} -- cgit v1.2.3-70-g09d2 From 50fd98e5a6c9d7c36b0cc678f577f7ce1d181bea Mon Sep 17 00:00:00 2001 From: Gam <1348187+Y3K@users.noreply.github.com> Date: Thu, 29 Feb 2024 14:59:27 -0600 Subject: fix(ssh): allow multiple definitions per host (#12227) --- plugins/ssh/ssh.plugin.zsh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'plugins/ssh/ssh.plugin.zsh') diff --git a/plugins/ssh/ssh.plugin.zsh b/plugins/ssh/ssh.plugin.zsh index 085e71fa1..b5b050536 100644 --- a/plugins/ssh/ssh.plugin.zsh +++ b/plugins/ssh/ssh.plugin.zsh @@ -4,9 +4,16 @@ # Filter out wildcard host sections. _ssh_configfile="$HOME/.ssh/config" if [[ -f "$_ssh_configfile" ]]; then - _hosts=($(egrep '^Host.*' "$_ssh_configfile" | awk '{print $2}' | grep -v '^*' | sed -e 's/\.*\*$//')) - zstyle ':completion:*:hosts' hosts $_hosts - unset _hosts + _ssh_hosts=($( + egrep '^Host.*' "$_ssh_configfile" |\ + awk '{for (i=2; i<=NF; i++) print $i}' |\ + sort |\ + uniq |\ + grep -v '^*' |\ + sed -e 's/\.*\*$//' + )) + zstyle ':completion:*:hosts' hosts $_ssh_hosts + unset _ssh_hosts fi unset _ssh_configfile -- cgit v1.2.3-70-g09d2 From caba9ae0347b5b52b65bccc27ff420cb77b71720 Mon Sep 17 00:00:00 2001 From: Florian Date: Tue, 21 Oct 2025 10:42:04 +0200 Subject: fix(ssh): use `grep -E` instead of `egrep` (#13380) --- plugins/ssh/ssh.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/ssh/ssh.plugin.zsh') diff --git a/plugins/ssh/ssh.plugin.zsh b/plugins/ssh/ssh.plugin.zsh index b5b050536..24ad88028 100644 --- a/plugins/ssh/ssh.plugin.zsh +++ b/plugins/ssh/ssh.plugin.zsh @@ -5,7 +5,7 @@ _ssh_configfile="$HOME/.ssh/config" if [[ -f "$_ssh_configfile" ]]; then _ssh_hosts=($( - egrep '^Host.*' "$_ssh_configfile" |\ + grep -E '^Host.*' "$_ssh_configfile" |\ awk '{for (i=2; i<=NF; i++) print $i}' |\ sort |\ uniq |\ -- cgit v1.2.3-70-g09d2