Howard Abrams has a nifty video on his setup for invoking remote shells. Those of you who have followed his recent videos know that he works with a lot of virtual machines and it is convenient for him to be able to swiftly connect to them—from within Emacs, of course.
To do that he first builds a hash table (this happens automatically) mapping host nickname to IP address or fully qualified domain name. Then he does a completing read to get the nickname, and hence the IP address or FQDN, of the host he wants to connect to. The rest is easy: he essentially executes the code
(defun remote-host (host) (let ((default-directory (format "/%s:" host))) (shell host)))
where host
is the result of the hash table mapping.
Notice the nice trick here. By setting default-directory
to a remote host, tramp
is automatically invoked and you’re running a shell on the remote machine. By setting default-directory
to something like /ssh:host|sudo:host:
you can invoke the shell as root. See Abrams’ code for the details.
Naturally, his setup is a little more complex but this captures its essence. He also has a function to run a command on a remote host using the same strategy but calling shell-command
instead of shell
. He even shows how to run the same command on a list of remote hosts all automatically. You can see how this would be useful if you’re administering several machines.
In the video, he doesn’t spend a lot of time going over the code so you’ll want to check it out at on his github. The video is only 6 minutes 42 seconds so you can enjoy it during a coffee break. Highly recommended.
UPDATE
: Fixed link to the github code. Abrams → Abrams’.