Skip to content

Commit

Permalink
sshrpc_client now working, finally
Browse files Browse the repository at this point in the history
  • Loading branch information
jj1bdx committed Mar 21, 2010
1 parent 0f91407 commit d9058c3
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

ERL = erl -boot start_clean

MODS = sshrpc_subsystem server_test client_subsystest
MODS = sshrpc_subsystem server_test client_test_nonotp \
sshrpc_client client_test

all: compile

Expand Down
67 changes: 67 additions & 0 deletions src/client_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
%% 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_test).
-export([startup/0, linkup/0, 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_config").

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

linkup() ->
{ok, Pid, Cm} = sshrpc_client:start_channel("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}
]),
io:format("Pid: ~p Cm: ~p ~n", [Pid, Cm]),
{Pid, Cm}.

test(M,N) ->
{Pid, _Cm} = linkup(),
Status = lists:map(
fun(X) ->
sshrpc_client:sync_call(Pid, 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]),
sshrpc_client:stop_channel(Pid).

%% end of file
4 changes: 2 additions & 2 deletions src/client_subsystest.erl → src/client_test_nonotp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
%% @doc example code for testing SSH RPC client site
%% TODO: this code must be OTP-nized!

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

-include("sshrpc.hrl").
Expand All @@ -27,7 +27,7 @@
%% specifyclient configuration key directory
%% (put id_rsa and id_rsa.pub)

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

startup() ->
ok = crypto:start(),
Expand Down
14 changes: 10 additions & 4 deletions src/readme.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sshrpc readme.txt
by Kenji Rikitake
kenji.rikitake@acm.org
25-FEB-2010
21-MAR-2010

This is sshrpc, an experimental module for Erlang RPC over SSH.

Expand All @@ -22,10 +22,16 @@ subsystem code: sshrpc_subsystem.erl
* client code

client code is still incomplete and under development.
client_subsystest.erl is an ad-hoc implementation;
sshrpc_client.erl will be the ssh_channel based implementation.

* NOTYET: sshrpc_client module functions
client_test_nonotp.erl: an ad-hoc implementation without OTP;
sshrpc_client.erl: OTP ssh_channel based client code implementation,
client_test.erl: example code using sshrpc_client.erl.

NOTE: R13B04 ssh_connection:send/4 has an argument parsing bug
to treat the Timeout 'infinity' atom as a Data and
crashes when trying to erlang:size([infinity]).

* sshrpc_client module functions (still experimental)

sync_call(Pid, Module, Function, Arguments) -> result | {badrpc, Reason}
synchronous function evaluation on the remote node
Expand Down
2 changes: 1 addition & 1 deletion src/server_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
%% (put id_rsa.pub, ssh_host_rsa_key{.pub}, and authorized_keys

-define(SERVER_CONFIG,
"/your/server/directory").
"/your/server_config").

startup() ->
ok = crypto:start(),
Expand Down
8 changes: 6 additions & 2 deletions src/sshrpc_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,13 @@ call(Pid, Msg, TimeOut) ->
send_command(Pid, ChannelId, Cmd) ->
Bin = term_to_binary(Cmd),
Len = size(Bin),
%% R13B04 ssh_connection:send/4 has a bug:
%% if the send(ConnectionRef, ChannelId, Data, Timeout)'s
%% Timeout = 'infinity' (an atom), this is interpreted as a Data in
%% send(ConnectionRef, ChannelId, Type, Data);
%% the atom 'infinity' removed for disambiguation and a workaround
ssh_connection:send(Pid, ChannelId,
<<?UINT32(Len), Bin/binary>>,
infinity).
<<?UINT32(Len), Bin/binary>>).

handle_reply(State, <<?UINT32(Len),Reply:Len/binary,Rest/binary>>) ->
do_handle_reply(State, Reply, Rest);
Expand Down

0 comments on commit d9058c3

Please sign in to comment.