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

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

问题描述

几个问题,包括这个,讨论相关方面从 Emacs 中 ssh 连接.不过,我还没有找到问题的答案:如何从 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?

我不想在 Emacs 中编辑远程机器上的文件.我知道 M-x shell 它在我的本地机器上打开一个 shell,我知道使用 TRAMP 在远程机器上通过 ssh 编辑文件.然而,这些都与这个问题无关.

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.)

相关讨论此处.

推荐答案

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

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.

如果您希望以交互方式使用 ssh,那么 ssh 进程需要在连接的本地端有一个终端.

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

因此问题变成:Emacs 是否实现了可以附加 ssh 进程的终端?

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

答案是:是的——term.el 提供了一个健壮的终端实现,通过它可以直接运行 ssh,不需要 shell.

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

如果你运行 M-x term RET 你会被提示输入一个程序.(它默认为 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.)

由于未知的原因,交互式 term(和 ansi-term)函数不支持将参数传递给指定的程序,如果您愿意,这会使它们变得不太有用运行类似 ssh user@host 的东西.您可以改为指定一个处理参数的脚本,但我们也可以在 elisp 中进行管理:

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:

term 函数实际上是一个简单的包装器,它调用 make-term 来启动程序,然后设置适当的模式.由于 make-term 确实 接受程序参数,因此复制和修改 term 的定义以适合您自己的目的非常简单.

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: 
sHost: 
sPort (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.

你应该确保你熟悉term-mode的怪癖.见:

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
  • M-: (info "(emacs) Terminal emulator") RET
  • M-: (info "(emacs) Terminal Mode") RET
  • C-hf term-mode RET

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

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