2
0

ssh.plugin.zsh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ############################################################
  2. # Take all host sections in .ssh/config and offer them for
  3. # completion as hosts (e.g. for ssh, rsync, scp and the like)
  4. # Filter out wildcard host sections.
  5. _ssh_configfile="$HOME/.ssh/config"
  6. if [[ -f "$_ssh_configfile" ]]; then
  7. _ssh_hosts=($(
  8. egrep '^Host.*' "$_ssh_configfile" |\
  9. awk '{for (i=2; i<=NF; i++) print $i}' |\
  10. sort |\
  11. uniq |\
  12. grep -v '^*' |\
  13. sed -e 's/\.*\*$//'
  14. ))
  15. zstyle ':completion:*:hosts' hosts $_ssh_hosts
  16. unset _ssh_hosts
  17. fi
  18. unset _ssh_configfile
  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. # Load SSH key into agent
  31. function ssh_load_key() {
  32. local key="$1"
  33. if [[ -z "$key" ]]; then return; fi
  34. local keyfile="$HOME/.ssh/$key"
  35. local keysig=$(ssh-keygen -l -f "$keyfile")
  36. if ( ! ssh-add -l | grep -q "$keysig" ); then
  37. ssh-add "$keyfile"
  38. fi
  39. }
  40. ############################################################
  41. # Remove SSH key from agent
  42. function ssh_unload_key {
  43. local key="$1"
  44. if [[ -z "$key" ]]; then return; fi
  45. local keyfile="$HOME/.ssh/$key"
  46. local keysig=$(ssh-keygen -l -f "$keyfile")
  47. if ( ssh-add -l | grep -q "$keysig" ); then
  48. ssh-add -d "$keyfile"
  49. fi
  50. }