Skip to content

Instantly share code, notes, and snippets.

@dch dch/openssh.erl forked from maxlapshin/openssh.erl
Created Feb 27, 2018

Embed
What would you like to do?
#!/usr/bin/env escript
%%
%%
-mode(compile).
-compile(export_all).
main([Port]) ->
{ok, _} = application:ensure_all_started(ssh),
{ok, Sshd} = ssh:daemon(list_to_integer(Port), [{system_dir, "/Users/max/Sites/flussonic"},
{user_dir, "/Users/max/.ssh"}, {shell, {?MODULE, start, []}}]),
erlang:monitor(process, Sshd),
receive
_ -> ok
end,
ok.
start() ->
proc_lib:spawn(?MODULE, init, []).
-record(shell, {
leader,
leader_ref,
port
}).
init() ->
io:fwrite(standard_error, "Shell started\n", []),
Leader = group_leader(),
Ref = erlang:monitor(process, Leader),
Port = erlang:open_port({spawn_executable, "/bin/bash"}, [{args,["-i"]}, binary, exit_status, stderr_to_stdout, eof]),
Leader ! {io_request,self(),Ref,{get_line,latin1,""}},
gen_server:enter_loop(?MODULE, [], #shell{leader = Leader, leader_ref = Ref, port = Port}).
handle_info({io_reply, _, {error, Error}}, #shell{port = Port} = Shell) ->
erlang:port_close(Port),
io:fwrite(standard_error, "ending: ~p\n", [Error]),
{stop, normal, Shell};
handle_info({io_reply, Ref, Data}, #shell{leader_ref = Ref, leader = Leader, port = Port} = Shell) ->
erlang:port_command(Port, Data),
Leader ! {io_request,self(),Ref,{get_line,latin1,""}},
{noreply, Shell};
handle_info({Port, {data, Data}}, #shell{leader = Leader, port = Port} = Shell) ->
Leader ! {io_request,self(),make_ref(),{put_chars,latin1,Data}},
{noreply, Shell};
handle_info({io_reply, _, ok}, #shell{} = Shell) ->
{noreply, Shell};
handle_info(Msg, #shell{} = Shell) ->
io:fwrite(standard_error, "~p\n", [Msg]),
{noreply, Shell}.
terminate(_,_) ->
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.