Last active
August 6, 2018 16:55
-
-
Save lucashalbert/57e3f67cbc7f5e6698aed437a024afaa to your computer and use it in GitHub Desktop.
Function to add to bashrc to remap ssh-agent after disconnect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function intended to remap an SSH agent socket after reconnection | |
function ssh-reagent() { | |
# find new ssh-agent socket | |
SSH_AUTH_SOCK=$(find /tmp/ssh-* -user `whoami` -name agent\* -printf '%T@ %p\n' 2>/dev/null | sort -k 1nr | sed 's/^[^ ]* //' | head -n 1) | |
# Check if SSH_AUTH_SOCK is a socket and if it is a symbolic link | |
if [ -S "${SSH_AUTH_SOCK}" ] && [ ! -h "${SSH_AUTH_SOCK}" ]; then | |
# Map SSH_AUTH_SOCK to Canonical Socket in ~/.ssh/ | |
echo "Mapping ${SSH_AUTH_SOCK} to ~/.ssh/ssh_auth_sock as a symbolic link" | |
ln -sf ${SSH_AUTH_SOCK} ~/.ssh/ssh_auth_sock | |
# Export Canonical Socket location as SSH_AUTH_SOCK | |
export SSH_AUTH_SOCK="$HOME/.ssh/ssh_auth_sock" | |
else | |
if [ ! -S "${SSH_AUTH_SOCK}" ]; then | |
echo "WARNING: ${SSH_AUTH_SOCK} is not a socket" | |
elif [ -h "${SSH_AUTH_SOCK}" ]; then | |
echo "OK: ${SSH_AUTH_SOCK} is already a symbolic link" | |
else | |
echo "UNKNOWN: Something went very wrong!" | |
echo "SSH_AUTH_SOCK Variable: ${SSH_AUTH_SOCK}" | |
ls -lah ${SSH_AUTH_SOCK} | |
fi | |
fi | |
if $(ssh-add -l > /dev/null); then | |
echo "OK: Working SSH agent found at ${SSH_AUTH_SOCK}" | |
ssh-add -l | |
return 0 | |
else | |
echo "WARNING: No Working SSH agent was found at ${SSH_AUTH_SOCK}" | |
echo "Try initializing ssh-agent using the command 'eval \$(ssh-agent)'" | |
return 1 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment