Net-SSH2
view release on metacpan or search on metacpan
lib/Net/SSH2/Channel.pm view on Meta::CPAN
package Net::SSH2::Channel;
use strict;
use warnings;
use Carp;
# methods
sub shell {
$_[0]->process('shell')
}
sub exec {
$_[0]->process(exec => $_[1])
}
sub subsystem {
$_[0]->process(subsystem => $_[1])
}
sub error {
shift->session->error(@_)
}
sub blocking {
shift->session->blocking(@_)
}
sub setenv {
my ($self, %env) = @_;
my $rc = 1;
while (my ($k, $v) = each %env) {
$self->_setenv($k, $v)
or undef $rc;
}
$rc
}
sub read1 {
my $self = shift;
my $buffer;
my $rc = $self->read($buffer, @_);
return (defined $rc ? $buffer : undef);
}
sub read2 {
my ($self, $max_size) = @_;
$max_size = 32678 unless defined $max_size;
my $ssh2 = $self->session;
my $old_blocking = $ssh2->blocking;
my $timeout = $ssh2->timeout;
my $delay = (($timeout and $timeout < 2000) ? 0.0005 * $timeout : 1);
my $deadline;
$deadline = time + 1 + 0.001 * $timeout if $timeout;
$ssh2->blocking(0);
while (1) {
my @out;
my $bytes;
my $fail;
my $zero;
for (0, 1) {
my $rc = $self->read($out[$_], $max_size, $_);
if (defined $rc) {
$rc or $zero++;
$bytes += $rc;
$deadline = time + 1 + 0.001 * $timeout if $timeout;
}
else {
$out[$_] = '';
if ($ssh2->error != Net::SSH2::LIBSSH2_ERROR_EAGAIN()) {
$fail++;
last;
}
}
}
if ($bytes) {
$ssh2->blocking($old_blocking);
return (wantarray ? @out : $out[0])
}
my $eof = $self->eof;
if ($fail or $eof) {
$ssh2->_set_error if $eof;
$ssh2->blocking($old_blocking);
return;
}
unless ($zero) {
return unless $old_blocking;
if ($deadline and time > $deadline) {
$ssh2->_set_error(Net::SSH2::LIBSSH2_ERROR_TIMEOUT(), "Time out waiting for data");
return;
}
my $sock = $ssh2->sock;
my $fn = fileno($sock);
lib/Net/SSH2/Channel.pm view on Meta::CPAN
my $bytes = $self->read(my($buffer), $len);
substr($_[1], $offset || 0) = $buffer
if defined $bytes;
return $bytes;
}
sub BINMODE { 1 }
sub CLOSE {
my $self = shift;
my $ob = $self->blocking;
$self->blocking(1);
my $rc = undef;
if ($self->close and
$self->wait_closed) {
my $status = $self->exit_status;
my $signal = $self->exit_signal_number;
$self->session->_set_error;
$? = ($status << 8) | $signal;
$rc = 1 if $? == 0;
}
$self->blocking($ob);
$rc;
}
sub EOF {
my $self = shift;
$self->eof;
}
*GETC = \&getc;
1;
__END__
=head1 NAME
Net::SSH2::Channel - SSH2 channel object
=head1 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";
=head1 DESCRIPTION
A channel object is created by the L<Net::SSH2> C<channel> method. As well
as being an object, it is also a tied filehandle.
=head2 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.
=head2 blocking ( flag )
Enable or disable blocking.
Note that this is currently implemented in libssh2 by setting a
per-session flag. It's equivalent to L<Net::SSH2::blocking>.
=head2 eof
Returns true if the remote server sent an EOF.
=head2 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 C<STDIN> channel.
Note that if a PTY was requested for the channel, the EOF may be
ignored by the remote server. See L</pty>.
=head2 close
Close the channel (happens automatically on object destruction).
=head2 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.
=head2 exit_status
Returns the channel's program exit status.
This method blocks until the remote side closes the channel.
=head2 pty ( terminal [, modes [, width [, height ]]] )
Request a terminal on a channel.
C<terminal> is the type of emulation (e.g. vt102, ansi,
etc...).
C<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 (L<RFC4254|https://tools.ietf.org/html/rfc4254#section-8>).
( run in 0.613 second using v1.01-cache-2.11-cpan-6aa56a78535 )