ssh-agent 자동실행 및 private 키 자동 등록

2023-10-30 12:26 | Linux

ssh-agent 자동 실행을 통해 계정 인증 요청이 발생하지 않도록 처리


# run ssh-agent and add private keys
SSH_ENV="$HOME/.ssh/environment"
 
function start_agent {
    echo "**** Initialising new SSH agent... ****"
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add;
    echo "ssh-add private keys..."
    FILES=~/.ssh/*
    for f in $FILES
    do
      filename=$(basename "$f")
      extension="${filename##*.}"
      if [[ $extension != "pub" && $filename != "known_hosts" && $filename != "environment" ]]; then
        echo - ssh-add $f
        ssh-add $f > /dev/null
      fi
    done
        echo "**** Agent settings complete! ****"
}
 
if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

이미지예시추가