NAME

Net::SSH2::Channel - SSH2 channel object

SYNOPSIS

  my $chan = $ssh2->channel()
    or $ssh2->die_with_error;

  $chan->exec("ls -ld /usr/local/libssh2*")
    or $ssh2->die_with_error;

  $chan->send_eof;

  while (<$chan>) {
    print "line read: $_";
  }

  print "exit status: " . $chan->exit_status . "\n";

DESCRIPTION

A channel object is created by the Net::SSH2 channel method. As well as being an object, it is also a tied filehandle.

setenv ( key, value ... )

Sets remote environment variables. Note that most servers do not allow environment variables to be freely set.

Pass in a list of keys and values with the values to set.

It returns a true value if all the given environment variables were correctly set.

blocking ( flag )

Enable or disable blocking.

Note that this is currently implemented in libssh2 by setting a per-session flag. It's equivalent to Net::SSH2::blocking.

eof

Returns true if the remote server sent an EOF.

send_eof

Sends an EOF to the remote side.

After an EOF has been sent, no more data may be sent to the remote process STDIN channel.

Note that if a PTY was requested for the channel, the EOF may be ignored by the remote server. See "pty".

close

Close the channel (happens automatically on object destruction).

wait_closed

Wait for a remote close event.

In order to avoid a bug in libssh2 this method discards any unread data queued in the channel.

exit_status

Returns the channel's program exit status.

This method blocks until the remote side closes the channel.

pty ( terminal [, modes [, width [, height ]]] )

Request a terminal on a channel.

terminal is the type of emulation (e.g. vt102, ansi, etc...).

modes are the terminal mode modifiers, for instance:

    $c->pty('vt100', { echo => 0, vintr => ord('k') });

The list of acceptable mode modifiers is available from the SSH Connection Protocol RFC (RFC4254).

If provided, width and height are the width and height in characters (defaults to 80x24); if negative their absolute values specify width and height in pixels.

pty_size ( width, height )

Request a terminal size change on a channel. width and height are the width and height in characters; if negative their absolute values specify width and height in pixels.

ext_data ( mode )

Set extended data handling mode:

normal (default)

Keep data in separate channels; STDERR is read separately.

ignore

Ignore all extended data.

merge

Merge into the regular channel.

process ( request, message )

Start a process on the channel. See also shell, exec, subsystem.

Note that only one invocation of process or any of the shortcuts shell, exec or subsystem is allowed per channel. In order to run several commands, shells or/and subsystems, a new Channel instance must be used for every one.

Alternatively, it is also possible to launch a remote shell (using shell) and simulate the user interaction printing commands to its stdin stream and reading data back from its stdout and stderr. But this approach should be avoided if possible; talking to a shell is difficult and, in general, unreliable.

shell

Start a shell on the remote host (calls process("shell")).

exec ( command )

Execute the command on the remote host (calls process("exec", command)).

Note that the given command is parsed by the remote shell; it should be properly quoted, specially when passing data from untrusted sources.

subsystem ( name )

Run subsystem on the remote host (calls process("subsystem", name)).

read ( buffer, max_size [, ext ] )

Attempts to read up to max_size bytes from the channel into buffer. If ext is true, reads from the extended data channel (STDERR).

The method returns as soon as some data is available, even if the given size has not been reached.

Returns number of bytes read or undef on failure. Note that 0 is a valid return code.

read2 ( [max_size] )

Attempts to read from both the ordinary (stdout) and the extended (stderr) channel streams.

Returns two scalars with the data read both from stdout and stderr. It returns as soon as some data is available and any of the returned values may be an empty string.

When some error happens it returns the empty list.

Example:

  my ($out, $err) = ('', '');
  while (!$channel->eof) {
      if (my ($o, $e) = $channel->read2) {
          $out .= $o;
          $err .= $e;
      }
      else {
          $ssh2->die_with_error;
      }
  }
  print "STDOUT:\n$out\nSTDERR:\n$err\n";

readline ( [ext [, eol ] ] )

Reads the next line from the selected stream (ext defaults to 0: stdout).

$/ is used as the end of line marker when eol is undef.

In list context reads and returns all the remaining lines until some read error happens or the remote side sends an eof.

Note that this method is only safe when the complementary stream (e.g. !ext) is guaranteed to not generate data or when "ext_data" has been used to discard or merge it; otherwise it may hang. This is a limitation of libssh2 that hopefully would be removed in a future release, in the meantime you are advised to use read2 instead.

getc( [ext] )

Reads and returns the next character from the selected stream.

Returns undef on error.

Note that due to some libssh2 quirks, the return value can be the empty string which may indicate an EOF condition (but not always!). See "eof".

write ( buffer )

Send the data in buffer through the channel. Returns number of bytes written, undef on failure.

In versions of this module prior to 0.57, when working in non-blocking mode, the would-block condition was signaled by returning LIBSSH2_ERROR_EAGAIN (a negative number) while leaving the session error status unset. From version 0.59, undef is returned and the session error status is set to LIBSSH2_ERROR_EAGAIN as for any other error.

In non-blocking mode, if write fails with a LIBSSH2_ERROR_EAGAIN error, no other operation must be invoked over any object in the same SSH session besides "sock" and blocking_directions.

Once the socket becomes ready again, the exact same former write call, with exactly the same arguments must be invoked.

Failing to do that would result in a corrupted SSH session. This is a limitation in libssh2.

flush ( [ ext ] )

Discards the received but still unread data on the channel; if ext is present and set, discards data on the extended channel. Returns number of bytes discarded, undef on error.

exit_signal

Returns the name of exit signal from the remote command.

In list context returns also the error message and a language tag, though as of libssh2 1.7.0, those values are always undef.

This method blocks until the remote side closes the channel.

exit_signal_number

Converts the signal name to a signal number using the local mapping (which may be different to the remote one if the operating systems differ).

window_read

Returns the number of bytes which the remote end may send without overflowing the window limit.

In list context it also returns the number of bytes that are immediately available for read and the size of the initial window.

window_write

Returns the number of bytes which may be safely written to the channel without blocking at the SSH level. In list context it also returns the size of the initial window.

Note that this method doesn't take into account the TCP connection being used under the hood. Getting a positive integer back from this method does not guarantee that such number of bytes could be written to the channel without blocking the TCP connection.

receive_window_adjust (adjustment [, force])

Adjust the channel receive window by the given adjustment bytes.

If the amount to be adjusted is less than LIBSSH2_CHANNEL_MINADJUST and force is false the adjustment amount will be queued for a later packet.

On success returns the new size of the receive window. On failure it returns undef.

SEE ALSO

Net::SSH2.