Skip to content

Instantly share code, notes, and snippets.

@tychoish
Last active May 3, 2023 14:23
Show Gist options
  • Save tychoish/72b9e77434785b66e4a02019b1673820 to your computer and use it in GitHub Desktop.
Save tychoish/72b9e77434785b66e4a02019b1673820 to your computer and use it in GitHub Desktop.
(defun ssh-reagent ()
(interactive)
(let* ((sshdir (car (directory-files "/tmp" nil "ssh-*")))
(agent (car (directory-files (concat "/tmp/" sshdir) nil "agent.*"))))
(setenv "SSH_AUTH_SOCK" (concat "/tmp/" sshdir "/" agent)))
(message "Attached to SSH Session"))
func GetSSHAgentPath() (out string, err error) {
usr, err := user.Current()
if err != nil {
return "", err
}
if path := filepath.Join("/run/user", usr.Uid, "ssh-agent-socket"); FileExists(path) {
return path, nil
}
err = filepath.Walk("/tmp", func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
return nil
}
if !strings.HasPrefix(path, "/tmp/ssh-") {
return nil
}
out = path
return io.EOF
})
if out == "" || err == nil {
return "", errors.New("could not find ssh socket")
}
return out, nil
}
[Unit]
Description=SSH key agent
[Service]
Type=simple
Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
ExecStart=/usr/bin/ssh-agent -D -a $SSH_AUTH_SOCK
[Install]
WantedBy=default.target
ssh-reagent () {
for agent in /tmp/ssh-*/agent.*
do
export SSH_AUTH_SOCK=$agent
if ssh-add -l 2>&1 > /dev/null
then
echo Found working SSH Agent:
ssh-add -l
return
fi
done
echo Cannot find ssh agent - maybe you should reconnect and forward it?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment