Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 76d4aa58b2
Fetching contributors…

Cannot retrieve contributors at this time

file 136 lines (115 sloc) 4.413 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
%% Copyright (c) 2009-2010 Kenji Rikitake. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.

%% @author Kenji Rikitake <kenji.rikitake@acm.org>
%% @copyright 2009-2010 Kenji Rikitake

%% @doc example code for testing SSH RPC client site
%% TODO: this code must be OTP-nized!

-module(client_subsystest).
-export([startup/0, linkup/0, doit/2, test/2]).

-include("sshrpc.hrl").

-define(TIMEOUT, 30000). % in milliseconds

%% specifyclient configuration key directory
%% (put id_rsa and id_rsa.pub)

-define(CLIENT_CONFIG, "/your/client/directory").

startup() ->
    ok = crypto:start(),
    ok = ssh:start().

linkup() ->
    {ok, Pid} = ssh:connect("127.0.0.1", % server address
11122, % port number
[
%% note: private user key (id_rsa) and
%% public host key (known_hosts) must exist
%% in the "user_dir" for public-key auth
%% NOTE WELL: user key has NULL password protected
%% (password-protected is UNSUPPORTED)
{user_dir, ?CLIENT_CONFIG},
%% the following user/password pair needed for
%% plain password-based authentication
%% {user,"test"}
%% {password,"shiken"}
%%
%% nodelay must be set true for immediate response!
{nodelay, true}
]),
    {ok, Chanid} = ssh_connection:session_channel(Pid, infinity),
    io:format("Pid: ~p Chanid: ~p ~n", [Pid, Chanid]),
    Status = ssh_connection:subsystem(Pid, Chanid, "sshrpc@k2r.org", ?TIMEOUT),
    io:format("subsystem connection status: ~p ~n", [Status]),
    {Pid, Chanid}.

doit({Pid, Chanid}, Command) ->
    Bin = term_to_binary(Command),
    Len = size(Bin),
    ssh_connection:send(Pid, Chanid, 0, <<?UINT32(Len), Bin/binary>>, ?TIMEOUT),
    ssh_receive(Pid, Chanid).

ssh_receive(CM, Channel) ->
    ssh_loop(CM, Channel, <<>>).

-define(RWINSIZE, 4096).

%% SSH client loop: this hasn't been OTP-nized yet.

ssh_loop(CM, Channel, Buf) ->
    receive
stop ->
            % Closing channel
ssh_connection:close(CM, Channel);

{ssh_cm, CM, {data, Channel, 0, Data}} ->
% looks like this is a must for flow control...
ssh_connection:adjust_window(CM, Channel, size(Data)),
State = handle_reply(<<Buf/binary, Data/binary>>),
case State of
{moredata, _, Data1} ->
ssh_loop(CM, Channel, Data1);
{success, Term, Data2} ->
case Term of
{answer, {Reply, Status}} ->
io:format("Answer: Reply: ~p Status: ~p~n",
[Reply, Status]),
case Data2 of
<<>> -> ok;
Other -> ssh_loop(CM, Channel, Other)
end;
Other ->
io:format("[~p] Unknown Term returned: ~p~n", [?MODULE, Other])
end;
Other ->
io:format("[~p] Unknown State returned: ~p~n", [?MODULE, Other])
end;

{ssh_cm, CM, {closed, Channel}} ->
io:format("detached: ~p~n", [Channel]),
            % Closing channel
io:format("close channel: ~p~n", [Channel]),
ssh_connection:close(CM, Channel);

{ssh_cm, CM, {eof, Channel}} ->
io:format("eof: ~p~n", [Channel]);

E ->
io:format("[~p] Received: ~p~n", [?MODULE, E]),
ssh_loop(CM, Channel, Buf)
    after 1000 ->
io:format("[~p] Timed Out 1sec~n", [?MODULE])
    end.

handle_reply(<<?UINT32(Len), Msg:Len/binary, Rest/binary>>) ->
    %io:format("handle_reply: Len: ~p Msg: ~p Rest: ~p~n", [Len, Msg, Rest]),
    {success, binary_to_term(Msg), Rest};
handle_reply(Data) ->
    %io:format("handle_reply: Data: ~p~n", [Data]),
    {moredata, {}, Data}.

test(M,N) ->
    L = linkup(),
    Status = lists:map(fun(X) ->
doit(L, {mfa, {lists, seq, [1, M]}}),
io:format("NR: ~p Time: ~p~n", [X, erlang:now()]) end,
lists:seq(1,N)),
    io:format("Time: ~p Status: ~p~n", [erlang:now(),Status]),
    {Pid, Chanid} = L,
    ssh_connection:close(Pid, Chanid),
    ssh_loop(Pid, Chanid, <<>>).

%% end of file
Something went wrong with that request. Please try again.