I am trying to establish an SFTP connection to a remote server, using SSH keys rather than password authentication. From the command line, this works fine:

$ sftp user@[ipaddress] Connecting to [ipaddress]... sftp> ls [dir1] [dir2] sftp> bye

However, when I try to use either Net::SFTP or Net::SFTP::Foreign, it doesn't seem to be using the SSH keys at all, and is instead failing due to trying to use a password.

Net::SFTP:

my $sftp = Net::SFTP->new('[ipaddress]', user => 'user', password => '', debug => 1, ssh_args => { });

With the debugging turned on, here's what I get:

Reading configuration data /scripts//.ssh/config Reading configuration data /etc/ssh_config Connecting to [ipaddress], port 22. Remote protocol version 2.0, remote software version SSHD Math::BigInt: couldn't load specified math lib(s), fallback to Math::B +igInt::Calc at /usr/lib/perl5/site_perl/5.8.8/Crypt/DH.pm line 6 Net::SSH::Perl Version 1.34, protocol version 2.0. No compat match: SSHD Connection established. Sent key-exchange init (KEXINIT), wait response. Algorithms, c->s: 3des-cbc hmac-sha1 none Algorithms, s->c: 3des-cbc hmac-sha1 none Entering Diffie-Hellman Group 1 key exchange. Sent DH public key, waiting for reply. Received host key, type 'ssh-rsa'. Host '[ipaddress]' is known and matches the host key. Computing shared secret key. Verifying server signature. Waiting for NEWKEYS message. Send NEWKEYS. 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,keyboard- +interactive. Next method to try is password. Trying password authentication. Will not query passphrase in batch mode. Authentication methods that can continue: password,publickey,keyboard- +interactive. Next method to try is password. Trying password authentication. Will not query passphrase in batch mode. Authentication methods that can continue: password,publickey,keyboard- +interactive. Next method to try is password. Trying password authentication. Will not query passphrase in batch mode. Received disconnect message: Too many bad authentication attempts! at /usr/lib/perl5/site_perl/5.8.8/Net/SSH/Perl/AuthMgr.pm line 143

And with Net::SFTP::Foreign

my $sftp = Net::SFTP::Foreign->new('[ipaddress]', user => 'user', port => 22, password => '',  more => [ '-v' ] );

The debugging output this time is:

OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008 debug1: Reading configuration data /scripts//.ssh/config debug1: Applying options for [ipaddress] debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to [ipaddress] [[ipaddress]] port 22. debug1: Connection established. debug1: identity file /scripts/.ssh/hl_xfer type 1 debug1: loaded 1 keys debug1: Remote protocol version 2.0, remote software version SSHD debug1: no match: SSHD debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_4.3 debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: server->client aes128-cbc hmac-sha1 none debug1: kex: client->server aes128-cbc hmac-sha1 none debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<2048<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 '[ipaddress]' is known and matches the RSA host key. debug1: Found key in /scripts/.ssh/known_hosts2:58 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 debug1: Authentications that can continue: password,publickey,keyboard +-interactive debug1: Next authentication method: keyboard-interactive Password Authentication debug1: Authentications that can continue: password,publickey,keyboard +-interactive debug1: Next authentication method: password

At that point, it hangs and I eventually kill it.

If anyone has any clues or insights as to how to repair this, or where it's going wrong, I would be most appreciative.


In reply to passwordless SFTP works from cmd line, not from modules by mritty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.