Go如何使用ssh的基于主机的认证方式

问题描述

我的远程主机已启用 HostbasedAuthentication 登录

cat /etc/ssh/sshd_config

...
# Example of overriding settings on a per-user basis
#Match User anoncvs
#   X11Forwarding no
#   AllowTcpForwarding no
#   PermitTTY no
#   ForceCommand cvs server

UseDNS no
HostbasedAuthentication yes
IgnoreRhosts no

当我使用命令行登录时,它提示我Authentication succeeded (hostbased)

ssh -v 192.168.1.2

...
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password,hostbased
debug1: Next authentication method: hostbased
debug1: userauth_hostbased: trying hostkey ecdsa-sha2-nistp256 SHA256:nzCIDPxLBPF1qhdmY8yrrZ3DmFzc76oM1Jcx1+BlPRLp
debug1: Authentications that can continue: publickey,hostbased
debug1: userauth_hostbased: trying hostkey ssh-ed25519 SHA256:DPxLBlPRLp+nzCID+Bbz9KpF1qhdaYJ0oM1JrrZ3Dm
debug1: Authentication succeeded (hostbased).
Authenticated to 192.168.1.2 ([192.168.1.2]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
...

但是当我使用 golang.org/x/crypto/ssh

登录时出现错误
package main

import (
    "errors"
    "fmt"
    "os"

    "golang.org/x/crypto/ssh"
    "golang.org/x/crypto/ssh/knownhosts"
)

var addr = "192.168.1.2:22"

func main() {
    key,err := os.ReadFile("/etc/ssh/ssh_host_ed25519_key")
    if err != nil {
        panic("read key error," + err.Error())
    }
    signer,err := ssh.ParsePrivateKey(key)
    if err != nil {
        panic("ssh parse private key error," + err.Error())
    }

    check,err := knownhosts.New("/etc/ssh/ssh_known_hosts")
    if err != nil {
        panic(fmt.Errorf("knownhosts error,%s",err.Error()))
    }

    config := &ssh.ClientConfig{
        User:            "root",HostKeyCallback: check,Auth:            []ssh.AuthMethod{ssh.PublicKeys(signer)},}
    client,err := ssh.Dial("tcp",addr,config)
    if err != nil {
        panic(errors.New("ssh new client conn error," + err.Error()))
    }
    _ = client.Close()
}

输出:

[root@arm_linux ~]# ./ssh-test 
panic: ssh new client conn error,ssh: handshake failed: ssh: unable to authenticate,attempted methods [none publickey],no supported methods remain

goroutine 1 [running]:
main.main()
    D:/project/test/main.go:38 +0x354

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...