gpt4 book ai didi

go - SSH 握手提示缺少主机 key

转载 作者:IT王子 更新时间:2023-10-29 00:37:16 24 4
gpt4 key购买 nike

我正在尝试连接到远程主机并检查文件是否存在在这个阶段,我只是尝试连接,但出现错误:

2017/08/01 18:16:39 unable to connect: ssh: handshake failed: ssh: required host key was nil

我试图找出其他人是否遇到了我的问题,但我就是找不到。

我知道我需要在此过程中以某种方式检查 knowns_hosts,但我只是不知道如何...

    var hostKey ssh.PublicKey
// A public key may be used to authenticate against the remote
// server by using an unencrypted PEM-encoded private key file.
//
// If you have an encrypted private key, the crypto/x509 package
// can be used to decrypt it.
key, err := ioutil.ReadFile("/home/user/.ssh/id_rsa")
if err != nil {
log.Fatalf("unable to read private key: %v", err)
}

// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatalf("unable to parse private key: %v", err)
}

config := &ssh.ClientConfig{
User: "user",
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.FixedHostKey(hostKey),
}

// Connect to the remote server and perform the SSH handshake.
client, err := ssh.Dial("tcp", "host.com:22", config)
if err != nil {
log.Fatalf("unable to connect: %v", err)
}
defer client.Close()
}

最佳答案

我建议使用 knownhosts 子包

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

...

hostKeyCallback, err := knownhosts.New("/Users/user/.ssh/known_hosts")
if err != nil {
log.Fatal(err)
}

...

config := &ssh.ClientConfig{
User: "user",
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(signer),
},
HostKeyCallback: hostKeyCallback,
}

这样您就可以避免自己解析 known_hosts...

第,

关于go - SSH 握手提示缺少主机 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45441735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com