diff --git a/plugins/available/ssh_agent.plugin.bash b/plugins/available/ssh_agent.plugin.bash index b403ac17..83a012fd 100644 --- a/plugins/available/ssh_agent.plugin.bash +++ b/plugins/available/ssh_agent.plugin.bash @@ -1,20 +1,38 @@ -RUNINNG_AGENT_PID=`ps -ef | grep ssh-agent | grep -v grep | awk '{print $2}' | xargs` - -if [ "$RUNINNG_AGENT_PID" = "" ] && [ -f ~/.ssh/id_rsa ]; then - MY_TIMEOUT=28800 - - [ "$SSH_AGENT_TIMEOUT" = "" ] || MY_TIMEOUT="$SSH_AGENT_TIMEOUT" - +function get_ssh_agent_timeout(){ #28800, 8 hours. then go home - ssh-agent -t $MY_TIMEOUT | grep -v echo > ~/.ssh/ssh_agent_rc + local my_timeout=28800 - . ~/.ssh/ssh_agent_rc + [ "$SSH_AGENT_TIMEOUT" != "" ] && my_timeout="$SSH_AGENT_TIMEOUT" - echo "ssh-agent::ssh-add:" - ssh-add -else - . ~/.ssh/ssh_agent_rc + echo $my_timeout +} + +function bind_ssh_agent(){ + local agent_pid=`ps -ef | grep ssh-agent | grep -v grep | awk '{print $2}' | xargs` + + if [ "$agent_pid" = "" ]; then + ssh-agent -t $(get_ssh_agent_timeout) > ~/.ssh/ssh_agent_rc + + . ~/.ssh/ssh_agent_rc + + else + . ~/.ssh/ssh_agent_rc + fi; +} + +function ssh_add_to_ssh_agent(){ + local id_key_file=$1 + local added=`ssh-add -l | grep $id_key_file` + + [ "$added" = "" ] && ssh-add -t $(get_ssh_agent_timeout) +} + +MY_KEY_FILE=~/.ssh/id_rsa + +if [ "$MY_KEY_FILE" != "" ]; then + bind_ssh_agent + ssh_add_to_ssh_agent $MY_KEY_FILE fi; #alias kagent="kill -9 $SSH_AGENT_PID"