From e5b3017792c5743f093cc6c19db4a575d6002acb Mon Sep 17 00:00:00 2001 From: revengel Date: Thu, 16 Mar 2023 04:16:16 +0300 Subject: [PATCH] show public ssh key comment --- ssh-add.go | 17 ++++++++++++++++- ssh-agent.go | 3 +-- utils.go | 4 ++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/ssh-add.go b/ssh-add.go index 2782e98..288195c 100644 --- a/ssh-add.go +++ b/ssh-add.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "errors" "fmt" "path/filepath" @@ -428,8 +429,22 @@ func (s *gc) ShowSSHPublicKey(c *cli.Context) error { return err } + pubKey = bytes.SplitN(pubKey, []byte{'\n'}, 2)[0] + var pubKeySpaceParts = bytes.SplitN(pubKey, []byte{' '}, 3) + if len(pubKeySpaceParts) == 3 && bytes.Equal(pubKeySpaceParts[2], []byte("noname")) { + pubKeySpaceParts = pubKeySpaceParts[0:2] + } + + if len(pubKeySpaceParts) < 3 { + var comment = getSSHKeyComment(s.key) + pubKeySpaceParts = append(pubKeySpaceParts, []byte(comment)) + pubKey = bytes.Join(pubKeySpaceParts, []byte{' '}) + } + + var pubKeyStr = string(pubKey) + if !clip { - fmt.Println(string(pubKey)) + fmt.Println(pubKeyStr) return nil } diff --git a/ssh-agent.go b/ssh-agent.go index 096afea..82f1a20 100644 --- a/ssh-agent.go +++ b/ssh-agent.go @@ -4,7 +4,6 @@ import ( "crypto/x509" "encoding/pem" "errors" - "fmt" "net" "os" @@ -48,7 +47,7 @@ func (sa *sshAgent) add(privateKeyB []byte, password, comment string, lifetime u key := agent.AddedKey{ PrivateKey: privateKey, LifetimeSecs: lifetime, - Comment: fmt.Sprintf("%s: %s", appName, comment), + Comment: getSSHKeyComment(comment), } err = sa.agent.Add(key) if err != nil { diff --git a/utils.go b/utils.go index e86806c..f44701f 100644 --- a/utils.go +++ b/utils.go @@ -90,3 +90,7 @@ func askForConfirmation(s string, ss ...any) bool { panic(err) } } + +func getSSHKeyComment(key string) string { + return fmt.Sprintf("%s:%s", "gssh", key) +}