Skip to content

Instantly share code, notes, and snippets.

@soxofaan
Created September 7, 2016 22:01
Show Gist options
  • Save soxofaan/843634bc2a6b5b453f76fd9795583b8b to your computer and use it in GitHub Desktop.
Save soxofaan/843634bc2a6b5b453f76fd9795583b8b to your computer and use it in GitHub Desktop.
.xonshrc snippet with alias to reconnect SSH sockets (after reconnecting a screen session)
def _reconnect_ssh_agent_socket(args, stdin=None):
import re
print("Updating ssh-agent socket environment. Current value: $SSH_AUTH_SOCK = %s" % $SSH_AUTH_SOCK)
# Use find to list candidate paths with timestamp (as float).
raw = $(find /tmp/ssh-* -user @$(whoami) -name 'agent*' -printf '%T@:%p;')
candidates = [(float(m.group(1)), m.group(2)) for m in re.finditer('([0-9.]*):(.*?);', raw)]
# Take latest.
$SSH_AUTH_SOCK = max(candidates)[1]
print("Successfully updated. New value: $SSH_AUTH_SOCK = %s" % $SSH_AUTH_SOCK)
aliases['screenfix'] = _reconnect_ssh_agent_socket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment