erlang 接入遠程shell控制檯

erlang shell是用戶與 erlang 運行時系統交互的界面程序。事實上,erlang VM的運行不依賴任何shell,只要在啓動的時候添加參數detached就可以脫離終端。
-detached
Starts the Erlang runtime system detached from the system console. Useful for running daemons and backgrounds processes. Implies -noinput.
實際上,detached 等效於noshell 加上 noinput。
# erl -detached -emu_args
Executing: /home/erl/lib/erlang/erts-5.10.3/bin/beam /home/erl/lib/erlang/erts-5.10.3/bin/beam -- -root /home/erl/lib/erlang -progname erl -- -home /root -- -noshell -noinput
另外,需要注意的是,windows不直接支持detached,而是以服務的形式在後臺運行,見 erlsrv

現在討論erlang 接入遠程shell控制檯的幾種方式。

作業(JCL )模式 

在 Erlang shell 中按下^G鍵,就可以看到作業控制模式(JCL mode)的菜單。在菜單中,有個選項能讓我們連接到一個遠程 shell。
先以detached運行一個節點1:
# erl -name [email protected] -setcookie 123456 -detached
檢查這個erlang進程是否運行
# ps -ef | grep beam
root 20672 1 0 01:32 ? 00:00:00 /home/erl/lib/erlang/erts-5.10.3/bin/beam -- -root /home/erl/lib/erlang -progname erl -- -home /root -- -name [email protected] -setcookie 123456 -noshell -noinput 
啓動另外一個節點2,並接入到節點1
# erl -name [email protected] -setcookie 123456 
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false] 
Eshell V5.10.3 (abort with ^G) 
([email protected])1> 
User switch command 
 --> h 
   c  [nn]                       - connect to job 
   i   [nn]                       - interrupt job 
   k  [nn]                       - kill job 
   j                                 - list all jobs 
   s [shell]                    - start local shell 
   r [node [shell]]        - start remote shell 
   q                               - quit erlang 
   ? | h                          - this message 
 --> r '[email protected]
 --> j 
  1 {shell,start,[init]} 
  2* {'[email protected]',shell,start,[]} 
 --> c 2 
Eshell V5.10.3 (abort with ^G) 
([email protected])1>
注意了,windows下要使用werl

連接到遠程 shell 後,所有的終端輸入解析操作都由本地 shell 完成,不過求值的工作是在遠 程完成的。遠程求值的結果輸出全部被轉發給本地 shell。  
 
要退出 shell, 按^G回到 JCL 模式。 終端輸入解析操作是在本地進行的, 因此通過^G q 的方式退出 shell  是安全的。
Eshell V5.10.3 (abort with ^G) 
([email protected])1> 
User switch command 
--> q


Remsh 模式

Remsh和 JCL 模式很類似,但是調用方式不同的機制。使用這種機制,JCL 模式的所有 操作步驟都可以被繞過,只需像下面這樣啓動 shell,對於長名字:

-remsh Node
Starts Erlang with a remote shell connected to Node.

以下方式啓動節點2,將直接接入節點1控制檯:
# erl -name [email protected] -setcookie 123456 -remsh [email protected]
Erlang R16B02 (erts-5.10.3) [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.3  (abort with ^G)
([email protected])1> node().
這種方式和JCL很相像,本地也會啓動一個erlang節點用於接入遠程shell


SSH 模式

erlang自帶了SSH的功能,我們可以很方便的開啓SSH服務,對外提供遠程 shell服務。 SSH的使用需要開啓crypto,如果erlang顯示以下錯誤,可以參考這篇文章
1> crypto:start().  
** exception error: undefined function crypto:start/0
要使用該功能,通常需要先準備好具有遠程訪問 SSH 權限的 key,不過這裏爲了快速測試,可以這樣做:
節點1啓動ssh服務:
Eshell V5.10.3  (abort with ^G)
([email protected])1> ssh:start().
ok
([email protected])2> ssh:daemon(8888, [{password, "12345"}]).
{ok,<0.57.0>}
本地不需要啓動erlang節點,直接使用ssh連接即可,輸入以上設置的密碼,就可以接入節點1的shell控制檯。
# ssh -p 8888 [email protected]
The authenticity of host '[127.0.0.1]:8888 ([127.0.0.1]:8888)' can't be established.
RSA key fingerprint is ad:03:b4:6b:df:51:97:23:dc:47:cb:75:85:15:44:89.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:8888' (RSA) to the list of known hosts.
[email protected]'s password:
Eshell V5.10.3  (abort with ^G)
這種方式,erlang shell所有操作都是在遠程節點完成的。


管道(pipe)模式

在使用管道(pipe)連接到一個Erlang節點時,和SSH一樣不需要啓動本地erlang節點。這種方法很少用,每次輸出時都調用fsync,如果輸出過多時,會有很大的性能損失。

具體做法爲:用 run_erl 啓動 erlang,相當於把 erlang 進程包在一個管道中:
# mkdir /tmp/erl_log
# cd /home/erl/bin
# ./run_erl -daemon /tmp/erl_pipe /tmp/erl_log "erl -name [email protected] -setcookie 123456"
其中,daemon 表示以後臺進程運行,/tmp/erl_pipe是管道文件的名稱,/tmp/erl_log指定了日誌保存文件夾
然後使用 to_erl 程序來連接節點: 
# ./to_erl /tmp/erl_pipe
Attaching to /tmp/erl_pipe (^D to exit) 
([email protected])1> node(). 
'[email protected]'

參考:http://blog.csdn.net/mycwq/article/details/43850735
https://s3.amazonaws.com/erlang-in-anger/text.v1.0.3.pdf
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章