NAME

Net::LibSSH::Channel - SSH exec channel for Net::LibSSH

VERSION

version 0.005

SYNOPSIS

my $ch = $ssh->channel;

$ch->exec('uname -r') or die "exec failed";
my $out = $ch->read;
print "kernel: $out";
print "exit: ", $ch->exit_status, "\n";
$ch->close;

DESCRIPTION

Net::LibSSH::Channel represents an open SSH session channel. Instances are created via "channel" in Net::LibSSH and must not be constructed directly.

A channel keeps its session alive on its own: it holds its own reference to the session object it was opened on, so letting the $ssh variable it came from go out of scope, assigning undef to it, or reassigning it to something else does not invalidate the channel. That guarantee is about the Perl variable, not about the session's connection — it does not protect a channel from $ssh->disconnect; see below.

Calling $ssh->disconnect on the originating session invalidates every channel already opened on it — libssh frees them as part of disconnecting. From that point every channel method but close croaks with "Net::LibSSH::Channel::<method>: session was disconnected", a message distinct from "channel is closed" so a caller can tell its own teardown from libssh's. A channel that was closed with close before its session disconnected keeps reporting "channel is closed" afterwards — whichever happened to the channel first is what it reports. Calling close on a channel invalidated by disconnect is a harmless no-op, not an error, and so is simply dropping the channel (letting it go out of scope, or assigning undef to it) — teardown that has already happened is not a failure.

A channel runs one command per lifetime; an interactive shell ("request_pty([$cols, $rows [, $term]])", "request_shell") counts as that one command. After exec completes you can read stdout and stderr independently, then retrieve the exit status. Call close (or let the object go out of scope) to free the underlying libssh channel. close is idempotent; every other channel method croaks once the channel has been closed, with "Net::LibSSH::Channel::<method>: channel is closed".

METHODS

exec($command)

$ch->exec('uname -r') or die "exec failed";

Execute a command on the remote host. Returns 1 on success, 0 on failure. Must be called exactly once per channel.

Croaks if the channel has already been closed (see "close"), or if its session has been disconnected (see "disconnect" in Net::LibSSH).

read([$length [, $is_stderr]])

my $stdout = $ch->read;         # slurp all stdout until EOF
my $chunk  = $ch->read(4096);   # read up to 4096 bytes from stdout
my $stderr = $ch->read(-1, 1);  # slurp all stderr

Read output from the channel. Without arguments (or with -1 length), reads until the remote side closes the stream. With a positive $length, reads at most that many bytes. Pass a true $is_stderr as the second argument to read from stderr instead of stdout.

Note: Do not pass undef as the length — it evaluates to 0 and reads nothing. Use -1 or omit the argument entirely to slurp all output. Nothing is consumed from the channel by a zero-length read, so the data is not lost — a later read call still returns it in full.

Note: read has no way to tell a clean end-of-stream apart from a read error. Both end the same underlying loop and come back as a (possibly empty) string; read never returns undef and never croaks over a read failure itself.

Returns a string (possibly empty). Croaks if the channel has already been closed (see "close"), or if its session has been disconnected (see "disconnect" in Net::LibSSH).

request_pty([$cols, $rows [, $term]])

$ch->request_pty or die "no pty";               # xterm, 80x24
$ch->request_pty(132, 50, 'vt100') or die "no pty";

Ask the server for a pseudo-terminal on this channel; call it before "request_shell". Without arguments the terminal is xterm, 80 columns by 24 rows (libssh's own default). $cols and $rows go together and must be integers from 1 to 65535; $term defaults to xterm and must not be empty. Returns 1 on success, 0 if the server refuses.

Unlike the numeric options of "option" in Net::LibSSH, bad arguments are not silently turned into 0: a lone size, a zero or non-numeric size, or an empty $term croaks.

Croaks if the channel has already been closed (see "close"), or if its session has been disconnected (see "disconnect" in Net::LibSSH).

request_shell

$ch->request_shell or die "no shell";

Start the user's interactive shell on the channel, in place of "exec($command)". Returns 1 on success, 0 on failure. A shell is the channel's one command, like an exec: a channel runs one command or one shell in its lifetime, and a second request on it is the server's to refuse. Talk to the shell with "write($data)" and "read_timeout($seconds [, $length [, $is_stderr]])"; "read([$length [, $is_stderr]])" would block until the shell ends. A device that runs its own dialog needs "auth_none" in Net::LibSSH first.

$ssh->auth_none or die $ssh->error;
my $ch = $ssh->channel;
$ch->request_pty and $ch->request_shell or die "no shell";
my $prompt = $ch->read_timeout(5);     # "User Name:"
$ch->write("admin\r");

Croaks if the channel has already been closed (see "close"), or if its session has been disconnected (see "disconnect" in Net::LibSSH).

read_timeout($seconds [, $length [, $is_stderr]])

my $data = $ch->read_timeout(2);
if    (!defined $data) { print "the remote side is done\n" }
elsif ($data eq '')    { print "nothing yet\n" }
else                   { print "got: $data" }

Wait up to $seconds (a number, fractions allowed) for output and return as soon as some has arrived, at most $length bytes of it (default 4096; must be at least 1). A true $is_stderr reads stderr instead of stdout. 0 does not wait: it takes what has already arrived, once.

The three outcomes are told apart, which "read([$length [, $is_stderr]])" cannot do:

a non-empty string

Data. Call again for more.

the empty string

Nothing arrived within $seconds; the channel is still open and may yet produce output.

undef

End of file: the remote side closed or sent EOF and everything it sent has been read. Every further call returns undef at once.

A failing connection (the peer vanished, a protocol error) croaks with libssh's message, so it cannot be mistaken for either an empty timeout or the end of the stream. A negative or non-numeric $seconds, or a $length below 1, croaks too; a negative timeout would otherwise mean "wait forever" to libssh.

This is also the non-blocking read: read_timeout(0). The timeout is in seconds like the timeout option, whereas libssh counts milliseconds.

Croaks if the channel has already been closed (see "close"), or if its session has been disconnected (see "disconnect" in Net::LibSSH).

write($data)

my $n = $ch->write("input\n");

Write $data to the channel's standard input. Returns the number of bytes written, or a negative value on error. On a shell channel this is what you type.

Croaks if the channel has already been closed (see "close"), or if its session has been disconnected (see "disconnect" in Net::LibSSH).

send_eof

$ch->send_eof;

Signal end-of-input to the remote command. Call this after all write calls so that commands reading stdin (e.g. cat) know to terminate.

Croaks if the channel has already been closed (see "close"), or if its session has been disconnected (see "disconnect" in Net::LibSSH).

eof

$ch->send_eof;
my $out = $ch->read;
$ch->eof and print "channel closed by remote\n";

Returns true when the remote side has sent EOF on its stdout.

Croaks if the channel has already been closed (see "close"), or if its session has been disconnected (see "disconnect" in Net::LibSSH).

exit_status

my $rc = $ch->exit_status;

Returns the exit status of the executed command, or -1 when there is no exit code to report. It does not poll and return at once: the call waits for the remote side to report the exit status, and returns -1 only if that does not happen. The supported order is to read all output first.

-1 has two causes, and the return value alone does not tell them apart:

the process has not exited yet

The session timeout option (see option) ran out before the exit status arrived. Without that option this module sets no timeout of its own, so nothing bounds the wait; set one if a stuck remote command must not hold the caller. With timeout => 1, a call made while the command is still running returns -1 after about that second, and the -1 does not stick: asking again once the process has exited returns its real code.

the process was killed by a signal

A command that dies from a signal (kill -9 $$) never reports an exit status, so there is no code to return and the answer is -1. Waiting longer does not turn it into a code, so a loop that retries on -1 needs an upper bound of its own.

Calling exit_status before draining the output still returns the correct exit code in the test suite, and does not discard what has not been read yet: a later read returns it in full, for a few bytes as for 300000. That is observed behaviour, not a guarantee, and a libssh that blocked here would hang instead of returning; keep reading output before checking exit_status as the supported order.

Croaks if the channel has already been closed (see "close"), or if its session has been disconnected (see "disconnect" in Net::LibSSH). Note that exit_status must be read before close and before $ssh->disconnect, not after either: once the channel is closed, or its session has disconnected, this croaks instead of returning the exit code.

close

$ch->close;

Close the channel and free the underlying libssh resources (send EOF, close, free). Also called automatically when the object is garbage-collected.

close is idempotent — calling it again on an already-closed channel is a no-op, not an error. Every other channel method croaks once the channel is closed, with "Net::LibSSH::Channel::<method>: channel is closed".

close is also a no-op, not an error, on a channel invalidated by $ssh->disconnect — see "disconnect" in Net::LibSSH. It never croaks with "session was disconnected"; that message is reserved for the other methods.

SEE ALSO

Net::LibSSH, Net::LibSSH::SFTP

SUPPORT

Issues

Please report bugs and feature requests at https://github.com/Getty/p5-net-libssh/issues.

CONTRIBUTING

Contributions are welcome! Please fork the repository and submit a pull request.

AUTHOR

Torsten Raudssus <getty@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> https://raudssus.de/.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.