2

I don't know if I managed to install Net::SSH::Perl module successfully but I can't seem to be able to run the following code:

my $ssh = Net::SSH::Perl->new($remote_host);
$ssh->login($username, $password);
print "login done", "\n";
my ($out, $err, $exit) = $ssh->cmd($cmd);
print "$out", "\n";

I am able to login but cannot print the $out. I keep getting this error:

Use of uninitialized value $out in string at test_ssh.pl line 28.

Line 28 refers to print "$out", "\n";.

I am running this code on Cygwin. What should I do now?

EDIT: I got the following error msg when I ran my $ssh = Net::SSH::Perl->new($remote_host, options => ["Debug yes"]);:

Use of uninitialized value $out in string at test_ssh.pl line 29 (#1)
    (W uninitialized) An undefined value was used as if it were already
    defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.

    To help you figure out what was undefined, perl will try to tell you the
    name of the variable (if any) that was undefined. In some cases it cannot
    do this, so it also tells you what operation you used the undefined value
    in.  Note, however, that perl optimizes your program and the operation
    displayed in the warning may not necessarily appear literally in your
    program.  For example, "that $foo" is usually optimized into "that "
    . $foo, and the warning will refer to the concatenation (.) operator,
    even though there is no . in your program.

EDIT2: Here's my full code

use strict;
use warnings;
use Net::SSH::Perl;

my $remote_host = '<host ip address>';
my $password    = 'pass';
my $username    = 'user';
my $cmd         = 'copy run tftp:<my own ip address>';

warn "Starting SSH Services:...";
my $ssh = Net::SSH::Perl->new($remote_host, debug => 1);
print "done", "\n";

warn "Starting Login:...";
$ssh->login($username, $password);
print "login done", "\n";

warn "Starting command:...";
#$ssh->cmd($cmd);
#my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
my ($out, $err, $exit) = $ssh->cmd($cmd);
print "$out", "\n";

The error message on "print "$out","\n";" line:

<Computername>: channel 1: new [client-session]
<Computername>: Requesting channel_open for channel 1.
<Computername>: Entering interactive session.
<Computername>: Channel open failure: 1: reason 4:
Use of uninitialized value $out in string at test_ssh.pl line 29.

LAST EDIT: I decided to use Net::Appliance::Session to login via SSH to the network devices instead. it's a lot easier to use than Net::SSH::Perl.

5
  • Turn on debug with my $ssh = Net::SSH::Perl->new($remote_host, options => ["Debug yes"]); Check for the value of err before printing. If you are unsure, show us the value off err and the debug output. Commented Apr 10, 2012 at 3:50
  • It is "print "$out", "\n";". I got the same error message (except with different line number but is referring to the same line of code) after adding the code you gave me. Commented Apr 10, 2012 at 4:41
  • @weismat: that's not how you enable debugging for this module. Commented Apr 10, 2012 at 7:03
  • Whats the cmd you are running? Commented Apr 10, 2012 at 7:56
  • @user967552, I am using Cygwin, if that is what you mean. Commented Apr 10, 2012 at 8:42

3 Answers 3

1

Please show more of your code. What is the value of $cmd?

Note that the login method doesn't perform a login: it merely stores the username and password to be used when the connection is set up for each cmd call.

The correct way of enabling debugging messages is

my $ssh = Net::SSH::Perl->new($remote_host, debug => 1);

This will give you trace information from the cmd method which should say, amongst other things

Sending command: xxx
Entering interactive session.

and should give you some clues about what is going wrong.


Your debug output shows the problem. Looking at SSH2.h, open failure reason 4 is SSH2_DISCONNECT_HOST_AUTHENTICATION_FAILED. Your username and password are incorrect.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I have added to my answer to explain your error. What are the contents of $err and $exit after the call?
Hi. I am quite sure that the username and password is correct because I used the same credentials and use Net::SSH and Expect to code the login and it does not give me any error. In fact, it managed to print out the welcome message when I have successfully log into the system.
All I can tell you is that the software is saying the authentication has failed :-/
1

Net::SSH::Perl does support login via username/password, I have a working example, I just got this to work. I used the code from above and took out the Double Quotes (" ") and used single quotes (' ') instead. And "debug => 1" works for debugging the code when having issues. It will display info to you when you try to login if the debug option is set.

I am connecting to a Win32-OpenSSH SSHD server based on Windows Powershell very similar to BSDLinux SSHD server with SFTP support. Supports same Linux style based connection.

I have been trying all other SSH modules all day. Hopefully someone can use this code to just run a command and get the output if required.

You can install Net::SSH::Perl with "cpan -i module-name"

 use Net::SSH::Perl;

my $host = 'testmachine.acme.local';     #Or just IP Address
my $user = 'domain\username';            #Or just username
my $pass = 'Password@123';
my $cmd = 'dir C:\\Windows\\'; 

use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new($host, debug => 1);
$ssh->login($user, $pass);
my ($out, $err, $exit) = $ssh->cmd($cmd);
print "$out", "\n";

Comments

0

Net::SSH::Perl does not support login via username/password only via interactive password entry or public key. See this post for more information.

http://www.perlmonks.org/?node_id=590452

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.