summaryrefslogtreecommitdiff
path: root/plugins/ssh/ssh.plugin.zsh
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2026-01-04 22:47:54 -0800
committerTuowen Zhao <ztuowen@gmail.com>2026-01-04 22:47:54 -0800
commit2aa4cb7a52b28722816ecfd55f3b06293332c55c (patch)
treef02a9f3d59d109c70caf932a24e43368994e0e8c /plugins/ssh/ssh.plugin.zsh
parent7e951c254e779ff0620537cf43ca69dd878387b4 (diff)
parentd23d3ea69fdb839088e6e5589557cce77b34aaf8 (diff)
downloadzsh-2aa4cb7a52b28722816ecfd55f3b06293332c55c.tar.gz
zsh-2aa4cb7a52b28722816ecfd55f3b06293332c55c.tar.bz2
zsh-2aa4cb7a52b28722816ecfd55f3b06293332c55c.zip
Merge remote-tracking branch 'github/master'HEADmaster
Diffstat (limited to 'plugins/ssh/ssh.plugin.zsh')
-rw-r--r--plugins/ssh/ssh.plugin.zsh53
1 files changed, 53 insertions, 0 deletions
diff --git a/plugins/ssh/ssh.plugin.zsh b/plugins/ssh/ssh.plugin.zsh
new file mode 100644
index 000000000..24ad88028
--- /dev/null
+++ b/plugins/ssh/ssh.plugin.zsh
@@ -0,0 +1,53 @@
+############################################################
+# 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
+ _ssh_hosts=($(
+ grep -E '^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
+
+############################################################
+# 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
+}