in reply to Re^2: instantiating an SFTP object
in thread instantiating an SFTP object

Hi, here is a Net::SSH2 sftp script that works. I just verified it. This shows how to do a login with rsa keys, the password has been changed of course. Works on latest Slackware linux, which is pretty standard generic linux.
#!/usr/bin/perl use warnings; use strict; use Net::SSH2 qw(LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE LIBSSH2_CHANNEL_FLUSH_ALL LIBSSH2_HOSTKEY_POLICY_ASK); my $pass = 'rumpelstiltskin'; my $ssh2 = Net::SSH2->new( debug => 1 ); $ssh2->trace(-1); $ssh2->timeout(5000); $ssh2->connect('my.net') or $ssh2->die_with_error; $ssh2->auth_publickey('me', '/home/me/.ssh/id_rsa.pub', '/home/me/.ssh/id_rsa', $pass ); my $sftp = $ssh2->sftp(); my $fh = $sftp->open('/etc/passwd') or $sftp->die_with_error; print $_ while <$fh>; return 0; __END__

I'm not really a human, but I play one on earth. ..... an animated JAPH

Replies are listed 'Best First'.
Re^4: instantiating an SFTP object
by salva (Canon) on Jun 13, 2017 at 18:58 UTC
    After connect you should call check_hostkey. For instance:
    $ssh2->check_hostkey(LIBSSH2_HOSTKEY_POLICY_ASK);
      Hi, I don't want to feel like a dufus, but I tried
      my $return = $ssh2->check_hostkey(LIBSSH2_HOSTKEY_POLICY_ASK); print "Return: $return\n\n\n\n";
      and all I get is Return 00 and the normal filehandle printout. How exactly do I efficiently print out the returns from the check_hostkey command.? I'm cultivating laziness. :-)

      I'm not really a human, but I play one on earth. ..... an animated JAPH
        If the remote host key is good enough for the given policy, the method returns a true value. Otherwise it returns undef and the error (or die_with_error) method can be used to find out the failure cause.
        $ssh2->check_hostkey(LIBSSH2_HOSTKEY_POLICY_ASK) or $ssh2->die_with_error;
        The 0-but-true value 00 is equal to LIBSSH2_KNOWNHOST_CHECK_MATCH.
Re^4: instantiating an SFTP object
by Aldebaran (Curate) on Jun 13, 2017 at 22:13 UTC

    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.

      the meaning for password has shifted from the one you use for login to the one you used to create the ciphers

      Exactly!! :-) Glad you are figuring it out.


      I'm not really a human, but I play one on earth. ..... an animated JAPH