ssh-agent.plugin.zsh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #
  2. # INSTRUCTIONS
  3. #
  4. # To enabled agent forwarding support add the following to
  5. # your .zshrc file:
  6. #
  7. # zstyle :omz:plugins:ssh-agent agent-forwarding on
  8. #
  9. # To load multiple identies use the identities style, For
  10. # example:
  11. #
  12. # zstyle :omz:plugins:ssh-agent id_rsa id_rsa2 id_github
  13. #
  14. #
  15. # CREDITS
  16. #
  17. # Based on code from Joseph M. Reagle
  18. # http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
  19. #
  20. # Agent forwarding support based on ideas from
  21. # Florent Thoumie and Jonas Pfenniger
  22. #
  23. local _plugin__ssh_env=$HOME/.ssh/environment-$HOST
  24. local _plugin__forwarding
  25. function _plugin__start_agent()
  26. {
  27. local -a identities
  28. # start ssh-agent and setup environment
  29. /usr/bin/env ssh-agent | sed 's/^echo/#echo/' > ${_plugin__ssh_env}
  30. chmod 600 ${_plugin__ssh_env}
  31. . ${_plugin__ssh_env} > /dev/null
  32. # load identies
  33. zstyle -a :omz:plugins:ssh-agent identities identities
  34. echo starting...
  35. /usr/bin/ssh-add $HOME/.ssh/${^identities}
  36. }
  37. # test if agent-forwarding is enabled
  38. zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding
  39. if [[ ${_plugin__forwarding} == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
  40. # Add a nifty symlink for screen/tmux if agent forwarding
  41. [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
  42. elif [ -f "${_plugin__ssh_env}" ]; then
  43. # Source SSH settings, if applicable
  44. . ${_plugin__ssh_env} > /dev/null
  45. ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
  46. _plugin__start_agent;
  47. }
  48. else
  49. _plugin__start_agent;
  50. fi
  51. # tidy up after ourselves
  52. unfunction _plugin__start_agent
  53. unset _plugin__forwarding
  54. unset _plugin__ssh_env