I generated a SSH RSA keypair with the crypto/ssh
package. However, when I try to use it via a script in Go I'm getting the error:
unable to connect: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
When I connect to the remote device via CLI, it connects successfully:
ssh -i ~/.ssh/test_key_1 username@172.22.4.1
Could I possibly be using the package incorrectly?
Note: Private key does NOT have a passphrase.
package main
import (
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
)
func main() {
privateKeyFile := "/Users/username/.ssh/test_key_1"
remoteIP := "172.22.4.1:22"
user := "username"
privateKeyBytes, err := ioutil.ReadFile(privateKeyFile)
if err != nil {
log.Fatal(err)
}
key, err := ssh.ParsePrivateKey(privateKeyBytes)
if err != nil {
log.Fatal(err)
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(key),
},
// using InsecureIgnoreHostKey() for testing purposes
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
client, err := ssh.Dial("tcp", remoteIP, config)
if err != nil {
log.Fatalf("unable to connect: %v", err)
}
defer client.Close()
fmt.Println("Success!")
}