7

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Can I know which versions you using now?
1

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

4 Comments

Thank you for your input. I'm using Windows, my private key is called "my_key.pem" and is in the same folder as my program so using ~/.ssh/id_rsa just throws an error that this path doesn't exist. After tweaking your code a bit to make it use the public key in my directory it still throws an error ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain. Like I said, my code works just fine on my laptop so I think it must be an issue with my computer.
can you ssh user@host ?
Nope, I get Permission denied (publickey).
I waited some time, looking for answers, ran it again and it worked. Now I made another folder, copied and pasted exactly the same code and it didn't work. What is going on?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.