-
-
Save dch/0d4a52ab4c29bb5496084a652c61545d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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