My reading of the Net::SSH::Perl documentation says that identity_files => [$rsa_file] is for the id_rsa or similar identity , not for your known hosts file -- ie, the key sent to the remote server for authentication. It mentions UserKnownHostsFile in the options section, so that implies that the following might be what you want (untested):

use Net::SSH::Perl; my $keyfile='~/.ssh/known_hosts'; my $ssh = Net::SSH::Perl->new( $host, options => ["UserKnownHostsFile +$keyfile"] );

If I run Data::Dump::dd on $ssh , it will show the default path to known_hosts2 if I don't have that options parameter, but shows the new path I indicate if I do have that parameter, so I think it's right. (But I don't have a known-working connection for Net::SSH::Perl to test, since I've never used that module and only installed it to look into this question, and couldn't get a connection working in the 5 min I allocated, so I cannot tell you that it will for-sure work.)


update: a few hours later, the following SSCCE works for me

#!perl use 5.012; # strict, // use warnings; use autodie; my $hostname = "obfuscated"; my $username = "obfuscated"; my $known_hosts = '/obfuscated/.ssh/known_hosts'; my ($id_rsa, $id_ecdsa, $id_ed25519) = map '/obfuscated/.ssh/'.$_, qw/ +id_rsa id_ecdsa id_ed25519/; use Net::SSH::Perl; use Data::Dump; my %options = ( # debug => 1, # set this one to help with de +bug protocol => 2, # ssh2 interactive => 1, # ask for password if somethin +g goes wrong strict_host_key_checking => 'yes', # makes sure it actually check +s known hosts options => [ "UserKnownHostsFile $known_hosts", # defines +the user path/name for known_hosts ], identity_files => [$id_rsa, $id_ed25519, $id_ecdsa], # defines +the possible identity_file path strings ); my $ssh = Net::SSH::Perl::->new($hostname, %options) or die "create ss +h: $!"; #dd $ssh; # use this when debugging the +various %options $ssh->login($username) or die "login: $!"; # using id_rsa identificat +ion, at default id_rsa location, or ask for pw if it fails print map { $_//"<undef>\n", "\n----\n"} $ssh->cmd('pwd');

I tried with known_hosts and known_hosts2 and even the made-up known_hosts3 and they all worked -- if they didn't exist, it would prompt if I wanted to save the host key; if it did exist already, it would use that previously-saved host key.


In reply to Re: Why does Net::SFTP and Net::SSH::Perl not use "known_hosts" by pryrt
in thread Why does Net::SFTP and Net::SSH::Perl not use "known_hosts" by misterperl

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.