ssh-agent.plugin.zsh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. typeset _agent_forwarding _ssh_env_cache
  2. function _start_agent() {
  3. local lifetime
  4. local -a identities
  5. # start ssh-agent and setup environment
  6. zstyle -s :omz:plugins:ssh-agent lifetime lifetime
  7. ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache
  8. chmod 600 $_ssh_env_cache
  9. . $_ssh_env_cache > /dev/null
  10. # load identies
  11. zstyle -a :omz:plugins:ssh-agent identities identities
  12. echo starting ssh-agent...
  13. ssh-add $HOME/.ssh/${^identities}
  14. }
  15. # Get the filename to store/lookup the environment from
  16. _ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
  17. # test if agent-forwarding is enabled
  18. zstyle -b :omz:plugins:ssh-agent agent-forwarding _agent_forwarding
  19. if [[ $_agent_forwarding == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
  20. # Add a nifty symlink for screen/tmux if agent forwarding
  21. [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
  22. elif [[ -f "$_ssh_env_cache" ]]; then
  23. # Source SSH settings, if applicable
  24. . $_ssh_env_cache > /dev/null
  25. ps x | grep ssh-agent | grep -q $SSH_AGENT_PID || {
  26. _start_agent
  27. }
  28. else
  29. _start_agent
  30. fi
  31. # tidy up after ourselves
  32. unset _agent_forwarding _ssh_env_cache
  33. unfunction _start_agent