gpt4 book ai didi

Go SSH key 不适用于 crypto/ssh,但可以手动使用

转载 作者:IT王子 更新时间:2023-10-29 01:41:14 26 4
gpt4 key购买 nike

我使用 crypto/ssh 包生成了 SSH RSA key 对。但是,当我尝试通过 Go 中的脚本使用它时,出现错误:

unable to connect: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

当我通过 CLI 连接到远程设备时,它连接成功:

ssh -i ~/.ssh/test_key_1 username@172.22.4.1

我可能没有正确使用这个包吗?

注意:私钥没有密码。

package main

import (
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
)

func main() {
privateKeyFile := "/Users/username/.ssh/test_key_1"
remoteIP := "172.22.4.1:22"
user := "username"

privateKeyBytes, err := ioutil.ReadFile(privateKeyFile)
if err != nil {
log.Fatal(err)
}

key, err := ssh.ParsePrivateKey(privateKeyBytes)
if err != nil {
log.Fatal(err)
}

config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(key),
},
// using InsecureIgnoreHostKey() for testing purposes
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}

client, err := ssh.Dial("tcp", remoteIP, config)
if err != nil {
log.Fatalf("unable to connect: %v", err)
}
defer client.Close()

fmt.Println("Success!")
}

最佳答案

经过漫长的组件隔离过程后,我终于能够验证我的 key 对未通过身份验证的原因。这是由于我使用的自定义便利包生成的公钥略有偏差。

我在他的未决问题上发帖:

https://github.com/ianmcmahon/encoding_ssh/issues/1

简而言之:

使用 EncodePublicKey() 函数创建的公钥如下:(为简洁起见被截断)

ssh-rsa AAAAB3NzaC1yc2EAAAAEAAEAAQAAAgC2u3I/nbN9jcWDV..

但是,当运行 ssh-keygen -y -f id_rsa 时,会创建以下内容:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC2u3I/nbN9jcWDV...

请注意粗体部分略有不同。这会导致 SSH 身份验证不起作用。

关于Go SSH key 不适用于 crypto/ssh,但可以手动使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47402775/

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