diff --git a/client/cmd/ssh.go b/client/cmd/ssh.go index b8ae20b9f..999ec6536 100644 --- a/client/cmd/ssh.go +++ b/client/cmd/ssh.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "github.com/netbirdio/netbird/client/internal" - "github.com/netbirdio/netbird/client/proto" nbssh "github.com/netbirdio/netbird/client/ssh" "github.com/netbirdio/netbird/util" log "github.com/sirupsen/logrus" @@ -57,34 +56,6 @@ var sshCmd = &cobra.Command{ ctx := internal.CtxInitState(cmd.Context()) - conn, err := DialClientGRPCServer(ctx, daemonAddr) - if err != nil { - return fmt.Errorf("failed to connect to daemon error: %v\n"+ - "If the daemon is not running please run: "+ - "\nnetbird service install \nnetbird service start\n", err) - } - - defer func() { - err := conn.Close() - if err != nil { - log.Warnf("failed closing dameon gRPC client connection %v", err) - return - } - }() - client := proto.NewDaemonServiceClient(conn) - - status, err := client.Status(ctx, &proto.StatusRequest{}) - if err != nil { - return fmt.Errorf("unable to get daemon status: %v", err) - } - - if status.Status != string(internal.StatusConnected) { - // todo maybe automatically start it? - cmd.Printf("You are disconnected from the NetBird network. Please run the UP command first to connect: \n\n" + - " netbird up \n\n") - return nil - } - config, err := internal.ReadConfig("", "", configPath, nil) if err != nil { return err @@ -95,7 +66,8 @@ var sshCmd = &cobra.Command{ sshctx, cancel := context.WithCancel(ctx) go func() { - if err := runSSH(sshctx, host, []byte(config.SSHKey)); err != nil { + // blocking + if err := runSSH(sshctx, host, []byte(config.SSHKey), cmd); err != nil { log.Print(err) } cancel() @@ -111,10 +83,15 @@ var sshCmd = &cobra.Command{ }, } -func runSSH(ctx context.Context, addr string, pemKey []byte) error { +func runSSH(ctx context.Context, addr string, pemKey []byte, cmd *cobra.Command) error { c, err := nbssh.DialWithKey(fmt.Sprintf("%s:%d", addr, port), user, pemKey) if err != nil { - return err + cmd.Printf("Error: %v\n", err) + cmd.Printf("Couldn't connect. " + + "You might be disconnected from the NetBird network, or the NetBird agent isn't running.\n" + + "Run the status command: \n\n" + + " netbird status\n\n") + return nil } go func() { <-ctx.Done() diff --git a/client/ssh/client.go b/client/ssh/client.go index 486a8f803..29ebb2481 100644 --- a/client/ssh/client.go +++ b/client/ssh/client.go @@ -6,6 +6,7 @@ import ( "golang.org/x/term" "net" "os" + "time" ) // Client wraps crypto/ssh Client to simplify usage @@ -92,7 +93,8 @@ func DialWithKey(addr, user string, privateKey []byte) (*Client, error) { } config := &ssh.ClientConfig{ - User: user, + User: user, + Timeout: 5 * time.Second, Auth: []ssh.AuthMethod{ ssh.PublicKeys(signer), },