Skip to content

Instantly share code, notes, and snippets.

@soxofaan
Created January 16, 2017 12:50
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 soxofaan/dffad18f79c03ed2df3b8e090fb666ee to your computer and use it in GitHub Desktop.
Save soxofaan/dffad18f79c03ed2df3b8e090fb666ee to your computer and use it in GitHub Desktop.
Script to fix SSH agent environment variables on a remote server after reconnecting to a screen session
#!/bin/bash
# Script to fix the SSH agent environment variable after reconnecting to a running screen session.
# Usage: *source* this script (don't just execute).
# For example, if you store it at ~/screen-ssh-agent-fix.sh, create this alias to have it available easily:
# alias screenfix='source ~/screen-ssh-agent-fix.sh'
echo "Updating ssh-agent socket environment.."
echo "Current value: $SSH_AUTH_SOCK"
export 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)
if [ $? -gt 0 ]; then
echo "ERROR!"
exit $?
fi
echo "New value: $SSH_AUTH_SOCK"
echo "All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment