[erlang-questions] Erlang ssh shell question

Hans Nilsson R hans.r.nilsson@REDACTED
Fri Nov 2 10:37:00 CET 2018


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
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4161 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20181102/7597d20a/attachment.bin>


More information about the erlang-questions mailing list