Skip to content

Instantly share code, notes, and snippets.

@whalliburton
Created July 10, 2013 16:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whalliburton/5967969 to your computer and use it in GitHub Desktop.
Save whalliburton/5967969 to your computer and use it in GitHub Desktop.
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 var…
;; 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
Copy link

mtk commented Jul 10, 2013

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