ssh-agent.plugin.zsh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #
  10. # CREDITS
  11. #
  12. # Based on code from Joseph M. Reagle
  13. # http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
  14. #
  15. # Agent forwarding support based on ideas from
  16. # Florent Thoumie and Jonas Pfenniger
  17. #
  18. local _plugin__ssh_env=$HOME/.ssh/environment-$HOST
  19. local _plugin__forwarding
  20. function _plugin__start_agent()
  21. {
  22. /usr/bin/env ssh-agent | sed 's/^echo/#echo/' > ${_plugin__ssh_env}
  23. chmod 600 ${_plugin__ssh_env}
  24. . ${_plugin__ssh_env} > /dev/null
  25. /usr/bin/ssh-add;
  26. }
  27. # test if agent-forwarding is enabled
  28. zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding
  29. if [[ ${_plugin__forwarding} == "yes" && -z $SSH_AGENT_PID && -n "$SSH_AUTH_SOCK" ]]; then
  30. # No PID but a AUTH_SOCK means agent forwarding is enabled
  31. # Add a nifty symlink for screen/tmux
  32. [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
  33. elif [ -f "${_plugin__ssh_env}" ]; then
  34. # Source SSH settings, if applicable
  35. . ${_plugin__ssh_env} > /dev/null
  36. ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
  37. _plugin__start_agent;
  38. }
  39. else
  40. _plugin__start_agent;
  41. fi
  42. # tidy up after ourselves
  43. unfunction _plugin__start_agent
  44. unset _plugin__forwarding
  45. unset _plugin__ssh_env