From 0da8fb32b4856505174dec5a354062c0203bf14f Mon Sep 17 00:00:00 2001 From: Changwoo Park Date: Thu, 13 Oct 2011 14:34:15 +0900 Subject: [PATCH 1/3] Add a plugin for ssh_agent --- plugins/available/ssh_agent.plugin.bash | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 plugins/available/ssh_agent.plugin.bash diff --git a/plugins/available/ssh_agent.plugin.bash b/plugins/available/ssh_agent.plugin.bash new file mode 100644 index 00000000..b403ac17 --- /dev/null +++ b/plugins/available/ssh_agent.plugin.bash @@ -0,0 +1,20 @@ +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" + + #28800, 8 hours. then go home + ssh-agent -t $MY_TIMEOUT | grep -v echo > ~/.ssh/ssh_agent_rc + + . ~/.ssh/ssh_agent_rc + + echo "ssh-agent::ssh-add:" + ssh-add +else + . ~/.ssh/ssh_agent_rc + +fi; + +#alias kagent="kill -9 $SSH_AGENT_PID" From 6cccf6e575ba99a181c9d3bf2ae860b4840740dd Mon Sep 17 00:00:00 2001 From: Changwoo Park Date: Fri, 14 Oct 2011 10:44:45 +0900 Subject: [PATCH 2/3] Fix ssh-add bug and refactor Bug: Once key is timeout, then never re-add key until new ssh-agent. It fixed. --- plugins/available/ssh_agent.plugin.bash | 44 +++++++++++++++++-------- 1 file changed, 31 insertions(+), 13 deletions(-) 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" From 8f8a1686b89ebe872a254b132738ceb85a800220 Mon Sep 17 00:00:00 2001 From: Changwoo Park Date: Fri, 28 Oct 2011 19:51:44 +0900 Subject: [PATCH 3/3] fix to avoid other's ssh-agent --- plugins/available/ssh_agent.plugin.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/available/ssh_agent.plugin.bash b/plugins/available/ssh_agent.plugin.bash index 83a012fd..f085b744 100644 --- a/plugins/available/ssh_agent.plugin.bash +++ b/plugins/available/ssh_agent.plugin.bash @@ -8,7 +8,7 @@ function get_ssh_agent_timeout(){ } function bind_ssh_agent(){ - local agent_pid=`ps -ef | grep ssh-agent | grep -v grep | awk '{print $2}' | xargs` + local agent_pid=`ps -U $USER | grep ssh-agent | grep -v grep | awk '{print $1}' | xargs` if [ "$agent_pid" = "" ]; then ssh-agent -t $(get_ssh_agent_timeout) > ~/.ssh/ssh_agent_rc