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

I am involved in a FTP to SFTP migration and have chosen Net::SFTP::Foreign as the Perl module to use to accomplish this. I have successfully modified all Perl scripts to this effect. Now another curve ball has been thrown my way ... now SSH2 (RSA) public key authentication is required. I have created the key files and passed along the public key . I am trying to use "key_path". I have changed the code from

%args = ( "user" => $remuser, "password" => $rempswd ); $sftp = Net::SFTP::Foreign->new( $remnode,%args, stderr_discard => 1 ) +;

for testing this to

$sftp = Net::SFTP::Foreign->new( $remnode ,key_path => '/export/home/secbatch/.ssh/id_rsa' , stderr_discard => 1);

After each call to $sftp = Net::SFTP::Foreign->new there is

if ( $sftp->error ) { print "Connect Failed!"; print "\n$@"; ##if $DEBUG; print "\nConnect error: " . $sftp->error . "\n"; print "\nConnect status: " . $sftp->status . "\n";

My log files show this

Connecting to Remote System: ... Connect Failed!

Connect error: Connection to remote server is broken

Connect status: Connection lost

Here is the dir

/export/home/secbatch/.ssh>% ls -l

total 34

drwx------ 2 secbatch gsm 1024 Feb 25 14:04 .

drwxr-xr-x 75 secbatch gsm 6144 Feb 4 17:15 ..

-rw------- 1 secbatch gsm 1675 Feb 25 14:04 id_rsa

-rw-r--r-- 1 secbatch gsm 403 Feb 25 14:04 id_rsa.pub

In the META::CPAN page for Net::SFTP::Foreign there is this note ... "Note also that latest versions of Net::SFTP::Foreign support the key_path argument".

I am successful when I try from command line with sftp -v username@remote host

.

A snippet from output ...

debug1: Authentications that can continue: publickey,password,keyboard-interactive

debug1: Next authentication method: publickey

debug1: Offering RSA public key: /export/home/secbatch/.ssh/id_rsa

debug1: Server accepts key: pkalg ssh-rsa blen 279

debug1: read PEM private key done: type RSA

debug1: Authentication succeeded (publickey).

Any guidance would be greatly appreciated.

Replies are listed 'Best First'.
Re: Net::SFTP::Foreign key_path
by tangent (Parson) on Feb 26, 2016 at 01:33 UTC
    Try adding more=>'-v' to the arguments to get verbose output:
    my $sftp = Net::SFTP::Foreign->new($host, %args, more => '-v');
Re: Net::SFTP::Foreign key_path
by salva (Canon) on Feb 26, 2016 at 07:37 UTC
    What do you get when you enable debugging?
    $Net::SFTP::Foreign::debug = -1;
      $sftp = Net::SFTP::Foreign->new( $remnode,key_path => '/export/home/secbatch/.ssh/id_rsa', stderr_discard => 1, more => '-v', );

      and added $Net::SFTP::Foreign::debug = -1;

      Output is ....

      20160226135158 Connecting to Remote System: sftp.bloomberg.com ... #2997 1456512718.00000 new: This is Net::SFTP::Foreign 1.81

      #2997 1456512718.00000 new: Loaded from /appl/perl_5.12.3/lib/site_perl/5.12.3/Net/SFTP/Foreign.pm

      #2997 1456512718.00000 new: Running on Perl v5.12.3 for solaris

      #2997 1456512718.00000 new: debug set to -1

      #2997 1456512718.00000 new: ~0 is 4294967295

      #2997 1456512718.00000 new: Using backend Net::SFTP::Foreign::Backend::Unix 1.76_03

      #2997 1456512718.00000 autodisconnect: setting disconnecting pid to 2997 and thread to 1

      #2997 1456512718.00000 _init_transport: ssh cmd: ssh -i /export/home/secbatch/.ssh/id_rsa -o PreferredAuthentications=publickey -v sftp.bloomberg.com -s sftp

      #2997 1456512718.00000 _queue_msg: queueing msg len: 5, code:1, id:3 ... 1

      00 00 00 05 01 00 00 00 03 | .........

      #2997 1456512718.00000 _get_msg: waiting for message... 1

      #2997 1456512718.00000 _do_io: _do_io connected: 1

      #2997 1456512718.00000 _do_io: _do_io select(-,-,-, undef)

      #2997 1456512718.00000 _do_io: _do_io write queue: 9, syswrite: 9, max: 65536, $!:

      00 00 00 05 01 00 00 00 03 | .........

      #2997 1456512718.00000 _do_io: _do_io select(-,-,-, undef)

      #2997 1456512718.00000 _do_io: _do_io read sysread: 0, total read: 0, $!:

      #2997 1456512718.00000 _conn_lost: _conn_lost

      #2997 1456512718.00000 _set_status: _set_status code: 7, str: Connection lost

      #2997 1456512718.00000 _set_error: _set_err code: 37, str: Connection to remote server is broken

      #2997 1456512718.00000 _conn_lost: _conn_lost

      Connect Failed!

      Connect error: Connection to remote server is broken

      Connect status: Connection lost

        do not discard the stderr output so that we can also see what the ssh process reports.