我試圖連接到遠程主機,並檢查文件是否存在 在這個階段,我想只是連接,但我得到了一個錯誤: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%確定我的密鑰是正確,因爲我能夠通過終端 –