ssh-agent.plugin.zsh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #
  2. # INSTRUCTIONS
  3. #
  4. # To enable 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 identities 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
  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. # Get the filename to store/lookup the environment from
  46. if (( $+commands[scutil] )); then
  47. # It's OS X!
  48. _plugin__ssh_env="$HOME/.ssh/environment-$(scutil --get ComputerName)"
  49. else
  50. _plugin__ssh_env="$HOME/.ssh/environment-$HOST"
  51. fi
  52. # test if agent-forwarding is enabled
  53. zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding
  54. if [[ ${_plugin__forwarding} == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
  55. # Add a nifty symlink for screen/tmux if agent forwarding
  56. [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
  57. elif [ -f "${_plugin__ssh_env}" ]; then
  58. # Source SSH settings, if applicable
  59. . ${_plugin__ssh_env} > /dev/null
  60. ps x | grep ${SSH_AGENT_PID} | grep ssh-agent > /dev/null || {
  61. _plugin__start_agent;
  62. }
  63. else
  64. _plugin__start_agent;
  65. fi
  66. # tidy up after ourselves
  67. unfunction _plugin__start_agent
  68. unset _plugin__forwarding
  69. unset _plugin__ssh_env