FAQ
Hi folks,

I have a test script ( mostly ripped from a posting I found on a CPAN
forum) which uses Net::SSH2 to run a `who' command on a remote server.
The script works fine for most servers, but I have a few boxes where,
despite being able to login, I get no output.
Anyone have any ideas why this might be happening?

TIA

richf

#!/usr/local/bin/perl
use warnings;
use strict;
use Net::SSH2;
use Getopt::Std;

our($opt_s, $opt_p);
getopts('p:s:');

my $passwd = $opt_p ? $opt_p : usage();
my $server = $opt_s ? $opt_s : usage();

print "Connecting to $server...\n";

my $ssh2 = Net::SSH2->new();
$ssh2->debug(1);
$ssh2->connect($server) or die "Can't connect to $server\n";

if( $ssh2->auth_password('root', $passwd)) {
my $chan = $ssh2->channel();
my $returnval = $chan->exec('who') or die "Couldn't exec
'who'\n";

my($len, $buff);
while($len = $chan->read($buff, 1024)) {
print $buff;
}
$chan->close;
}
else {
print "Invalid username or password $!\n";
}


sub usage {
die "usage: $0 -s <server> -p <passwd>\n";
}


[richf@richx:/home/richf/bin] ./mytest.pl -s server1 -p passwd
Connecting to server1...
libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type,
window_size, packet_size, 0L , 0 ) -> 0x89bb0600
Net::SSH2::Channel::read(size = 1024, ext = 0)
- read 44 bytes
- read 0 bytes
- read 44 total
root tty1 Mar 9 10:55
Net::SSH2::Channel::read(size = 1024, ext = 0)
- read 0 bytes
- read 0 total
Net::SSH2::Channel::DESTROY
Net::SSH2::DESTROY object 0x867c4c80
[richf@richx:/home/richf/bin]
[richf@richx:/home/richf/bin]
[richf@richx:/home/richf/bin] ./mytest.pl -s server2 -p passwd
Connecting to server2...
libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type,
window_size, packet_size, 0L , 0 ) -> 0x8241e600
Net::SSH2::Channel::read(size = 1024, ext = 0)
- read 0 bytes
- read 0 total
Net::SSH2::Channel::DESTROY
Net::SSH2::DESTROY object 0x81245b40

Search Discussions

  • Chas. Owens at Apr 9, 2008 at 9:27 pm

    On Wed, Apr 9, 2008 at 5:20 PM, RICHARD FERNANDEZ wrote:
    Hi folks,

    I have a test script ( mostly ripped from a posting I found on a CPAN
    forum) which uses Net::SSH2 to run a `who' command on a remote server.
    The script works fine for most servers, but I have a few boxes where,
    despite being able to login, I get no output.
    Anyone have any ideas why this might be happening?
    snip

    The first step is determining what, if anything, is different about
    the machines that you cannot get output from (OS, SSH Server,
    environment of the user, etc).

    --
    Chas. Owens
    wonkden.net
    The most important skill a programmer can have is the ability to read.
  • Zentara at Apr 10, 2008 at 1:32 pm

    On Wed, 9 Apr 2008 17:20:06 -0400, rfernandez@arrow.com ("RICHARD FERNANDEZ") wrote:

    Hi folks,

    I have a test script ( mostly ripped from a posting I found on a CPAN
    forum) which uses Net::SSH2 to run a `who' command on a remote server.
    The script works fine for most servers, but I have a few boxes where,
    despite being able to login, I get no output.
    Anyone have any ideas why this might be happening?
    Try setting:

    $chan->blocking(0);
    my $chan = $ssh2->channel();
    zentara

    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/japh.html
  • RICHARD FERNANDEZ at Apr 10, 2008 at 6:37 pm

    -----Original Message-----
    From: Linux@lists.develooper.com
    On Behalf Of zentara
    Sent: Thursday, April 10, 2008 9:31 AM
    To: beginners@perl.org
    Subject: Re: Sometimes Net::SSH2 can't read() from channel?

    On Wed, 9 Apr 2008 17:20:06 -0400, rfernandez@arrow.com ("RICHARD
    FERNANDEZ") wrote:
    Hi folks,

    I have a test script ( mostly ripped from a posting I found on a CPAN
    forum) which uses Net::SSH2 to run a `who' command on a
    remote server.
    The script works fine for most servers, but I have a few
    boxes where,
    despite being able to login, I get no output.
    Anyone have any ideas why this might be happening?
    Try setting:

    $chan->blocking(0);
    my $chan = $ssh2->channel();
    zentara
    Thank you, zentara. It seems this is what was needed.

    richf

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 9, '08 at 9:20p
activeApr 10, '08 at 6:37p
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase