$ssh2->auth_password($login{user}, $login{password});
We first need to establish exactly where you're script is failing.
I suspect that authorization might be the problem.
Instead do:
$ssh2->auth_password($login{user}, $login{password})
or die "Auth failed";
$ssh2->auth_ok();
The value returned by that expression will tell you whether you are authorized or not - but you haven't checked that value.
You'd normally check that value:
$ssh2->auth_ok() or die "Not authorized";
On Srawberry Perl, I'm finding that auth_password won't work now that I've got public key authorization set up. (Not sure if that's "just me", or "just windows", or "just the way it's supposed to be".)
So, if the user you're connecting as has public key authorization set up, you might need to authorize using auth_publickey.
Here's the script I'm using with Strawberry Perl 5.28.0 on Windows 7, to channel into a local Ubuntu box:
use warnings;
use Net::SSH2;
use strict;
my @output;
my $ssh = Net::SSH2->new();
$ssh->connect("host") or die "Unable to connect host\n";
# auth_password no longer working for me
# $ssh->auth_password("user", "password")
# or die "Failed to auth\n";
# using auth_publickey works
$ssh->auth_publickey(
"user",
"C:\\cygwin\\home\\user\\.ssh\\id_rsa.pub",
"C:\\cygwin\\home\\user\\.ssh\\id_rsa",
0)
or die "Auth failed";
die "Not authenticated"
unless $ssh->auth_ok; # Double checking
my $channel = $ssh->channel()
or die "Channel creation failed\n";
$channel->blocking(1);
$channel->exec("ls ~");
while (<$channel>) {push @output, $_}
print scalar @output, "\n"; # See no. of entries
print for @output;
Works fine for me - but doesn't necessarily help you ;-)
UPDATE: I also have SSH access to a remote machine via the internet, and this access requires auth_password authorization (as public key authorization has not been set up).
The above script also works fine for it, once I switch from using auth_publickey to auth_password.
Cheers,
Rob
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.