Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote ssh connection from within Emacs

Tags:

emacs

ssh

Several questions, including this one, discuss aspects relating to ssh connections from within Emacs. I haven't found an answer to my question though: How can I ssh into a remote machine from within Emacs?

I do not wish to edit a file on the remote machine from within Emacs. I am aware of M-x shell which opens a shell on my local machine and I am aware of using TRAMP to edit a file over ssh on the remote machine. However, neither of these relate to this question.

(Instead of voting to close, maybe migrate the question to another site.)

Edit: Related discussion here.

like image 780
SabreWolfy Avatar asked May 08 '12 08:05

SabreWolfy


People also ask

Can you use Emacs over SSH?

The Emacs “Tramp” functionality brings the power of SSH into your text editor. You no longer need to open a separate terminal window in order to run scripts, edit files, or perform other server maintenance.

What is Emacs tramp?

TRAMP is for transparently accessing remote files from within Emacs. TRAMP enables an easy, convenient, and consistent interface to remote files as if they are local files.


2 Answers

Firstly, I am unaware of a native elisp ssh client (and do not imagine there is a great deal of motivation for writing one), so you will certainly need to interact with an external ssh client process.

As you wish to use ssh interactively, the ssh process requires a terminal on the local side of the connection.

The question therefore becomes: Does Emacs implement a terminal to which an ssh process can be attached?

The answer is: yes -- term.el provides a robust terminal implementation, through which ssh can be run directly, without the requirement for a shell.

If you run M-x term RET you will be prompted for a program. (It defaults to a shell, but that is certainly not the only type of process you can run.)

For reasons unknown, the interactive term (and ansi-term) functions do not support passing arguments to the specified program, which renders them less useful if you wished to run something like ssh user@host. You could instead specify a script which handled the arguments, but we can manage that in elisp as well:

The term function is actually a simple wrapper which calls make-term to start the program and then sets the appropriate modes. As make-term does accept program arguments, it is quite straightforward to copy-and-modify the definition of term to suit your own purposes.

Here is a very simple implementation:

(defun my-ssh (user host port)
  "Connect to a remote host by SSH."
  (interactive "sUser: \nsHost: \nsPort (default 22): ")
  (let* ((port (if (equal port "") "22" port))
         (switches (list host "-l" user "-p" port)))
    (set-buffer (apply 'make-term "ssh" "ssh" nil switches))
    (term-mode)
    (term-char-mode)
    (switch-to-buffer "*ssh*")))

or perhaps this is preferable:

(defun my-ssh (args)
  "Connect to a remote host by SSH."
  (interactive "sssh ")
  (let ((switches (split-string-and-unquote args)))
    (set-buffer (apply 'make-term "ssh" "ssh" nil switches))
    (term-mode)
    (term-char-mode)
    (switch-to-buffer "*ssh*")))

Obviously there is scope for improvements, but I think that's fairly useful as-is.

You should ensure that you are familiar with the quirks of term-mode. See:

  • M-: (info "(emacs) Terminal emulator") RET
  • M-: (info "(emacs) Terminal Mode") RET
  • C-hf term-mode RET
like image 135
phils Avatar answered Sep 28 '22 03:09

phils


It turns out, there is what you want:

(setq rlogin-program "ssh")
(setq rlogin-process-connection-type t)

and then M-x rlogin RET <myhost> RET will do that.

like image 34
Stefan Avatar answered Sep 28 '22 05:09

Stefan