in reply to Why does Net::SFTP and Net::SSH::Perl not use "known_hosts"

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.