Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

While keeping long standing emacs sessions on remote machines nside terminal multiplexers such as 'screen' and 'tmux', the SSH_AUTH_SOCK environment variable of the emacs process becomes stale after disconnection and reconnection. The interactive function SET-SSH-AGENT-SOCKET below allows for a quick and dirty way to automatically reset this variable to the latest incoming SSH agent authorization socket.

View ssh-keys.el
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
;; ssh-keys.el
 
;; While keeping long standing emacs sessions on remote machines
;; inside terminal multiplexers such as 'screen' and 'tmux', the
;; SSH_AUTH_SOCK environment variable of the emacs process becomes
;; stale after disconnection and reconnection. The interactive
;; function SET-SSH-AGENT-SOCKET below allows for a quick and dirty
;; way to automatically reset this variable to the latest incoming SSH
;; agent authorization socket.
 
(defun string-starts-with (string prefix)
(let ((plen (length prefix)))
(when (>= (length string) plen)
(string-equal (substring string 0 plen) prefix))))
 
;; Note that no attempt is made to distinguish users. This work is
;; needed in order to use this code on a machine with multiple users
;; SSHing in.
 
(defun find-latest-ssh-socket ()
"Return the latest incoming ssh agent socket."
(first
(directory-files
(caar
(sort (directory-files-and-attributes "/tmp" t "ssh-.*")
(lambda (a b)
(tod> (sixth a) (sixth b))))) t "agent.*")))
 
(defun tod> (a b)
(or (> (first a) (first b)) (> (second a) (second b))))
 
(defun set-ssh-agent-socket (&optional path)
"[Re]set the SSH_AUTH_SOCK environment variable. If PATH
is not specified, an attempt is made to discover the latest
connection into the machine."
(interactive)
(unless path
(setf path (find-latest-ssh-socket)))
(unless (file-attributes path)
(error "Socket %S was not found." path))
(setenv "SSH_AUTH_SOCK" path)
(message "sSet SSH_AUTH_SOCK to %S" path))
mtk commented

the agent binding is one part of the story, but the X display binding is the other. or do you typically operate on a tty only?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.