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

Hi I am using SFTP to connect to a server using private-public key authentication . However I am not able to connect. I am able to connect using bash command

here is the code
Net::SFTP::Foreign->new($ftp_server, port => $ftp_port , user => $ftp_ +username, password => $ftp_password , key_path => $private_key , more + => [ '-v', -o => 'PreferredAuthentications publickey']); here is verbose log debug1: Reading configuration data /home/fds/apfh/.ssh/config debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to 121.241.2.65 [121.241.2.65] port 6010. debug1: Connection established. debug1: identity file /home/fds/apfh/FDSnse_fh/etc/id_rsa type 1 debug1: identity file /home/fds/apfh/FDSnse_fh/etc/id_rsa-cert type -1 debug1: Remote protocol version 2.0, remote software version OpenSSH_5 +.3 debug1: match: OpenSSH_5.3 pat OpenSSH* debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_5.3 debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: server->client aes128-ctr hmac-md5 none debug1: kex: client->server aes128-ctr hmac-md5 none debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP debug1: SSH2_MSG_KEX_DH_GEX_INIT sent debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY debug1: Host '[121.241.2.65]:6010' is known and matches the RSA host k +ey. debug1: Found key in /home/fds/apfh/.ssh/known_hosts:1 debug1: ssh_rsa_verify: signature correct debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: SSH2_MSG_NEWKEYS received debug1: SSH2_MSG_SERVICE_REQUEST sent debug1: SSH2_MSG_SERVICE_ACCEPT received "NOTICE TO USERS" "The system is to be used for AUTHORIZED business purpose +only. All activities on this system are being monitored. Unauthorize +d access to this system may be subject to legal action,and/or pros +ecution" debug1: Authentications that can continue: publickey debug1: Next authentication method: publickey debug1: Offering public key: /home/fds/apfh/FDSnse_fh/etc/id_rsa debug1: Server accepts key: pkalg ssh-rsa blen 277 debug1: read PEM private key done: type RSA debug1: Authentication succeeded (publickey). debug1: channel 0: new [client-session] debug1: Requesting no-more-sessions@openssh.com debug1: Entering interactive session. debug1: Sending environment. debug1: Sending subsystem: sftp debug1: channel 0: free: client-session, nchannels 1 debug1: fd 0 clearing O_NONBLOCK debug1: fd 1 clearing O_NONBLOCK debug1: fd 2 clearing O_NONBLOCK Connection to 121.241.2.65 closed by remote host. Transferred: sent 2392, received 2488 bytes, in 30.4 seconds Bytes per second: sent 78.7, received 81.8 debug1: Exit status -1 ERROR[06/21 13:41:52] fetch_symbology.pl.new:736 Cannot connect to [SF +TP] server, error message: SSH slave exited unexpectedly with error c +ode 255 WARN [06/21 13:41:52] fetch_symbology.pl.new:303 [SFTP] conne +ction attempt to server[121.241.2.65] failed, will retry in 60 second +s!
Any help will be appreciated .

Replies are listed 'Best First'.
Re: Issue in SFTP connection using Net::SFTP::Foreign-
by salva (Canon) on Jun 21, 2016 at 10:51 UTC
      Hi salva. Thanks for looking into it here is the log
      #12546 1466506866.00000 new: This is Net::SFTP::Foreign 1.73 #12546 1466506866.00000 new: Loaded from /export/fds/Linux_RHEL6_x86_6 +4/lang/perl/FDSperl5.12-CPANmodules-5.12-20160408/lib/perl5/Net/SFTP/ +Foreign.pm #12546 1466506866.00000 new: Running on Perl v5.12.5 for linux #12546 1466506866.00000 new: debug set to -1 #12546 1466506866.00000 new: ~0 is 18446744073709551615 #12546 1466506866.00000 new: Using backend Net::SFTP::Foreign::Backend +::Unix 1.73 #12546 1466506866.00000 _init_transport: ssh cmd: ssh -i /home/fds/apf +h/FDSnse_fh/etc/id_rsa -p 6010 -o NumberOfPasswordPrompts=1 -l FASEMD + -o PreferredAuthentications publickey 121.241.2.65 -s sftp #12546 1466506866.00000 _init_transport: starting password authenticat +ion #12546 1466506866.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506867.00000 _init_transport: waiting for data from the pty + to become available "NOTICE TO USERS" "The system is to be used for AUTHORIZED business purpose +only. All activities on this system are being monitored. Unauthorize +d access to this system may be subject to legal action,and/or pros +ecution" #12546 1466506868.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506869.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506870.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506871.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506872.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506873.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506874.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506875.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506876.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506877.00000 _init_transport: waiting for data from the pty + to become available #12546 1466506878.00000 _init_transport: waiting for data from the pty + to become available Connection to 121.241.2.65 closed by remote host.^M #12546 1466506899.00000 _conn_lost: _conn_lost #12546 1466506899.00000 _set_status: _set_status code: 6, str: No conn +ection #12546 1466506899.00000 _set_error: _set_err code: 37, str: SSH slave +exited unexpectedly with error code 255
        First thing you should do is to remove the -o => 'PreferredAuthentications publickey' part that is not really required and may interfere with Net::SFTP::Internals... unless you have some reason to include it!

        Do you know the vendor and version of the remote software? you can just telnet the server at port 22 and it will tell you.