aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ssh/examples/ssh_device.erl
diff options
context:
space:
mode:
authorLinus.yuan <[email protected]>2015-01-14 23:19:03 +0800
committerZandra Hird <[email protected]>2015-03-06 12:47:14 +0100
commit6da38730c44a1fc39d651d476b8a23e2cf62c8ea (patch)
treee52c0ce1f3416137f5a67048cea27fa5ffabaa50 /lib/ssh/examples/ssh_device.erl
parentc312d2eb264d0f97e82f6fc5f8adfc35f2d9d5e7 (diff)
downloadotp-6da38730c44a1fc39d651d476b8a23e2cf62c8ea.tar.gz
otp-6da38730c44a1fc39d651d476b8a23e2cf62c8ea.tar.bz2
otp-6da38730c44a1fc39d651d476b8a23e2cf62c8ea.zip
Add client example for using the SSH module
Diffstat (limited to 'lib/ssh/examples/ssh_device.erl')
-rw-r--r--lib/ssh/examples/ssh_device.erl62
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/ssh/examples/ssh_device.erl b/lib/ssh/examples/ssh_device.erl
new file mode 100644
index 0000000000..f6be812915
--- /dev/null
+++ b/lib/ssh/examples/ssh_device.erl
@@ -0,0 +1,62 @@
+%%
+%% %CopyrightBegin%
+%%
+%% Copyright Ericsson AB 2005-2015. 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.
+%%
+%% %CopyrightEnd%
+%%
+
+-module(ssh_device).
+
+%% api
+-export([ssh_device/5]).
+
+%%% I wrote this because of i think a fully ssh client sample will be easy to start the ssh module better than
+%%% go though each function file.
+ssh_device(Host, Port, User, Pass, Cmd) ->
+ ssh:start(),
+ case ssh:connect(Host, Port,
+ [{user, User}, {password, Pass},
+ {silently_accept_hosts, true}, {quiet_mode, true}])
+ of
+ {ok, Conn} ->
+ {ok, ChannelId} = ssh_connection:session_channel(Conn,
+ infinity),
+ ssh_connection:exec(Conn, ChannelId, Cmd, infinity),
+ Init_rep = <<>>,
+ wait_for_response(Conn, Host, Init_rep),
+ ssh:close(Conn);
+ {error, nxdomain} ->
+ {error,nxdomain}
+ end.
+
+%%--------------------------------------------------------------------
+%%% Internal application API
+%%--------------------------------------------------------------------
+wait_for_response(Conn, Host, Acc) ->
+ receive
+ {ssh_cm, Conn, Msg} ->
+ case Msg of
+ {closed, _ChannelId} ->
+ {ok,Acc};
+ {data, _, _, A} ->
+ Acc2 = <<Acc/binary, A/binary>>,
+ wait_for_response(Conn, Host, Acc2);
+ _ ->
+ wait_for_response(Conn, Host, Acc)
+ end
+ after
+ 5000 ->
+ {error,timeout}
+ end.