Home > study > 用 SSH 来连接 Erlang Shell

用 SSH 来连接 Erlang Shell

June 6th, 2008 :: jackyz

Erlang 的 SSH 支持 “很好很强大” ——可以让 Erlang 在 22 (或其他指定的)端口启动 Erlang 自己的 sshd ,然后,当你通过 Putty 之类的 ssh 客户端连上去时,就能直接得到 Erlang Shell 。很酷吧,更酷的是,这个功能完备的 Erlang Shell 已经支持了 Public-Key Authorization 以及 “One Password Rule Them All” 的 “超级密码” Authorization 。尤其对于那些需要伺候集群中 N 多台 Erlang 服务器的 “ IT 农场工人” ,这会是一个极度 “系统管理员友好” 的功能。那么,具体来说,整个步骤又是怎么样的呢?

下面是我们的前方记者从 “不明地带” 发回的 —— 现场 “流水帐” :D

第一回

启动:

1> ssh_sshd:listen(9999).
{
ok,<0.38.0>}

连接:

$ ssh -o Port=9999 localhost

得到:

2>
=
ERROR REPORT==== 6-Jun-2008::10:39:26 ===
Error in process <0.39.0> with exit value: {badarg,[{ets,lookup,[crypto_server_table,port]},{crypto,control,2},{crypto,mod_exp,3},{crypto,mod_exp,3},{ssh_transport,dh_gen_key,3},{ssh_transport,server_kex,3},{ssh_transport,kex_negotiate,8}]}
...

看来和一个 crypto 啥的有关系。关门,放 google ……。原来 ssh 的加密体系依赖于 crypto app 需要先启动它。

第二回

启动:

1> crypto:start().
ok
2> ssh_sshd:listen(9999).
{
ok,<0.41.0>}

连接:

$ ssh -o Port=9999 localhost

得到:

3>
=
ERROR REPORT==== 6-Jun-2008::10:47:12 ===
**
Generic server <0.45.0> terminating
**
Last message in was {'EXIT',<0.42.0>,{error,eacces}}
...

这个 eacces 貌似很可疑,关门,再放 google ……。原来 sshd 默认会访问 /etc/ssh 以获取 “host files that identifies the host for ssh” ,而 /etc/ssh 只有 root 能访问,当然是会失败的。必须加上一个 system_dir 参数指定一个新位置。

第三回

启动:

1> crypto:start().
ok
2> ssh_sshd:listen(9999, [{system_dir, "."}]).
{
ok,<0.41.0>}

连接:

$ ssh -o Port=9999 localhost

得到:

3>
=
ERROR REPORT==== 6-Jun-2008::10:57:10 ===
**
Generic server <0.45.0> terminating
**
Last message in was {'EXIT',<0.42.0>,{error,enoent}}
...

enoent,想起来 “host files that identifies the host for ssh” 在 . 目录下根本没有,需要生成之。关门,又放 google ……。原来要如此这般的生成 ssh host 文件:

ssh-keygen -f ssh_host_rsa_key -t rsa -N ''
ssh-keygen -f ssh_host_dsa_key -t dsa -N ''

得到了 4 个 ssh_host_xxx 文件,分别对应 rsa 和 dsa 加密算法。

第四回

启动:

1> crypto:start().
ok
2> ssh_sshd:listen(9999, [{system_dir, "."}]).
{
ok,<0.41.0>}

连接:

$ ssh -o Port=9999 localhost

得到:

The authenticity of host '[localhost]:9999 ([127.0.0.1]:9999)' can't be established.
DSA key fingerprint is 48:71:fa:f7:2b:b1:99:af:10:c7:ea:67:48:57:fa:68.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:9999' (DSA) to the list of known hosts.
Eshell V5.5.5  (abort with ^G)
1>

BINGO! 搞定!

延伸问题

上述方式启动的 ssh_sshd 仍然存在问题——当 q(). 退出 erlang shell in ssh 时,会让 ssh_sshd 退出,但同时也会报错。why?how to fix?

上述方式启动的 sshd 登录时,如何采用 public key 或者用户名/密码验证?

有兴趣了解的同学,自己做一下实验吧。

BTW. ssh application 目前正在 refactor 之中,有 OTP team 的 mail 为证:

Hi!

Thank you for reporting this and other issues in previous mails. We are in the process of refactoring the ssh-application and there is much room for improvement of the documentation, we will look over this.

Regards Ingela – Erlang/OTP, Ericsson

主要的 refactor 方向是(基于情报进行合理的大胆假设):“更优雅的整和 ssh_sshd 和 ssh_sftpd 系统” 以及更 “customize 友好的架构” ,比如:ssh/sftp 连上去的不再是 erlang shell/real file system 而是一个 “定制的环境” 。ssh as a protocol (like http)? sounds great!

study

  1. cheng
    June 6th, 2008 at 11:51 | #1

    片刻之间,楼主的G放出来了N次。呵呵。
    谢了,期待erlang的ssh更完善。。。

  2. KrzyCube
    June 6th, 2008 at 16:49 | #2

    放google的技术炉火纯青了

  3. cbkid
    June 7th, 2008 at 07:51 | #3

    没有google.估计大多数人都没法活了。哈哈,

  4. dogstar
    June 11th, 2008 at 16:59 | #4

    确实,没google,估计开发人员就只能抱着书慢慢翻啦.大家都背代码,看谁背的多.

  5. cavaluo
    June 21st, 2008 at 06:10 | #5

    呵呵,俺也耍耍.

  6. ziseyiran
    June 26th, 2008 at 15:04 | #6

    我水平太差不知怎么玩

  7. crackcell
    October 11th, 2009 at 02:30 | #7

    挺期待后续版本的完善……

  8. October 12th, 2009 at 12:15 | #8

    新版本的ssh改进很多.

    http://yufeng.info

  1. No trackbacks yet.