----- Original Message ----- From: "perl_haxor 123" <perl.ha...@gmail.com>
To: "Sisyphus" <sisyph...@optusnet.com.au>
Cc: <beginners@perl.org>
Sent: Thursday, March 11, 2010 6:38 AM
Subject: Re: stripped output from Net::SSH2


find the script below:

#!/usr/bin/perl -w
use strict;

use Term::ReadKey;
use Net::SSH2;

use constant BUFLEN => 10_0000;


my $user = "user";

print "Enter the the password: ";
ReadMode 'noecho';
my $pass = ReadLine 0;
print "\n";
chomp $pass;
ReadMode 'normal';

open INPUT, "<", "input.txt";
open STDERR, ">", "Error.txt";


while(<INPUT>) {


   chomp $_;

my $ssh2 = Net::SSH2->new();

unless ($ssh2->connect("$_")) {

print "unable to connect to Host: $_\n";
print STDERR "Unable to connect to $_: $!\n";
print STDERR
"#############################################################################################\n\n";
next;

}

print "connecting to $_\n";


unless ($ssh2->auth_password("$user","$pass")) {


   print "Invalid Password\n";
   exit;
}

my $chan = $ssh2->channel;
$chan->exec('sh run');

my $buf;
my $read = $chan->read($buf, BUFLEN);
warn 'More than ', BUFLEN, ' characters in listing' if $read >= BUFLEN;
open OUTPUT, ">", "$_.txt";
print OUTPUT "HOST: $_\n\n";
print OUTPUT "$buf\n";
print OUTPUT "\n\n\n";
print OUTPUT
"##################################################################################################\n\n";
close OUTPUT;
$chan->close();
}


I can't reproduce the problem.

However, I have access to only one SSH2 server, so my "input.txt" simply contained 3 identical lines - namely "192.168.0.3" (which is the IP address of the machine running the server).

Does your problem go away if *you* connect to the same server at each iteration ?

For me "sh run" just produces a single line of output - namely "run: run: No such file or directory". Not very useful, so I changed the command to "cat pscrpt/hello.pl" which prints out a multiline file. If you change your command to "cat some_multiline_file", does your problem go away ?

Finally because I was connecting to the same server each time, I had to modify the script to create separate files for each iteration of the while loop. First iteration writes to 192.168.0.30.txt, second iteration to 192.168.0.31.txt and the third iteration to 192.168.0.32.txt.

For each of those 3 iterations, pscrpt/hello.pl is correctly reproduced.
In order to get the same error as you, do I need to:
a) cat a bigger file ? (hello.pl is only about 15 lines)
b) run some command other than cat ?
c) iterate through the loop more than 3 times ?
d) connect to different servers each time ?

Do you get the error for *every* server you connect to ?
If it's just *some* servers that are giving the problem, is it the *same* servers each time ? And do each of those "same" servers produce the error *every* time you run the script ?

Finally, which version of perl are you running ?
I have version 5.10.1, and am using Net-SSH2-0.28 (built against libssh2-1.2.2).

There's a ppm for that version of Net-SSH2-0.28 (also built against libssh2-1.2.2) for both perl-5.8 and perl-5.10 at the uwinnipeg repository. If you're using a different version of Net::SSH2, maybe give that uwinnipeg ppm a try and see if it makes any difference.

Sorry for all of the questions - but if I can't reproduce the problem it's going to be difficult to provide any useful help.

Admittedly, it's not your fault that I have only one SSH2 server to connect to, or that "sh run" doesn't produce multiline output on that server :-)

Below my sig is the actual script I ran.

Cheers,
Rob

##############################
#!/usr/bin/perl -w
use strict;

use Term::ReadKey;
use Net::SSH2;

use constant BUFLEN => 10_0000;

print "Enter username: ";
my $user = <STDIN>;
chomp $user;

print "Enter the the password: ";
ReadMode 'noecho';
my $pass = ReadLine 0;
print "\n";
chomp $pass;
ReadMode 'normal';

my $inc = 0; # Incremented at each iteration
            # of the while loop

open INPUT, "<", "input.txt";
open STDERR, ">", "Error.txt";


while(<INPUT>) {


   chomp $_;

my $ssh2 = Net::SSH2->new();

unless ($ssh2->connect("$_")) {

print "unable to connect to Host: $_\n";
print STDERR "Unable to connect to $_: $!\n";
print STDERR
"#############################################################################################\n\n";
next;

}

print "connecting to $_\n";

unless ($ssh2->auth_password("$user","$pass")) {
   print "Invalid Password\n";
   exit;
}

my $chan = $ssh2->channel;
#$chan->exec('sh run');
$chan->exec('cat pscrpt/hello.pl');

my $buf;
my $read = $chan->read($buf, BUFLEN);
warn 'More than ', BUFLEN, ' characters in listing' if $read >= BUFLEN;
open OUTPUT, ">", "$_$inc.txt";
print OUTPUT "HOST: $_\n\n";
print OUTPUT "$buf\n";
print OUTPUT "\n\n\n";
print OUTPUT
"##################################################################################################\n\n";
close OUTPUT;
$chan->close();
$inc++;
}

########################################

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to