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

Has anyone gotten $ssh2->check_hostkey to work with Strawberry on Win7? After much fooling around I managed to get it to try to run with :

$ssh2->check_hostkey(Net::SSH2::LIBSSH2_HOSTKEY_POLICY_ASK(), "d:/prof +iles/known_hosts.pub") or $ssh2->die_with_error ;

That gets me

The authenticity of host 'shell02.theworld.com' can't be established. Key fingerprint is SHA1:17721d6703c62de34708db3cf40acb788fb755bf. Are you sure you want to continue connecting (yes/no)? Non-blocking Re +adLine is not supported on this architecture at C:/Strawberry/perl/vendor/lib/Ne +t/SSH2.pm line 314.

I've tried both _TOFU and_ADVISORY and they don't die on the check_hostkey but get me a

Authentication failed (username/password) (-18 LIBSSH2_ERROR_PUBLICKEY +_UNRECOGNI ZED) at D:\Desktop\sshtest.pl line 15.

when I try to authenticate. SSH2 did create a new entry in my .pub file

shell02.theworld.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDXGlHigbLsh +tD6CHX0uyIEkywwYP5Gnqlkn1BegYYrmWmgVrhN/XkxCyVqJQPVp6wqBK6fwY7jh/2VOM +mdMx4bPOz8sAqDfz6oubXQNbSCA/30IepqEpUEL24XNNrAtG1z9HPQHUoDEZBLNFoNEya +g6eIQaGFKl+GfF0qmo9Xj9jQ1SlPntmGkJj7O91qL5hVNkt7Zm1tRfiN2zvcDWBYHzzy9 +DROsyv1bPg/ycYtDYg+HZ9boBgZOJnEY5SiMlHd7gtIjQCB3wyap0U901+fOUCOGzKCy+ +1eKd5uDPNFeVrvIR4qgSIaS+YcADQUAfpbT73OLWwp4Ei6H6fsE5vCD (Net::SSH2)

which is different than the key in my .pub file for the host from my ssh client. Dunno quite what to try next.

Replies are listed 'Best First'.
Re: Can't get $ssh2->check_hostkey to work
by thanos1983 (Parson) on Aug 09, 2018 at 07:51 UTC

    Hello BernieC,

    I would suggest to include the Net::SSH2::debug-(-state-) flag it will help you to identify the problem. Then step by step resolve it. I would also suggest to enable the Net::SSH2::blocking-(-flag-)

    On my local node this sample of code works. UnixOS not WindowsOS but it should work for you also (I guess).

    #!/usr/bin/perl use strict; use warnings; use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->debug(1); $ssh2->blocking(1); $ssh2->connect('127.0.0.1', 22) # default port 22 or $ssh2->die_with_error; $ssh2->auth( publickey => "/home/user/.ssh/id_rsa"); $ssh2->check_hostkey('ask') or $ssh2->die_with_error; $ssh2->disconnect(); __END__ $ perl test.pl libssh2_knownhost_init(ss->session) -> 0x11b1a60 Net::SSH2::KnownHosts::DESTROY Net::SSH2::DESTROY object 0x13cf7a0

    Give it a try and let us know the output.

    Hope this helps, BR

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Can't get $ssh2->check_hostkey to work
by syphilis (Archbishop) on Aug 09, 2018 at 11:53 UTC
    $ssh2->check_hostkey(Net::SSH2::LIBSSH2_HOSTKEY_POLICY_ASK(), "d:/prof +iles/known_hosts.pub") or $ssh2->die_with_error ;

    Net::SSH2::LIBSSH2_HOSTKEY_POLICY_ASK() doesn't work for me - complains about the absence of Term::ReadKey.
    But the following works fine for me on Windows 7 with Strawberry Perl so long as I've established a connection to the host:
    $ssh2->check_hostkey('advisory', "C:/Cygwin/home/user/.ssh/known_hosts +") or $ssh2->die_with_error ;
    However, auth_password() still fails for me with that same error about the unknown username/public key combo.
    For authentication I need to use auth_publickey.

    Cheers,
    Rob

      I tried turning on debugging in SSH2 and I'm not sure what it is telling me but there seems to be a hint at the problem.

      d:\Desktop>sshtest libssh2_knownhost_init(ss->session) -> 0x294be38 Net::SSH2::KnownHosts::DESTROY Authentication failed (username/password) (-18 LIBSSH2_ERROR_PUBLICKEY +_UNRECOGNI ZED) at D:\Desktop\sshtest.pl line 13. Net::SSH2::DESTROY object 0x247cf08

      It looks almost as though the problem is that the knownhost info is being destroyed before I get to the auth step. The code is

      my $ssh2 = Net::SSH2->new(debug => 1); $ssh2->connect("shell02.theworld.com") or $ssh2->die_with_error ; $ssh2->check_hostkey('ask', "d:/profiles/known_hosts.pub") or $ssh2->die_with_error ; $ssh2->auth_password($login{user}, $login{password}) or $ssh2->die_with_error;

      As if the check_hostkey works and so doesn't "die" but the fact that it works is lost before I do the auth. It is probably a rough trip, but I guess I should take a look at the code for auth_password and see what it is expecting for its public key check, and then work backwards to see what check_hostkey is supposed to leave behind to make auth_password happy. Everything is so hard...:o)

        I've switched over to Net::SSH::Perl to see if that is any better. I turned on its debugging machinery
        {...} Bernie-7-PC: Host 'shell02.theworld.com' is known and matches the host + key. Bernie-7-PC: Verifying server signature. Bernie-7-PC: Send NEWKEYS. Bernie-7-PC: Waiting for NEWKEYS message. Bernie-7-PC: Enabling encryption/MAC/compression. Bernie-7-PC: Sending request for user-authentication service. Bernie-7-PC: Service accepted: ssh-userauth. Bernie-7-PC: Trying empty user-authentication request. Bernie-7-PC: Authentication methods that can continue: publickey,keybo +ard-inter ctive. Bernie-7-PC: Next method to try is publickey. Permission denied at D:\Desktop\sshtest.pl line 15.
        It appears that the server won't accept a "password"! I don't exactly know why it says "empty authentication" and doesn't seem to try sending my password. The code is simple
        my $ssh = Net::SSH::Perl->new("shell02.theworld.com", protocol => 2, debug=>1, strict_host_key_checking => "no", options => ["PasswordAuthentication yes +"] ) ; $ssh->login($login{user}, $login{password}) ;
        I'll have to read up on what happens with the SSH "user-authentication service" and try to understand what's happening there. I can see why everyone who has been successful with SSH has reverted to using publickey auth..:o)
Re: Can't get $ssh2->check_hostkey to work
by salva (Canon) on Aug 09, 2018 at 07:00 UTC
    Post your code, please!
      Here is the code I'm trying
      use Net::SSH2 ; my ( %login); my $ssh2 = Net::SSH2->new(); $ssh2->connect("shell02.theworld.com") or $ssh2->die_with_error ; $ssh2->check_hostkey(Net::SSH2::LIBSSH2_HOSTKEY_POLICY_ADVISORY(), "d: +/profiles/known_hosts.pub") or $ssh2->die_with_error ; $ssh2->auth_password($login{user}, $login{password}) or $ssh2->die_with_error; $ssh2->auth_ok() ; #### print "Logged in\n" ; ####

      as I posted, it never gets past the "check_hostkey"