sshkit v0.0.1 SSHKit.SSH.Channel

Summary

Functions

Closes an SSH channel

Sends an EOF message on an open SSH channel

Executes a command on the remote host

Loops over channel messages until the channel is closed

Opens a channel on an SSH connection

Receive the next message on an open SSH channel

Sends data across an open SSH channel

Functions

close(channel)

Closes an SSH channel.

Returns :ok.

For more details, see :ssh_connection.close/2.

eof(channel)

Sends an EOF message on an open SSH channel.

Returns :ok or {:error, :closed}.

For more details, see :ssh_connection.send_eof/2.

exec(channel, command, timeout \\ :infinity)

Executes a command on the remote host.

Returns :success, :failure or {:error, reason}.

For more details, see :ssh_connection.exec/4.

Processing channel messages

loop/4 may be used to process any channel messages received as a result of executing command on the remote.

loop(channel, timeout \\ :infinity, state \\ nil, fun)

Loops over channel messages until the channel is closed.

Invokes fun for each channel message, passing the channel, message and state as arguments. fun’s return value is stored in state.

timeout specifies the maximum delay between two subsequent messages.

Returns state after the channel is closed, or {:error, :timeout}.

open(connection, options \\ [])

Opens a channel on an SSH connection.

On success, returns {:ok, channel}, where channel is a Channel struct. Returns {:error, reason} if a failure occurs.

For more details, see :ssh_connection.session_channel/4.

Options

  • :type - the type of the channel, defaults to :session
  • :timeout - defaults to :infinity
  • :initial_window_size - defaults to 128 KiB
  • :max_packet_size - defaults to 32 KiB
recv(channel, timeout \\ :infinity)

Receive the next message on an open SSH channel.

Returns {:ok, message} or {:error, :timeout}.

For more details, see :ssh_connection.

Messages

The message tuples returned by recv/3 correspond to the underlying Erlang channel messages with the channel id stripped. recv only listens to messages from the channel specified as the first argument.

  • {:data, type, data} - data has arrived, type is 0 “normal” or 1 “stderr”
  • {:eof} - indicates that no more data is to be sent by the remote process
  • {:exit_signal, signal, msg, lang} - remote execution terminated by signal
  • {:exit_status, status} - remote command terminated with exit code status
  • {:closed} - indicates that the channel has now been shut down
send(channel, type \\ 0, data, timeout \\ :infinity)

Sends data across an open SSH channel.

Returns :ok, {:error, :timeout} or {:error, :closed}.

For more details, see :ssh_connection.send/5.