[erlang-questions] Erlang ssh shell question
Mattsson, Tommy
tommy.mattsson@REDACTED
Tue Nov 6 09:24:30 CET 2018
Hi Per and Hans,
Thank you both for your replies!
Per is correct, I want to execute "ssh <host> <cmd>" from a regular shell (bash, etc.) and have it connect to the Erlang SSH shell that I started on <host> and give me output like there was "echo test1", "echo test2" in a bash script.
I will look into if I can make use of the exec functionality for the Erlang SSH server. I looked at it earlier but it seemed to me that its intended use was for an Erlang SSH client connecting to an Erlang SSH server so I dismissed that as an option.
If all else fails I could look into the ssh_cli option and see if I can have an Erlang SSH shell handle I/O differently than the default.
Best regards,
Tommy
________________________________
From: Per Hedeland <per@REDACTED>
Sent: Friday, November 2, 2018 11:30:33 AM
To: Hans Nilsson R; Mattsson, Tommy
Cc: erlang-questions@REDACTED
Subject: Re: [erlang-questions] Erlang ssh shell question
Hi Tommy! (and Hans:-)
Hans, I *think* you are misunderstanding Tommy's question. More
speculation below, since I haven't actually verified how ssh(3)
works... When using e.g. the OpenSSH client,
$ ssh <host>
makes a "shell" request per
https://tools.ietf.org/html/rfc4254#section-6.5 , while
$ ssh <host> <cmd>
makes an "exec" request. I think Tommy is happy with how "shell" works,
but you seem to be changing it to a pretty boring implementation.:-) And
I think Tommy's question is about "exec" not associating 'standard_io'
with the SSH channel - thus if you use "exec" to call a function that
prints something, the output is lost.
I'm not sure there is anything "wrong" with that though - for an SSH
server running "directly" on *nix, it is obvious that "exec" should
associate stdio with the SSH channel, since <cmd> is *nix shell command,
and it's pretty fundamental that you want any output (and exit code)
that it produces. But for the Erlang/OTP SSH server, <cmd> is
(apparently) an Erlang function call, and sending only what the call
returns (in text form) back through the channel is not unreasonable per
se.
But maybe there could be an option to ssh:daemon() to make it do the
same thing with 'standard_io' for "exec" as it does for "shell" - I
can't see one in the man page. And I can't think of a way that the
called function could set it up. But I assume that it's possible to
do-it-all yourself via the 'ssh_cli' option.
--Per
On 2018-11-02 10:37, Hans Nilsson R wrote:
> Hi,
>
> Try
>
> 5> ssh:daemon(1234, [{system_dir,...}, {shell,fun(Usr,Hst) -> spawn(fun() -> io:format("Hello ~p ~p~n",[Usr,Hst]), L1 = io:get_line("&& "), io:format("Got ~p~n", [L1]) end) end}]).
> {ok,<0.110.0>}
> 6>
>
> Sorry for the one-liner. The fun() a bit edited, I hope I got it right:
>
> fun(Usr,Hst) ->
> spawn(fun() ->
> io:format("Hello ~p ~p~n",[Usr,Hst]),
> L1 = io:get_line("&& "),
> io:format("Got ~p~n", [L1])
> end)
> end
>
>
> Note the option shell and the spawn, it is essential.
> You don't need the subsystem option. The user_dir option defaults to the $HOME/.ssh of the user that started erl.
>
> Now in bash:
>
> ~$ ssh -p 1234 localhost
> Hello "USERNAME" {{127,0,0,1},60088}
> && some command
> Got "some command\n"
> Connection to localhost closed.
> ~$
>
> Hope this helps.
> /Hans
>
>
>
> On 11/2/18 10:05 AM, Mattsson, Tommy wrote:
>> Hi,
>>
>> First time writer here on the erlang questions list :)
>>
>>
>> I am wondering if anyone knows about how the erlang ssh handles I/O? More details can be found below.
>>
>>
>> Erlang version: 21.0
>>
>>
>>
>> Start of SSH server on testing (Windows) machine:
>>
>> application:ensure_all_started(ssh),
>> Options = [{system_dir, filename:join(SSHPath, "daemon")}, {user_dir, ?DIR}, {subsystems, [ssh_sftpd:subsystem_spec([{cwd, SSHPath}])]}],
>>
>> ssh:daemon(?ipaddr, ?port, Options]).
>>
>> Test module:
>> -module(sshtest).
>> sshIO() ->
>> io:format("test1~n"),
>> io:format("test2\n"),
>> test3.
>>
>>
>> Test run from my own machine:
>>> ssh $IPADDR 'sshtest:sshIO().'
>> test3
>>
>>
>> For some reason any io:format/io:fwrite does not travel back over the SSH connection. If I connect to some Linux machine that already has a (non-erlang) SSH server running and I try to run some random bash script then any echo I have in the script will travel back over the SSH connection.
>>
>> A test I did with a bash script towards a Linux server,
>> Bash script (~/test.sh):
>>
>> echo "test1"
>>
>> echo "test2"
>>
>> echo "test3"
>>
>>
>> Test run from my own machine:
>>> ssh $IPADDR ./test.sh
>>
>> test1
>>
>> test2
>>
>> test3
>>
>> This is how I'd like it to work.
>>
>>
>> I have been googling and reading the documentation for the ssh and related modules to no avail in regards to finding a solution to this problem.
>>
>> Other than this I/O problem the ssh connection works perfectly for me =
>>
>> Hopefully someone here on the list has some insight into how this works.
>>
>> Thankful for any help I can get :)
>> Best regards,
>>
>> Tommy
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20181106/39e30c19/attachment.htm>
More information about the erlang-questions
mailing list