SSH in Eshell

(defvar config-remote-alist nil
  "A list for remote hosts.

'((name :username username
        :host hostaddres
        :port port
        :program prog))")

(defun eshell/tramp (arg &optional dir prog)
  "An simple TRAMP warpper based on `eshell/cd'.

ARG should be user@remote-machine[#port] or a list in
`config-remote-alist'.
DIR is the directory to open, default \"~\".
PROG is the program used to fetch file, default to \"ssh\"."
  (let ((data (cdr (assoc (intern arg) config-remote-alist))))
    (if data
        (eshell/cd
         (concat
          (format "/%s:%s@%s"
                  (or prog (getf data :program) "ssh")
                  (getf data :username)
                  (getf data :host))
          (if (getf data :port)
              (format "#%d" (getf data :port)))
          (if dir
              (format ":%s" dir)
            ":~")))
      (eshell/cd (concat (format "/%s:%s" (or prog "ssh") arg)
                         (if dir
                             (format ":%s" dir)
                           ":~"))))))

(defun eshell/ssh (arg &optional dir)
  "A simple ssh emulation based on `eshell/tramp'."
  (eshell/tramp arg dir "ssh"))

利用 eshell/cdTramp 实现的 eshell/ssh,除了更加顺手以外还能通过设置 config-remote-alist 利用简写创建远程连接。

4 个赞