Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Net::SFTP::Foreign and Using Passphrases on Private Keys

by swamprich (Novice)
on Apr 27, 2012 at 13:11 UTC ( #967629=perlquestion: print w/ replies, xml ) Need Help??
swamprich has asked for the wisdom of the Perl Monks concerning the following question:

I've been using Net::SFTP::Foreign with the Net_SSH2 backend for username/password and private key authentication without error for a little while now but I've been recently tasked with adding in functionality to utilize passphrase protected private keys.

Net:SFTP::Foreign has an option for passphrase, according to CPAN, but when utilizing it I get the error "Invalid option 'passphrase' or bad combination of options at sftpTest.pl line 15".

It seems that Net::SFTP::Foreign passes the SFTP object's arguments directly to the Net_SSH2 backend and passphrase is not a recognized argument for the Net_SSH2 backend. Is that correct or am I way off base? If that is correct, is there a way to utilize passphrase protected private keys in Perl?

Below is my test script. Also, this has to be platform independent so utilizing the always useful linux command line arguments isn't an option. Thanks for any help.

use Net::SFTP::Foreign; my $host = 'localhost'; my $user = 'sshuser'; my $privateKey = "C:\\id_rsa"; my $file = "C:\\sshuser-results.xml"; my $passphrase = "password"; my $sftp = Net::SFTP::Foreign-> new(host => $host, backend => 'Net_SSH2', user => $user, key_path => $privateKey, passphrase => $passphrase, ); if( not $sftp) { $sftp->die_on_error("Unable to establish SFTP connection"); } elsif($sftp->error) { $sftp->die_on_error("Connect Failed: " .$sftp->status); } else { print "Connected!\n"; if($sftp->get("sshTest.txt")) { print "get success: " . $sftp->status . "\n"; } else{ print "get failed: " . $sftp->error . "\n"; } if($sftp->put($file)){ print "put file success \n"; } else{ print "put failed: " . $sftp->error . "\n"; } }

Comment on Net::SFTP::Foreign and Using Passphrases on Private Keys
Download Code
Re: Net::SFTP::Foreign and Using Passphrases on Private Keys
by mlebel (Hermit) on Apr 27, 2012 at 13:53 UTC

    According to http://search.cpan.org/~rkitover/Net-SSH2-0.44/lib/Net/SSH2.pm#auth_publickey_%28_username,_public_key,_private_key_,_password__%29

    It appears that passwords are supported for public keys.. "auth_publickey ( username, public key, private key , password ) Note that public key and private key are names of files containing the keys! Authenticate using keys and an optional password."

    Also, i'm not sure if this is a typo but your "passphrase => $passphrase," should be "passphrase => $passPhrase,"

    If I am reading the documentation correctly, "passphrase =>" should be "password =>"

    I hope this helps

      thanks for the quick response, but that Net-SSH2 package is different than the backend package I'm utilizing via Net::SFTP::Foreign. The package I'm using is here Net::SFTP::Foreign::Backend::Net_SSH2

      You're correct in that it was a typo, sorry about that. And if I was using the Net_SSH2 package, it would be $password instead of $passphrase. I've attempted to use the Net_SSH2 package before but it doesn't really work on Windows (getpwuid errors) and I need to make sure this code is cross platform.

        that Net-SSH2 package is different than the backend package I'm utilizing via Net::SFTP::Foreign

        No - Net-SSH2 is the backend package that Net::SFTP::Foreign::Backend::Net_SSH2 uses.

        but it doesn't really work on Windows (getpwuid errors)

        Actually, Net-SSH2 now works pretty well on Windows. That 'getpwuid' problem was fixed long ago.

        Cheers,
        Rob
Re: Net::SFTP::Foreign and Using Passphrases on Private Keys
by salva (Monsignor) on Apr 27, 2012 at 14:37 UTC
    I will try to publish a new version of the backend module supporting passphrases over the weekend.

      That is awesome news! Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967629]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-08-01 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls