Thanks for this post, zentara, it's really helped me understand the concepts and implementation of rsa authentication in perl. I think that I achieve success in that an sftp object is created using key pairs. The very critical thing to first do is this:
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/bob/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/bob/.ssh/id_rsa.
Your public key has been saved in /home/bob/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LCWI8Wrw14m0Cxz1P+TqmL7Nn+mY19fWETCfjYK87VY bob@bob-ThinkPad-SL
+510
The key's randomart image is:
+---[RSA 2048]----+
| . . . |
| = o o o |
|. o + o +. . +.o|
| + + + B .o . .+.|
| * + = S o . .|
| . o = . . . E. |
| + . . . o ..|
| . o o.o. + o .|
| o.=+= o . |
+----[SHA256]-----+
$
This was output upon first running:
values are home349337426.1and1-data.host
The authenticity of host 'home349337426.1and1-data.host' can't be esta
+blished.
Key fingerprint is SHA1:6bfe32c8859a967c8ed6cebdd5c48b72edff71c7.
Are you sure you want to continue connecting (yes/no)? y
mkdir is not a valid Net::SSH2 macro at ssh4.pl line 31.
$
$
Running it again, I get none of the dialog regarding whether the responder is trusted. At the end, I'm still left with not being able to invoke a mkdir method.
Turning to the source, the meaning for password has shifted from the one you use for login to the one you used to create the ciphers.
#!/usr/bin/perl -w
use strict;
use Net::SSH2 qw(LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE
LIBSSH2_CHANNEL_FLUSH_ALL
LIBSSH2_HOSTKEY_POLICY_ASK);
use 5.010;
use lib "template_stuff";
use config2;
# none of this makes any sense until you run
# ssh-keygen -t rsa
my $sub_hash = "my_sftp";
my $domain = $config{$sub_hash}->{'domain'};
my $username = $config{$sub_hash}->{'username'};
say "values are $domain";
my $pass = "ringo";
my $ssh2 = Net::SSH2->new( debug => 1 );
$ssh2->trace(-1);
$ssh2->timeout(5000);
$ssh2->connect($domain) or $ssh2->die_with_error;
$ssh2->check_hostkey(LIBSSH2_HOSTKEY_POLICY_ASK);
$ssh2->auth_publickey($username,
'/home/bob/.ssh/id_rsa.pub',
'/home/bob/.ssh/id_rsa',
$pass );
my $success = $ssh2->mkdir("perlmonks");
say "success is $success";
return 0;
__END__
I return to the documentation to find that sftp methods are less than fully supported, indeed that I'm advised to use something else: https://metacpan.org/pod/Net::SSH2#sftp I have found it immensely instructive so far, even if I might use the higher level Net::SFTP::Foreign henceforth. I hope to be able to do roughly this same exercise with that module.
|