【问题标题】:Golang SSH client error "unable to authenticate, attempted methods [none publickey], no supported methods remain"Golang SSH客户端错误“无法验证,尝试的方法[无公钥],没有支持的方法”
【发布时间】:2022-06-21 23:28:30
【问题描述】:

由于某种原因,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 中的实例,它运行良好。当然,我尝试在 CMD 和 MobaXTerm 客户端中使用 ssh 命令进行连接 - 两者都有效。我尝试使用其他 Golang SSH 客户端 golang.org/x/crypto/ssh,但它不起作用(同样的错误)。

感谢您的帮助。

【问题讨论】:

    标签: go ssh


    【解决方案1】:

    假设你可以ssh user@host不用密码,公钥可能是~/.ssh/id_rsa~/.ssh/id_ecda

    import "golang.org/x/crypto/ssh"
    
    func DialWithPublickey(addr string, port int, user, publickeyfile string) (*ssh.Client, error) {
        if key, err := ioutil.ReadFile(publickeyfile); err == nil {
            if signer, err := ssh.ParsePrivateKey(key); err == nil {
                config := &ssh.ClientConfig{
                    User: user,
                    Auth: []ssh.AuthMethod{
                        ssh.PublicKeys(signer),
                    },
                    HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, publickey ssh.PublicKey) error { return nil }),
                }
                if client, err := ssh.Dial("tcp", addr+":"+strconv.Itoa(port), config); client != nil && err == nil {
                    client.SendRequest(user+"@"+addr, true, nil) // keep alive
                    return client, nil
                }
                return nil, err
            } else {
                return nil, err
            }
        } else {
            return nil, err
        }
    }
    

    试试DialWithPublickey(host, port, user, "~/.ssh/id_rsa")

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 2017-04-28
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多