1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2005-2015. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20 21-module(ssh_device). 22 23%% api 24-export([ssh_device/5]). 25 26%%% I wrote this because of i think a fully ssh client sample will be easy to start the ssh module better than 27%%% go though each function file. 28ssh_device(Host, Port, User, Pass, Cmd) -> 29 ssh:start(), 30 case ssh:connect(Host, Port, 31 [{user, User}, {password, Pass}, 32 {silently_accept_hosts, true}, {quiet_mode, true}]) 33 of 34 {ok, Conn} -> 35 {ok, ChannelId} = ssh_connection:session_channel(Conn, 36 infinity), 37 ssh_connection:exec(Conn, ChannelId, Cmd, infinity), 38 Init_rep = <<>>, 39 wait_for_response(Conn, Host, Init_rep), 40 ssh:close(Conn); 41 {error, nxdomain} -> 42 {error,nxdomain} 43 end. 44 45%%-------------------------------------------------------------------- 46%%% Internal application API 47%%-------------------------------------------------------------------- 48wait_for_response(Conn, Host, Acc) -> 49 receive 50 {ssh_cm, Conn, Msg} -> 51 case Msg of 52 {closed, _ChannelId} -> 53 {ok,Acc}; 54 {data, _, _, A} -> 55 Acc2 = <<Acc/binary, A/binary>>, 56 wait_for_response(Conn, Host, Acc2); 57 _ -> 58 wait_for_response(Conn, Host, Acc) 59 end 60 after 61 5000 -> 62 {error,timeout} 63 end. 64