Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh: handshake failed: knownhosts: key is unknown exit status 1 #28

Open
kiransterling opened this issue Dec 29, 2021 · 4 comments
Open

Comments

@kiransterling
Copy link

Not able to ssh to EC2 instance . Below is the error
ssh: handshake failed: knownhosts: key is unknown
exit status 1

package main

import (
"fmt"
"log"

"github.com/melbahja/goph"

)

func main() {

// Start new ssh connection with private key.
auth, err := goph.Key("{YOUR_PRIVATE_KEY}", "")
if err != nil {
	log.Fatal(err)
}

client, err := goph.New("ec2-user", "{YOUR_PUBLIC_IP}", auth)
if err != nil {
	log.Fatal(err)
}

// Defer closing the network connection.
defer client.Close()

// Execute your command.
out, err := client.Run("pwd")

if err != nil {
	log.Fatal(err)
}

// Get your output as []byte.
fmt.Println(string(out))

}

@melbahja
Copy link
Owner

That's because the public key is not a known host, the public ip not in known_hosts file or ⚠️ public key mismatch.

Before you can connect to a new host you should trust the host public key first with: goph.AddKnownHost method.

@melbahja
Copy link
Owner

The goph.AddKnownHost it will append a new line in your known_hosts file.

@kiransterling
Copy link
Author

Can you update the code in readme file with goph.AddKnownHost usage.

@Akilan1999
Copy link

You could possibly ignore the KnownHost if you want.
Ex:

_, err = goph.DefaultKnownHosts()

	if err != nil {
		return
	}

	c, err = goph.NewConn(&goph.Config{
		User:     user,
		Addr:     addr,
		Port:     port,
		Auth:     auth,
		Timeout:  goph.DefaultTimeout,
		Callback: ssh.InsecureIgnoreHostKey(),
	})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants