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

I am trying to use Net::SFTP to download a file. I am using public key authentication, but the authentication seems to fail. If I use line command sftp (sftp userid@hostname) I have no problems connecting and sending a file. Both the line command sftp and Net::SFTP::perl are using SSH2. Here is a snipit of my code:
use Net::SFTP; use Net::SFTP::Util qw(fx2txt); %args = ( user => "thisuser", debug => 1, protocol => 2 ); $host = $ARGV[0]; $ftp = Net::SFTP->new($host, %args);
Below is the debug log. It appears that the public key authentication fails so it tries password authentication. Password authentication is not an option for this application. Anyone have any ideas how I can fix this?
server1: Reading configuration data /abc/userid/.ssh/config server1: Reading configuration data /etc/ssh_config server1: Connecting to xxxxxxxx.xxxx.xxx, port 22. server1: Remote version string: SSH-2.0-WS_FTP-SSH_6.1.1 server1: Remote protocol version 2.0, remote software version WS_FTP- +SSH_6.1.1 server1: Net::SSH::Perl Version 1.30, protocol version 2.0. server1: Connection established. server1: Sent key-exchange init (KEXINIT), wait response. server1: Algorithms, c->s: 3des-cbc hmac-sha1 none server1: Algorithms, s->c: 3des-cbc hmac-sha1 none server1: Entering Diffie-Hellman Group 1 key exchange. server1: Sent DH public key, waiting for reply. server1: Received host key, type 'ssh-dss'. server1: Host 'xxxxxxx.xxxx.xxx' is known and matches the host key. server1: Computing shared secret key. server1: Verifying server signature. server1: Waiting for NEWKEYS message. server1: Enabling incoming encryption/MAC/compression. server1: Send NEWKEYS, enable outgoing encryption/MAC/compression. server1: Sending request for user-authentication service. server1: Service accepted: ssh-userauth. server1: Trying empty user-authentication request. server1: Authentication methods that can continue: publickey,password +. server1: Next method to try is publickey. server1: Trying pubkey authentication with key file '/abc/userid/.ssh +/id_dsa' server1: Authentication methods that can continue: publickey,password +. server1: Next method to try is publickey. server1: Next method to try is password. server1: Trying password authentication. server1: Will not query passphrase in batch mode. server1: Login completed, opening dummy shell channel. server1: channel 0: new [client-session] server1: Requesting channel_open for channel 0. server1: channel 0: open confirm rwindow 300000 rmax 30000 server1: channel 1: new [client-session] server1: Requesting channel_open for channel 1. server1: Sending subsystem: sftp server1: Requesting service subsystem on channel 1. server1: channel 1: open confirm rwindow 300000 rmax 30000 server1: sftp: Sending SSH2_FXP_INIT server1: sftp: Remote version: 3
Thanks for any help you can give.

Replies are listed 'Best First'.
Re: Net::SFTP authentication problem
by salva (Canon) on Oct 16, 2008 at 14:09 UTC
    Try using Net::SFTP::Foreign instead.

    It doesn't reimplement the SSH protocol in Perl but uses the SSH client provided by your OS to connect to the server.

      Thanks. That helped. Using SFTP::Foreign I get public key authentication and I can put the file to the remote server. I am getting an error on the put even though the file makes it to the destination: "Couldn't setstat remote file (fsetstat): SSH_FILEXFER_ATTR_PERMISSIONS". I'll do some research and see if I can figure out it out.
        Probably, the server software (WS_FTP-SSH_6.1.1) does not support SFTP setattr operation.

        Try dissabling attribute copy on the put method:

        $sftp->put($local, $remote, copy_perm => 0);
Re: Net::SFTP authentication problem
by Anonymous Monk on Oct 16, 2008 at 14:10 UTC