Golang SSH 客户端错误“无法验证,尝试的方法 [无公钥],没有支持的方法仍然存在”

由于某种原因,Golang SSH 客户端无法连接到我的 EC2 实例。它抛出以下错误:

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


这是我的代码:


package main


import (

    "fmt"


    "github.com/helloyi/go-sshclient"

)


func main() {

    client, err := sshclient.DialWithKey("ip:port", "ubuntu", "my_key.pem")

    if err != nil {

        fmt.Println(err)

        return

    }


    out, err := client.Cmd("help").Output()

    if err != nil {

        fmt.Println(err)

        return

    }


    fmt.Println(string(out))

}

有趣的是,当我在我的另一台计算机上运行这段代码时,连接没有任何错误。所以我认为这一定是PC的问题而不是我的代码。我还尝试使用 Paramiko 客户端连接到 Python 中的实例,它工作得很好。当然,我尝试ssh在 CMD 和 MobaXTerm 客户端中使用命令进行连接 - 两者都有效。我尝试使用其他 Golang SSH 客户端golang.org/x/crypto/ssh,但没有成功(同样的错误)。


谢谢您的帮助。


走ssh


摇曳的蔷薇
浏览 1560回答 2
2回答

holdtom

显然,这是go.mod文件的问题。两者都已过时golang.org/x/crypto,golang.org/x/sys一旦我更新它们,它就开始工作了。感谢@kkleejoe 的帮助。

跃然一笑

假设您可以ssh user@host没有密码,公钥可能是~/.ssh/id_rsa或~/.ssh/id_ecdaimport "golang.org/x/crypto/ssh"import "io/ioutil"import "strconv"func DialWithPublickey(addr string, port int, user, publickeyfile string) (*ssh.Client, error) {    key, err := ioutil.ReadFile(publickeyfile)    if err != nil {        return nil, err    }    signer, err := ssh.ParsePrivateKey(key)    if err != nil {        return nil, err    }    client, err := ssh.Dial("tcp", addr+":"+strconv.Itoa(port), &ssh.ClientConfig{        User:            user,        Auth:            []ssh.AuthMethod{ssh.PublicKeys(signer)},        HostKeyCallback: ssh.HostKeyCallback(func(string, net.Addr, ssh.PublicKey) error { return nil }),    })    if client == nil || err != nil {        return nil, err    }    client.SendRequest(user+"@"+addr, true, nil) // keep alive    return client, nil}尝试DialWithPublickey(host, port, user, "~/.ssh/id_rsa")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go