Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

SFTP connection failure with Perl “Net::SFTP” module

by Michaels (Novice)
on May 06, 2019 at 07:54 UTC ( [id://1233379]=perlquestion: print w/replies, xml ) Need Help??

Michaels has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to connect to SFTP server using Perl but I get the following connection error:

Permission denied at /app/perl-5.24.3/lib/site_perl/5.24.3/Net/SFTP.pm line 63.

My connection code 'sftp_test2.pl':
use strict; use warnings; use Net::SFTP; my $server = 'downloads-server'; my $user = 'user'; my $port = "10022"; my $password = ''; my %args = ( user => "$user", password => '', port => "$port", ssh_args => { user => "$user", password => '', identity_files => [ 'path/sftp_download'], port => "$port", protocol=>'2,1', debug => 1, } ); my $sftp=Net::SFTP->new($server, %args) or die "could not open con +nection to $server\n";
execution:
$>perl sftp_test2.pl Reading configuration data path/home/.ssh/config Reading configuration data /etc/ssh_config Connecting to downloads-server, port 10022. Remote version string: SSH-2.0-CrushFTPSSHD Remote protocol version 2.0, remote software version CrushFTPSSHD Net::SSH::Perl Version 2.14, protocol version 2.0. No compat match: CrushFTPSSHD. Connection established. Sent key-exchange init (KEXINIT), waiting for response. Using diffie-hellman-group-exchange-sha256 for key exchange Host key algorithm: ssh-rsa Algorithms, c->s: aes256-ctr hmac-sha2-512-etm@openssh.com none Algorithms, s->c: aes256-ctr hmac-sha2-512-etm@openssh.com none Entering Diffie-Hellman Group Exchange. SSH2_MSG_KEX_DH_GEX_REQUEST(2048<4096<8192) sent Sent DH Group Exchange request, waiting for reply. Received 4096 bit DH Group Exchange reply. Generating new Diffie-Hellman keys. Entering Diffie-Hellman key exchange. Sent DH public key, waiting for reply. Received host key, type 'ssh-rsa'. Host 'downloads-server' is known and matches the host key. Verifying server signature. Send NEWKEYS. Waiting for NEWKEYS message. Enabling encryption/MAC/compression. Sending request for user-authentication service. Service accepted: ssh-userauth. Trying empty user-authentication request. Authentication methods that can continue: password,publickey,keybo +ard-interactive. Next method to try is password. Trying password authentication. Will not query passphrase in batch mode. Authentication methods that can continue: password,publickey,keybo +ard-interactive. Next method to try is password. Trying password authentication. Will not query passphrase in batch mode. Authentication methods that can continue: password,publickey,keybo +ard-interactive. Next method to try is password. Trying password authentication. Will not query passphrase in batch mode. Authentication methods that can continue: password,publickey,keybo +ard-interactive. Next method to try is password. Next method to try is publickey. Publickey: testing agent key 'my-server' Authentication methods that can continue: password,publickey,keybo +ard-interactive. Next method to try is password. Next method to try is publickey. Publickey: testing agent key 'path/home/.ssh/id_rsa' Authentication methods that can continue: password,publickey,keybo +ard-interactive. Next method to try is password. Next method to try is publickey. Trying pubkey authentication with key file 'path/sftp_download' Authentication methods that can continue: password,publickey,keybo +ard-interactive. Next method to try is password. Next method to try is publickey. Permission denied at /app/perl-5.24.3/lib/site_perl/5.24.3/Net/SFT +P.pm line 63.
I thought that it is related to my private key, but I can connect via unix command:
> sftp -oPort=10022 -oIdentityFile=path/sftp_download user@downloads-s +erver <br>Connecting to downloads-server... <br>sftp>
I tried to find a solution in many articles and also tried few code variations, but without a success. I suspected that it may be related to multiple connection failing attempts with password so I removed the password authentication from "%AUTH_MAP" in the file "app/perl-5.24.3/lib/site_perl/5.24.3/x86_64-linux/Net/SSH/Perl/AuthMgr.pm" and it connected as expected!

As I cannot edit Perl modules, I have to find other solution. Is there a way to force using only/first key authentication instead of password?

Thanks in advance! Mike

Replies are listed 'Best First'.
Re: SFTP connection failure with Perl “Net::SFTP” module
by poj (Abbot) on May 06, 2019 at 09:27 UTC

    Try

    ssh_args => { user => $user, options => [ 'PasswordAuthentication no' ], identity_files => [ 'path/sftp_download'], port => $port, protocol =>'2,1', debug => 1, }
    poj
      Thank you very much !!! It solved the problem! I already tried to use 'PasswordAuthentication no' but differently. Thanks! Mike
Re: SFTP connection failure with Perl “Net::SFTP” module
by thanos1983 (Parson) on May 06, 2019 at 08:16 UTC

    Hello Michaels,

    I would suggest to comment out the identity_files and also password. Simply do this:

    #!/usr/bin/perl use strict; use warnings; use Net::SFTP; my $host = '127.0.0.1'; my %args = ( user => 'user', ssh_args => [ port => "10022" ] ); my $sftp = Net::SFTP->new($host, %args); my $ls = $sftp->ls('/sample/path'); print "$_->{filename}\n" for (@$ls);

    I would also suggest to use the module Net::SFTP::Foreign. You can find more information as to why on a previoudly asked question Re: Using Net::SFTP.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Thank you thanos1983! But how can I point to the private key file if I'll remove 'identity_files'? BR, Michael

        Hello Michaels,

        If you do not use default path of you SSH keys then you can use the option identity_files but you need to remove the password.

        Let me know if it works for you.

        BR / Thanos

        Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 09:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found