How to create a custom ssh shell in elixir?

Viewed 163

I am trying to create a custom SSH shell in Elixir using the Erlang :ssh library so that I can SSH into my application with a restricted set of options/commands available. I manage to log in successfully (using passwords is fine in this case), but then I can neither send anything to the client nor receive anything from the client (at least I can't figure out how).

I have read the :ssh library's documentation and looked at the esshd library as an example, but couldn't find the information I needed, and the library did not work for me. Please see attached example code.

def init(_opts) do
  Logger.info("--> Starting Communications.SSHD process")

  # start the erlang library
  :ssh.start()
  # config the server
  {ok, sshd2} = :ssh.daemon(10022, 
                          shell: &on_shell/2,
                          system_dir: './priv/daemon', 
                          user_passwords: [{'test', 'testpass'}], 
                          user_dir: './') 

  # link the ssh daemon pid to ourselves
  Process.link sshd2

  {:ok, []}
end

def on_shell(username, {ip, port} = peer_address) do
  Logger.info("on_shell() Called")
  # spawn a thread to handle the shell
  spawn(__MODULE__, handle_shell, [])
end

# handle the shell commands
def handle_shell() do
  Logger.info("Shell started #{inspect Process.group_leader}")
  _ = :io.setopts(Process.group_leader, binary: true, encoding: :unicode)  

  # this never appears on client or server console

  IO.puts "Interactive example SSH shell - type exit ENTER to quit"
end

Any ideas?

0 Answers
Related