Sunday, April 5, 2009

Emacs Macro & ssh

Recently, I've been trying to customize emacs to do more and more stuff for me that I find tedious.

One of those things is setting up the ssh sessions that I use all the time. Often, it is nice to have them in an emacs session (although sometimes it's a pain, like when you want to run emacs in that session)

Multi-session does a pretty good job, but I was still having to connect to where I wanted to go and then rename the session to reflect where I was (i.e. work, live, etc)

I spent a bit of time one day making a macro for emacs lisp which did everything I really needed (warning, this requires multi-term.el to be loaded)


(defmacro defterm (termname &rest body)
(let ((mytermname (concatenate 'string
"term-" termname))
(intertermname (concatenate 'string
"*" termname "*")))
`(defun ,(intern mytermname) ()
(interactive)
(if (get-buffer ,mytermname)
(switch-to-buffer ,mytermname)
(progn
(make-term ,@(cons termname body))
(switch-to-buffer ,intertermname)
(rename-buffer ,mytermname)
(term-char-mode)
(multi-term-handle-close))))))



After this I can use the same arguments as you would for make-term, except that the first argument is the name of the terminal. So, for example, if I wanted to log into a remote server and then log into another remote server that is behind the firewall, I would call it like so:

(defterm "brutus"
"ssh" nil "-t"
"kelly@servera.somewhere.net"
"ssh" "-t"
"kelly@brutus.somewhere.net")


This would create function term-brutus that would create a buffer called term-brutus that was logged into the remote machine, or it would find that buffer if it already existed.

If you have a bunch of these, and want to do them all at the same time without
having to call each separately:


. . .
(defterm "bash" "bash" nil)
(defun start-terms ()
(interactive)
(term-greywolf)
(term-live)
(term-postgres)
(term-bash))



As a side note, I discovered while working with this that you can chain your ssh calls in this manner

ssh -t kpm@servera.somewhere.net ssh -t kpm@serverb.somewhere.net

and this would log in to the first machine, call ssh to the second machine and that's what you wanted in the first place. (don't ask about port forwarding doing this, I haven't tried it.)

Hope this helps someone.

1 comment:

  1. It is alreadywow gold at war wow goldin Afghanistan, about as far away as could be from the plains and citieswow gold of western Europe it was set up to defend against the Red Army. Now there is talk of a new strategic doctrine, one that would give it the freedom to intervene even more in conflicts around the world. Take note of the words of US President Barack Obama's National Security Adviser Generalwow gold James Jones. He was once Nato's supreme wow goldwow goldcommander and has ideas about Nato's future.

    ReplyDelete