Skip to content

Commit

Permalink
Improved SSH Hostkey error logging
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Lamp <[email protected]>
  • Loading branch information
modzilla99 committed Jul 24, 2024
1 parent 4f40866 commit 2d5a9c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 8 additions & 4 deletions cmd/osssh/osssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ type Process interface {
Close() error
}

var remotePids []int
var hypervisor string
var (
remotePids []int
hypervisor string
username string
)

func main() {
args := utils.ParseArgs()
username = args.Username
setupCleanup()
osc, err := openstack.CreateClient()
if err != nil {
Expand All @@ -46,7 +50,7 @@ func main() {
}
}

func run (info *openstack.Info, args generic.Args) error{
func run (info *openstack.Info, args generic.Args) error {
fmt.Print("Connecting to SSH...")
c, err := ssh.NewClient(hypervisor, args.Username)
if err != nil {
Expand Down Expand Up @@ -81,7 +85,7 @@ func run (info *openstack.Info, args generic.Args) error{
}

func cleanup() {
c, err := ssh.NewClient(hypervisor, "jlamp")
c, err := ssh.NewClient(hypervisor, username)
if err != nil {
fmt.Printf("Error creating SSH client: %s\n", err)
panic(err.Error())
Expand Down
6 changes: 5 additions & 1 deletion internal/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ssh
import (
"bytes"
"encoding/base64"
"fmt"
"log"
"net"
"os"
Expand Down Expand Up @@ -50,9 +51,12 @@ func NewClient(hostname string, username string) (*ssh.Client, error) {
// Connect
client, err := ssh.Dial("tcp", net.JoinHostPort(hostname, "22"), config)
if err != nil {
if err.Error() == "ssh: handshake failed: knownhosts: key is unknown" {
fmt.Printf("Error\n\nCould not verify host key of %s, please add it to your known_hosts file by connecting to it with SSH\n", hostname)
os.Exit(1)
}
return nil, err
}

return client, nil
}

Expand Down

0 comments on commit 2d5a9c8

Please sign in to comment.