ssh-agent.plugin.zsh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. if [[ $USER == "root" ]]; then
  26. FILTER="ax"
  27. else
  28. FILTER="x"
  29. fi
  30. ps $FILTER | grep ssh-agent | grep -q $SSH_AGENT_PID || {
  31. _start_agent
  32. }
  33. else
  34. _start_agent
  35. fi
  36. # tidy up after ourselves
  37. unset _agent_forwarding _ssh_env_cache
  38. unfunction _start_agent