Link to home
Start Free TrialLog in
Avatar of howruaz9
howruaz9Flag for Canada

asked on

How to use module Net::SSH::Perl?

I tried to use Net::SSH::Perl as below, but got error message. (I could manually login this server using putty)

Is there anyone know how to do it.

Thanks very much!
use Net::SSH::Perl;
 
$host="testserver";
$user="bnfjghy";
$pass="sdhj3$%ED";
$cmd="ls -l";
 
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass);
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
print "$stdout\n";
my($stdout, $stderr, $exit) = $ssh->cmd(exit);

Open in new window

ss.bmp
ASKER CERTIFIED SOLUTION
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of howruaz9

ASKER

Thank you so much nemws1, I will try tomorrow
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Adam314. I will install Net::SSH::W32Perl and try on it.
I can't install Net::SSH::W32Perl using ppm. Is there another way to do it?
Thanks

sshperl.bmp
Avatar of Adam314
Adam314


ppm install --location=http://www.soulcage.net/ppds Net::SSH::W32Perl

Open in new window

I have tried many different ways to install. But it didn't work.
 

1.bmp
Did you ever try:

set HOME=value_can_be_anything

just for the heck of it?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you nemws1,  but It still didn't work after set home.
The error message is below
Can't set socket non-blocking: Bad file descriptor at C:/Perl/site/lib/Net/SSH/P
erl.pm line 216.

home.bmp
Thanks very much Adam314.
I tried to add Soulcage in Repositories. after it done, the number of pkgs is 0.
Please see attached file.
Thanks you so much anyway.


1.bmp
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much Adam314.
I followed your instruction to install Net::SSH::W32Perl. The below is result. Please help me check where I did wrong.   Thank you again.

use Net::SSH::Perl;
use Net::SSH::W32Perl;
 
$host="testserver";
$user="bnfjghy";
$pass="sdhj3$%ED";
$cmd="ls -l";
 
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass);
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
print "$stdout\n";

Open in new window

4.bmp
5.bmp
You need to tell your program to use Net::SSH::W32Perl instead of Net::SSH::Perl.

Remove this line:
    use Net::SSH::Perl;

Change this line:
    my $ssh = Net::SSH::Perl->new($host);
To this:
    my $ssh = Net::SSH::W32Perl->new($host);
I have changed as you said. The result is below.
7.bmp
I hope it isn't a case problem.  Your 'w' in 'w32' is lowercase.   Windows doesn't care, but Perl might.  Change the 'w' to uppercase and try again.
Yes, case matters.  You need to have the W in upper case.
Thanks very much nemws1 and Adam314.

I will try it tomorrow in company because I can't test at home.

I really appreciate your kind help!
I have changed the W in upper case. below is what I got.
Is there anyone know what it mean?
Thanks
 
result

8.bmp
You still need the HOME environment variable set to your home directory:
    set HOME=c:\Documents and Settings\howruaz9
After set Home, it get much better, but there is still some error message and stop at step 3 .....
#system("set HOME=C:\IS");
use Net::SSH::W32Perl;
 
$host="testserver";
$user="bnfjghy";
$pass="sdhj3$%ED";
$cmd="ls -l";
 
   my $ssh = Net::SSH::W32Perl->new($host);
      print "\ndone 1\n";
 
   $ssh->login($user, $pass);
      print "\ndone 2\n";
 
   $ssh->session_id;
     print "\ndone 3\n";
 
    my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
      print "\ndone 4\n";
      print "$stdout\n";
 
     my($stdout, $stderr, $exit) = $ssh->cmd("exit");
      print "\ndone 5\n";

Open in new window

done-3.bmp
Do you have Math::BigInt installed?
Yes, I have Math::BigInt.
33.bmp
The messages you got are not errors, they are warnings.  The Math::BigInt module has several sub-modules it uses for various things.  If those modules aren't available, it can do the stuff itself, but is slower (sometimes much slower).  The warnings you got are telling you that it couldn't use the faster sub-module, so it's using a slower pure-perl module instead.
To address, try a re-install of these:
    Math::BigInt
    Math::BigInt::GMP
    Math::BigInt::Pari
    Math::BigInt::BitVector


What is the error after step 3?
After step 3, waiting looooong time, No any other message.

I will re-install the modules you mentioned.

Thank you very much Adam314
I have re-installed Math::BigInt, Math::BigInt::GMP, Math::BigInt::Pari. But can't find Math::BigInt::BitVector.

4.bmp
Try removing the "ls -l" command, just log in then exit.  
Sure. I will tell you the result on Monday.  Have a nice weekend.
Below is result. Stop at step 3, there is no any error message. WHY????????????
#system("set HOME=C:\IS");
use Net::SSH::W32Perl;
 
$host="testserver";
$user="bnfjghy";
$pass="sdhj3$%ED";
$cmd="ls -l";
 
   my $ssh = Net::SSH::W32Perl->new($host);
      print "\ndone 1\n";
 
   $ssh->login($user, $pass);
      print "\ndone 2\n";
 
   $ssh->session_id;
     print "\ndone 3\n";
 
#    my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
#      print "\ndone 4\n";
#      print "$stdout\n";
 
     my($stdout, $stderr, $exit) = $ssh->cmd("exit");
      print "\ndone 5\n"; 

Open in new window

222.bmp
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much Adam314.
Option 3 and 4 worked;  Option 1 and 2 didn't work;.
please see attached file
 
it executed my($stdout, $stderr, $exit) = $ssh->cmd("ls -l > /dev/null 2> /dev/null ");
but didn't execute   print "$stdout\n";
How can I get result after execute a command?

 
 

option.bmp
It appears there is a bug that causes it to hang when there is output on STDERR.  Try these.  If they work, you should have $stdout.  The second will also giver your STDERR in $stdout.


##### Option 6
my($stdout, $stderr, $exit) = $ssh->cmd("ls -l 2> /dev/null");
print "done option 6\n";
print "  stdout=$stdout\n";
print "  stderr=$stderr\n";
print "  exit=$exit\n";
 
##### Option 7
my($stdout, $stderr, $exit) = $ssh->cmd("ls -l 2> \&1");
print "done option 7\n";
print "  stdout=$stdout\n";
print "  stderr=$stderr\n";
print "  exit=$exit\n";

Open in new window

Sorry, Adam314, Option 6/7 didn't work. script stop at step 3. There is no any error message.
Maybe the problem is with getting any output.  How about this:
##### Option 8
my($stdout, $stderr, $exit) = $ssh->cmd("ls -l > /dev/null 2> /dev/null");
print "done option 8\n";
print "  stdout=$stdout\n";
print "  stderr=$stderr\n";
print "  exit=$exit\n";
 
##### Option 9 (change output file name if needed)
my($stdout, $stderr, $exit) = $ssh->cmd("ls -l > /tmp/test1.txt 2> /dev/null");
print "done option 9\n";
print "  stdout=$stdout\n";
print "  stderr=$stderr\n";
print "  exit=$exit\n";
 

Open in new window

I have tried option 8 and 9. It didn't work.
More confusion is below command didn't work SOMETIME, I used Putty manually login test server, there was no problem
my($stdout, $stderr, $exit) = $ssh->cmd("exit > /dev/null 2> /dev/null");

I also found when execute this script, it create a new directory  .ssh.  What does this mean?
Thanks so much Adam314 for your time and patien.
 

1.bmp
Don't you already have an .ssh directory in your home directory?  Do you have have the HOME environment variable pointing to your home directory, probably something like C:\Documents and Settings\<username>?
This .ssh directory is the only one on hard driver.
For environment variable, please see following file.

eee.bmp
What version of perl are you using?
This is perl, v5.10.0 built for MSWin32-x86-multi-thread

Thanks
Adam314 and nemws1, I really appreciate your help!