我试图连接到远程主机,并检查文件是否存在 在这个阶段,我想只是连接,但我得到了一个错误:SSH握手抱怨缺少主机密钥
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()
}
你hostKey是 ssh.FixedHostKey(hostKey) 我觉得空 hostKey = signer.PublicKey()也许可以帮助。 – bayrinat
@BayRinat有一个进展,现在我得到一个不匹配“2017/08/01 19:15:47无法连接:ssh:handshake failed:ssh:host key mismatch”,而我100%确定我的密钥是正确,因为我能够通过终端 –