Golang SSH client error "unable to authenticate, attempted methods [none publickey], no supported methods remain"

huangapple go评论101阅读模式
英文:

Golang SSH client error "unable to authenticate, attempted methods [none publickey], no supported methods remain"

问题

由于某种原因,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))
}

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

谢谢你的帮助。

英文:

For some reason Golang SSH client can't connect to my EC2 instance. It throws the following error:
ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

This is my code:

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))
}

What's interesting is that when I ran this code on my other computer the connection was made without any errors. So I think it must be a problem with the PC and not my code. I also tried connecting to the instance in Python using Paramiko client and it worked flawlessly. Of course I tried connecting using ssh command in CMD and MobaXTerm client - both worked. I tried using other Golang SSH client golang.org/x/crypto/ssh and it didn't work (same error).

Thank you for your help.

答案1

得分: 3

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

英文:

Apparently, it was an issue with go.mod file. Both golang.org/x/crypto and golang.org/x/sys were outdated, once I updated them it started working. Thanks @kkleejoe for your help.

答案2

得分: 1

假设您可以无需密码执行 ssh user@host,公钥可能是 ~/.ssh/id_rsa~/.ssh/id_ecda

import "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")`
英文:

assume you can ssh user@host without password, public key may be ~/.ssh/id_rsa or ~/.ssh/id_ecda

import "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
}

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

huangapple
  • 本文由 发表于 2022年6月21日 22:32:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/72702629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定