この文章では、Emacs 上のアプリケーションから SSH を経由してネットワークにアクセスする方法を述べています。無保証です。猛犬注意。濡れていて滑ります。
ほとんどの部分は 上野さんが公開していた Emacs with SSH からのパクりです。
open-network-stream の代替関数を用意するために、 まずは以下の関数を考える。
(defun open-ssh-stream-internal (&rest; plist)
(let ((name (or (plist-get plist :name) name))
(buffer (or (plist-get plist :buffer) buffer))
(host (or (plist-get plist :host) host))
(service (or (plist-get plist :service) service))
(process-connection-type nil))
(start-process-shell-command
name buffer
(or (plist-get plist :ssh) "ssh")
(or (plist-get plist :sshflags) "-Caxq")
(plist-get plist :relay)
(or (plist-get plist :command) "nc -w 60")
host (format "%s" service))))
上の関数 open-ssh-stream-internal はそのままでは使えない。 経由するホスト (relay-host) ごとに open-network-stream の代替になる関数を簡単に定義するためのものだ。 relay-host を judy.example.com として、 関数 open-ssh-stream-judy を定義するには次のようにする。
(defun open-ssh-stream-judy (name buffer host service)
(open-ssh-stream-internal :relay "judy.example.com"
:command "nc"
:name name
:buffer buffer
:host host
:service service))
この関数 open-ssh-stream-judy を利用するためには、 relay-host (judy.example.com) 上に netcat がインストールされており、 relay-host (judy.example.com) にパスフレーズ入力なしでログイン可能である必要がある。 パスフレーズ無しの専用鍵を用意しても、ssh-agent を使用してもよい。
ここまでで定義した関数を、各アプリケーションで open-network-stream の代わりに利用すればよい。 ここでは、アプリケーション毎に具体的な設定について述べる。
riece-server-alist での定義に :function を指定する。 :host は relay-host から見たホスト名であることは言うまでもない。
(add-to-list 'riece-server-alist
'("judy" :service 6667 :host "localhost"
:function open-ssh-stream-judy))
elmo-network-stream-type-alist に SSH 用の設定を加える。
(add-hook 'wl-init-hook
(lambda ()
(add-to-list 'elmo-network-stream-type-alist
'("!judy" judy nil open-ssh-stream-judy))))
必要ならば elmo-imap4-default-stream-type の設定も行う。 default-stream-type を変更した場合に open-network-stream を使いたい場合は、 フォルダ指定時に !direct を使うと便利である。
(setq elmo-imap4-default-stream-type 'judy)
POP の場合も同様に elmo-pop3-default-stream-type を変更する。
(setq elmo-pop3-default-stream-type 'judy)
smtp-open-connection-function を設定する。
(setq smtp-open-connection-function #'open-ssh-stream-judy)
QMTP を利用したい場合には、同様に qmtp-open-connection-function を設定する。
(setq qmtp-open-connection-function #'open-ssh-stream-judy)
navi2ch-open-network-stream-function を設定する。
(setq navi2ch-open-network-stream-function #'open-ssh-stream-judy)
他のアプリケーションで SSH を使うつもりがまったくなく、 navi2ch でだけ SSH を使いたい。 そんな場合には、自前で用意した代替関数 (open-ssh-stream-judy) の代わりに navi2ch-open-network-stream-via-command を使ってもよい。
(setq navi2ch-open-network-stream-function #'navi2ch-open-network-stream-via-command)
(setq navi2ch-open-network-stream-command
(lambda (host service)
(list "ssh" "-ax" "judy.example.com"
"nc" (format "%s" host)
(format "%s" service))))
上野乃毅さんの Emacs with SSH がなければ、そもそもこの文書は存在していないでしょう。 大部分のヒントをいただいたことに感謝します。
Copyright © 2004 TAKAHASHI Kaoru