├── README.md └── ssh-agent.plugin.zsh /README.md: -------------------------------------------------------------------------------- 1 | # ssh-agent plugin 2 | 3 | This plugin starts automatically `ssh-agent` to set up and load whichever 4 | credentials you want for ssh connections. 5 | 6 | To enable it, add `ssh-agent` to your plugins: 7 | 8 | ```zsh 9 | plugins=(... ssh-agent) 10 | ``` 11 | 12 | ## Instructions 13 | 14 | To enable **agent forwarding support** add the following to your zshrc file: 15 | 16 | ```zsh 17 | zstyle :omz:plugins:ssh-agent agent-forwarding on 18 | ``` 19 | 20 | To **load multiple identities** use the `identities` style, For example: 21 | 22 | ```zsh 23 | zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github 24 | ``` 25 | 26 | To **set the maximum lifetime of the identities**, use the `lifetime` style. 27 | The lifetime may be specified in seconds or as described in sshd_config(5) 28 | (see _TIME FORMATS_). If left unspecified, the default lifetime is forever. 29 | 30 | ```zsh 31 | zstyle :omz:plugins:ssh-agent lifetime 4h 32 | ``` 33 | 34 | ## Credits 35 | 36 | Based on code from Joseph M. Reagle: http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html 37 | 38 | Agent-forwarding support based on ideas from Florent Thoumie and Jonas Pfenniger 39 | -------------------------------------------------------------------------------- /ssh-agent.plugin.zsh: -------------------------------------------------------------------------------- 1 | typeset _agent_forwarding _ssh_env_cache 2 | 3 | function _start_agent() { 4 | local lifetime 5 | local -a identities 6 | 7 | # start ssh-agent and setup environment 8 | zstyle -s :omz:plugins:ssh-agent lifetime lifetime 9 | 10 | ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache 11 | chmod 600 $_ssh_env_cache 12 | . $_ssh_env_cache > /dev/null 13 | 14 | # load identies 15 | zstyle -a :omz:plugins:ssh-agent identities identities 16 | 17 | ssh-add $HOME/.ssh/${^identities} 18 | } 19 | 20 | # Get the filename to store/lookup the environment from 21 | _ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST" 22 | 23 | # test if agent-forwarding is enabled 24 | zstyle -b :omz:plugins:ssh-agent agent-forwarding _agent_forwarding 25 | 26 | if [[ $_agent_forwarding == "yes" && -n "$SSH_AUTH_SOCK" ]]; then 27 | # Add a nifty symlink for screen/tmux if agent forwarding 28 | [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen 29 | elif [[ -f "$_ssh_env_cache" ]]; then 30 | # Source SSH settings, if applicable 31 | . $_ssh_env_cache > /dev/null 32 | ps x | grep ssh-agent | grep -q $SSH_AGENT_PID || { 33 | _start_agent 34 | } 35 | else 36 | _start_agent 37 | fi 38 | 39 | # tidy up after ourselves 40 | unset _agent_forwarding _ssh_env_cache 41 | unfunction _start_agent 42 | --------------------------------------------------------------------------------