Skip to content

Instantly share code, notes, and snippets.

@didyhu
Last active May 9, 2022 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save didyhu/d11217362a7ce27a4de0fd1bcab9613d to your computer and use it in GitHub Desktop.
Save didyhu/d11217362a7ce27a4de0fd1bcab9613d to your computer and use it in GitHub Desktop.
auto ssh agent
function addkey() {
autoagent
ssh-add ~/.ssh/id_rsa
export_ssh_agent
}
function autoagent() {
# cache file
ssh_auth_sock_cache="/tmp/ssh_auth_sock.cache"
ssh_agent_pid_cache="/tmp/ssh_agent_pid.cache"
if [ ! -e "${ssh_auth_sock_cache}" ] || [ ! -d "${ssh_agent_pid_cache}" ]; then
# if sock file or pid file does not exist, create a new ssh agent
# echo "removing ssh caddache"
rm "${ssh_auth_sock_cache}" -f
rm "${ssh_agent_pid_cache}" -f
eval $(ssh-agent -s)
ln -s "${SSH_AUTH_SOCK}" "${ssh_auth_sock_cache}"
ln -s "/proc/${SSH_AGENT_PID}" "${ssh_agent_pid_cache}"
fi
SSH_AUTH_SOCK=${ssh_auth_sock_cache}
SSH_AGENT_PID=$(cat -- "${ssh_agent_pid_cache}/pgid")
export SSH_AUTH_SOCK
export SSH_AGENT_PID
}
function export_ssh_agent(){
setx SSH_AUTH_SOCK $SSH_AUTH_SOCK
setx SSH_AGENT_PID $SSH_AGENT_PID
}
autoagent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment