Perl Forum for Beginners

Sometimes Net::SSH2 can't read() from channel?

Apr 9, 2008 9:20 pm
Richard Fernandez

------_=_NextPart_001_01C89A87.7BA96B7A
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi folks,
=20
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.=20
Anyone have any ideas why this might be happening?
=20
TIA
=20
richf
=20
#!/usr/local/bin/perl
use warnings;
use strict;
use Net::SSH2;
use Getopt::Std;
=20
our($opt_s, $opt_p);
getopts('p:s:');
=20
my $passwd =3D $opt_p ? $opt_p : usage();
my $server =3D $opt_s ? $opt_s : usage();
=20
print "Connecting to $server...\n";
=20
my $ssh2 =3D Net::SSH2->new();
$ssh2->debug(1);
$ssh2->connect($server) or die "Can't connect to $server\n";
=20
if( $ssh2->auth_password('root', $passwd)) {
my $chan =3D $ssh2->channel();
my $returnval =3D $chan->exec('who') or die "Couldn't exec
'who'\n";
=20
my($len, $buff);
while($len =3D $chan->read($buff, 1024)) {
print $buff;
}
$chan->close;
}
else {
print "Invalid username or password $!\n";
}
=20
=20
sub usage {
die "usage: $0 -s <server> -p <passwd>\n";
}

=20
[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 =3D 1024, ext =3D 0)
- read 44 bytes
- read 0 bytes
- read 44 total
root tty1 Mar 9 10:55 =20
Net::SSH2::Channel::read(size =3D 1024, ext =3D 0)
- read 0 bytes
- read 0 total
Net::SSH2::Channel::DESTROY
Net::SSH2::DESTROY object 0x867c4c80
[richf@richx:/home/richf/bin]=20
[richf@richx:/home/richf/bin]=20
[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 =3D 1024, ext =3D 0)
- read 0 bytes
- read 0 total
Net::SSH2::Channel::DESTROY
Net::SSH2::DESTROY object 0x81245b40


------_=_NextPart_001_01C89A87.7BA96B7A--

Apr 9, 2008 9:26 pm
Chas. Owens
Re: Sometimes Net::SSH2 can't read() from channel?

On Wed, Apr 9, 2008 at 5:20 PM, RICHARD FERNANDEZ <rfernandez@arrow.com> 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.
Apr 10, 2008 1:31 pm
Zentara
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

I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Apr 10, 2008 6:37 pm
Richard Fernandez
RE: Sometimes Net::SSH2 can't read() from channel?



> -----Original Message-----
> From: Linux@lists.develooper.com
> [mailto: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




Previous Thread: How to extract digits by position in a string?
Next Thread: Re: how to globalize a lexical variable inside a sub routine

Related Forum Topics
Sending XML data file to the XML channel
------_=_NextPart_001_01C885CF.D5FD4070
Content-Type: text/plain;
charset="us-ascii"
content-transfer-encoding: quoted-printable

Hi,



I want to push XML data file to the XML channel, please find the code
snippet for the same.



########

#!/usr/bin/perl

use...
No output Net::SSH2
------=_Part_53108_10203566.1227126835359
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi All,

I have written 2 codes using "Net::SSH2" module which will connect
to remote machine (linux), execute commands and give me...
Problem installing Net::SSH2
------=_Part_105136_4704888.1227464212852
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi all
I'm getting error while installing Net::SSH2 module on Kubuntu
linux 8.10, I had installed the same module on in a...
Getting error when running Net::SSH2
------=_Part_114428_31201448.1227553113687
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi all,

I'm running kubuntu linux............I used Net::SSH2 to login to
remote machine, execute commands and give me the...
Stripped output from Net::SSH2
--000e0cd723169bd48b04817702b9
Content-Type: text/plain; charset=ISO-8859-1

Hi All,

I'm using Net::SSH2 module on windows machine, the purpose of this
script is to login to multiple devices and execute "show run" command and
print the output of the command to a text file, ...
Connecting to multiple hosts using Net::SSH2
--000e0cd1a62e68103d0463d4cff1
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Hi all,


I'm using windows machine and i'm using Net::SSH2 module to connect
to remote machine

below is the code i'm using, its working fine if i'm connecting to one...
Unable to connect to all the hosts using Net::SSH2 module
--000e0cd20bec2f18680464c3de9d
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Hi all,


Below is the script to login to multiple hosts and then execute the command
and give the ouput in text file


This script will login to all the hosts in input.txt(this...
How to read from keyboard?
------=_Part_7256_28608499.1204121090558
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi.

I want to read from keyboard some keys [pagedown],
[Home],[End][PageUp][up][left][down][right].

how can I do it?

Thanks in...
.profile not being read
I just installed perl on my Mac following the instructions on the perl.org website. It seems to work, as "perl -v" returns "This is perl 5, version 16, subversion 0 (v5.16.0) built for darwin-2level..."

The thing is that the commands in my .profile file are no longer being executed. The file...
Read and write
$ perl extract_v2.pl try.tex
Useless use of a constant (<) in void context at extract_v2.pl line 25.
Useless use of private variable in void context at extract_v2.pl line 25.
metabolism
toxic2
toxic4

#!/usr/bin/env perl

use strict;
use warnings;
use File::Spec;
use...