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

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

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

这是我的代码:

  1. package main
  2. import (
  3. "fmt";
  4. "github.com/helloyi/go-sshclient"
  5. )
  6. func main() {
  7. client, err := sshclient.DialWithKey("ip:port", "ubuntu", "my_key.pem")
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. out, err := client.Cmd("help").Output()
  13. if err != nil {
  14. fmt.Println(err)
  15. return
  16. }
  17. fmt.Println(string(out))
  18. }

有趣的是,当我在另一台计算机上运行此代码时,连接成功且没有任何错误。所以我认为这一定是计算机的问题,而不是我的代码问题。我还尝试使用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:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/helloyi/go-sshclient"
  5. )
  6. func main() {
  7. client, err := sshclient.DialWithKey("ip:port", "ubuntu", "my_key.pem")
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. out, err := client.Cmd("help").Output()
  13. if err != nil {
  14. fmt.Println(err)
  15. return
  16. }
  17. fmt.Println(string(out))
  18. }

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

  1. import "golang.org/x/crypto/ssh"
  2. import "io/ioutil"
  3. import "strconv"
  4. func DialWithPublickey(addr string, port int, user, publickeyfile string) (*ssh.Client, error) {
  5. key, err := ioutil.ReadFile(publickeyfile)
  6. if err != nil {
  7. return nil, err
  8. }
  9. signer, err := ssh.ParsePrivateKey(key)
  10. if err != nil {
  11. return nil, err
  12. }
  13. client, err := ssh.Dial("tcp", addr+":"+strconv.Itoa(port), &ssh.ClientConfig{
  14. User: user,
  15. Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
  16. HostKeyCallback: ssh.HostKeyCallback(func(string, net.Addr, ssh.PublicKey) error { return nil }),
  17. })
  18. if client == nil || err != nil {
  19. return nil, err
  20. }
  21. client.SendRequest(user+"@"+addr, true, nil) // keep alive
  22. return client, nil
  23. }
  24. 尝试 `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

  1. import "golang.org/x/crypto/ssh"
  2. import "io/ioutil"
  3. import "strconv"
  4. func DialWithPublickey(addr string, port int, user, publickeyfile string) (*ssh.Client, error) {
  5. key, err := ioutil.ReadFile(publickeyfile)
  6. if err != nil {
  7. return nil, err
  8. }
  9. signer, err := ssh.ParsePrivateKey(key)
  10. if err != nil {
  11. return nil, err
  12. }
  13. client, err := ssh.Dial("tcp", addr+":"+strconv.Itoa(port), &ssh.ClientConfig{
  14. User: user,
  15. Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
  16. HostKeyCallback: ssh.HostKeyCallback(func(string, net.Addr, ssh.PublicKey) error { return nil }),
  17. })
  18. if client == nil || err != nil {
  19. return nil, err
  20. }
  21. client.SendRequest(user+"@"+addr, true, nil) // keep alive
  22. return client, nil
  23. }

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:

确定