Emacs内的远程ssh连接 [英] Remote ssh connection from within Emacs

查看:464
本文介绍了Emacs内的远程ssh连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个问题,包括,讨论有关的方面到Emacs内的ssh连接。我没有找到我的问题的答案,虽然如下:我如何从Emacs进入远程机器?



我不希望从Emacs中编辑远程机器上的文件。我知道在我的本地机器上打开一个shell的 Mx shell ,我知道使用TRAMP来编辑通过远程机器上的ssh文件。但是,这两个都不是与这个问题有关的。



(而不是投票关闭,也许将问题迁移到另一个网站。)



编辑:相关讨论 here

解决方案

首先,我不知道一个本机elisp ssh客户端(并且不要想象有很大的写一个动机),所以你一定需要与一个外部的ssh客户端进程交互。



如果你希望以交互方式使用ssh ,ssh进程需要连接本地的终端。



因此,问题是:Emacs是否实现可以附加ssh进程的终端? / p>

答案是:是 - term.el提供了一个强大的终端实现,可以直接运行ssh,而不需要shell。



如果您运行 M-x 术语 RET ,系统将提示您输入程序。 (它默认是一个shell,但这肯定不是您可以运行的唯一的进程类型。)



由于未知的原因,交互式术语(和 ansi-term )函数不支持传递参数到指定的程序,这使得它们不太有用,如果你想运行类似 ssh user @ host 。您可以指定一个处理参数的脚本,但是我们也可以在elisp中进行管理:



术语 make-term 来启动程序,然后设置适当的模式。由于 make-term 接受程序参数,所以复制和修改术语以满足您的需要。



这是一个非常简单的实现:

(interactivesUser:\) nsHost:\\\
sPort(默认22):)
(let *((端口(如果(等于端口)22端口))
(开关(列表主机-l -pport)))
(set-buffer(apply'make-termsshsshnil switches))
(term-mode)
(term-char-模式)
(switch-to-buffer* ssh *)))

或也许这是更好的:

 (defun my-ssh(args)
通过SSH到远程主机
(interactivesssh)
(let((switches(split-string-and-unquote args)))
(set-buffer (应用'make-term'sshsshnil开关))
(term-mode)
(term-char-mode)
(switch-to-buffer* ssh * )))

显然有改进的余地,但我认为这是相当有用的。



您应该确保您熟悉术语模式的怪癖。请参阅:




  • M - : (信息(emacs)终端仿真器 RET

  • M - : (info(emacs) ) RET

  • Ch f / code> RET


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.

解决方案

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

这篇关于Emacs内的远程ssh连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆