ssh-agent.plugin.zsh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 identities use the identities style, For
  10. # example:
  11. #
  12. # zstyle :omz:plugins:ssh-agent id_rsa id_rsa2 id_github
  13. #
  14. # To set the maximum lifetime of the identities, use the
  15. # lifetime style. The lifetime may be specified in seconds
  16. # or as described in sshd_config(5) (see TIME FORMATS)
  17. # If left unspecified, the default lifetime is forever.
  18. #
  19. # zstyle :omz:plugins:ssh-agent lifetime 4h
  20. #
  21. # CREDITS
  22. #
  23. # Based on code from Joseph M. Reagle
  24. # http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
  25. #
  26. # Agent forwarding support based on ideas from
  27. # Florent Thoumie and Jonas Pfenniger
  28. #
  29. local _plugin__ssh_env=$HOME/.ssh/environment-$HOST
  30. local _plugin__forwarding
  31. function _plugin__start_agent()
  32. {
  33. local -a identities
  34. local lifetime
  35. zstyle -s :omz:plugins:ssh-agent lifetime lifetime
  36. # start ssh-agent and setup environment
  37. /usr/bin/env ssh-agent ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' > ${_plugin__ssh_env}
  38. chmod 600 ${_plugin__ssh_env}
  39. . ${_plugin__ssh_env} > /dev/null
  40. # load identies
  41. zstyle -a :omz:plugins:ssh-agent identities identities
  42. echo starting ssh-agent...
  43. /usr/bin/ssh-add $HOME/.ssh/${^identities}
  44. }
  45. # test if agent-forwarding is enabled
  46. zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding
  47. if [[ ${_plugin__forwarding} == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
  48. # Add a nifty symlink for screen/tmux if agent forwarding
  49. [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
  50. elif [ -f "${_plugin__ssh_env}" ]; then
  51. # Source SSH settings, if applicable
  52. . ${_plugin__ssh_env} > /dev/null
  53. ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
  54. _plugin__start_agent;
  55. }
  56. else
  57. _plugin__start_agent;
  58. fi
  59. # tidy up after ourselves
  60. unfunction _plugin__start_agent
  61. unset _plugin__forwarding
  62. unset _plugin__ssh_env