8b2ce98578da743fbc4a208285f33744d6abd3cf

Author
Frank Dana <ferdnyc@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(ssh): add plugin (#12186)

Closes #2707 

Co-authored-by: Frank Wittig <frank.wittig@here.com>
Co-authored-by: Frank Wittig <frank@e5k.com>

Diff

 1diff --git a/plugins/ssh/ssh.plugin.zsh b/plugins/ssh/ssh.plugin.zsh
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..085e71fa1c0b445b38a8a6655e0cb68151a1ec60
 4--- /dev/null
 5+++ b/plugins/ssh/ssh.plugin.zsh
 6@@ -0,0 +1,46 @@
 7+############################################################
 8+# Take all host sections in .ssh/config and offer them for
 9+# completion as hosts (e.g. for ssh, rsync, scp and the like)
10+# Filter out wildcard host sections.
11+_ssh_configfile="$HOME/.ssh/config"
12+if [[ -f "$_ssh_configfile" ]]; then
13+  _hosts=($(egrep '^Host.*' "$_ssh_configfile" | awk '{print $2}' | grep -v '^*' | sed -e 's/\.*\*$//'))
14+  zstyle ':completion:*:hosts' hosts $_hosts
15+  unset _hosts
16+fi
17+unset _ssh_configfile
18+
19+############################################################
20+# Remove host key from known hosts based on a host section
21+# name from .ssh/config
22+function ssh_rmhkey {
23+  local ssh_configfile="$HOME/.ssh/config"
24+  local ssh_host="$1"
25+  if [[ -z "$ssh_host" ]]; then return; fi
26+  ssh-keygen -R $(grep -A10 "$ssh_host" "$ssh_configfile" | grep -i HostName | head -n 1 | awk '{print $2}')
27+}
28+compctl -k hosts ssh_rmhkey
29+
30+############################################################
31+# Load SSH key into agent
32+function ssh_load_key() {
33+  local key="$1"
34+  if [[ -z "$key" ]]; then return; fi
35+  local keyfile="$HOME/.ssh/$key"
36+  local keysig=$(ssh-keygen -l -f "$keyfile")
37+  if ( ! ssh-add -l | grep -q "$keysig" ); then
38+    ssh-add "$keyfile"
39+  fi
40+}
41+
42+############################################################
43+# Remove SSH key from agent
44+function ssh_unload_key {
45+  local key="$1"
46+  if [[ -z "$key" ]]; then return; fi
47+  local keyfile="$HOME/.ssh/$key"
48+  local keysig=$(ssh-keygen -l -f "$keyfile")
49+  if ( ssh-add -l | grep -q "$keysig" ); then
50+    ssh-add -d "$keyfile"
51+  fi
52+}