From dc31a76e786bf3ca94b9bf08a32bb577180653d6 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sun, 11 Aug 2019 08:53:38 +0430 Subject: [PATCH] fix(ssh-agent): if ssh id_rsa exist --- 1bash.d/ssh-agent.sh | 45 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/1bash.d/ssh-agent.sh b/1bash.d/ssh-agent.sh index 1ca23a2..6704f80 100644 --- a/1bash.d/ssh-agent.sh +++ b/1bash.d/ssh-agent.sh @@ -8,27 +8,32 @@ then source $SSH_AGENT_INFO fi -if ssh-add -l > /dev/null +if [[ -f ~/.ssh/id_rsa ]] then - echo 'ssh agent identify key is loaded!' -else - if [ ! -z $SSH_AGENT_PID ] - then - # echo 'Kill the agent' - ssh-agent -k > /dev/null || echo 'Cannot kill ssh-agent' - fi - if [ -e $SSH_AUTH_SOCK ] - then - # echo 'Remove agent socket' - rm -f $SSH_AUTH_SOCK || echo 'Cannot remove agent socket! ($SSH_AUTH_SOCK)' - fi - if [[ -e $SSH_AGENT_INFO ]] + + if ssh-add -l > /dev/null then - # echo 'Remove agent info' - rm -f $SSH_AGENT_INFO || echo 'Cannot remove agent info! ($SSH_AGENT_INFO)' + echo 'ssh agent identify key is loaded!' + else + if [ ! -z $SSH_AGENT_PID ] + then + # echo 'Kill the agent' + ssh-agent -k > /dev/null || echo 'Cannot kill ssh-agent' + fi + if [ -e $SSH_AUTH_SOCK ] + then + # echo 'Remove agent socket' + rm -f $SSH_AUTH_SOCK || echo 'Cannot remove agent socket! ($SSH_AUTH_SOCK)' + fi + if [[ -e $SSH_AGENT_INFO ]] + then + # echo 'Remove agent info' + rm -f $SSH_AGENT_INFO || echo 'Cannot remove agent info! ($SSH_AGENT_INFO)' + fi + + echo 'Start ssh-agent' + ssh-agent -a "$SSH_AUTH_SOCK" -s > $SSH_AGENT_INFO + ssh-add fi - echo 'Start ssh-agent' - ssh-agent -a "$SSH_AUTH_SOCK" -s > $SSH_AGENT_INFO - ssh-add -fi +fi \ No newline at end of file