From 13dc498b6e2c10af5a0fe45288d06f9fc9cb3571 Mon Sep 17 00:00:00 2001 From: Matteo Gaggiano Date: Fri, 1 Dec 2023 09:41:34 +0100 Subject: [PATCH] add support for -o parameters to be passed to ssh final command --- cmd/cmd.go | 7 ++++++- commands/commands.go | 10 +++++++++ ssh/config.go | 48 +++++++++++++++++++++++--------------------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 3728296..1cdf55b 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -32,6 +32,11 @@ func main() { app.HelpName = "help" app.Flags = []cli.Flag{ + cli.BoolTFlag{ + Name: "non-strict", + EnvVar: "SSHED_NON_STRICT", + Usage: "disable strict mode, does not check the commented hosts only", + }, cli.StringFlag{ Name: "keychain", EnvVar: "SSHED_KEYCHAIN", @@ -60,7 +65,7 @@ func main() { } var err error - ssh.Config, err = ssh.Parse(context.String("config")) + ssh.Config, err = ssh.Parse(context.String("config"), context.IsSet("non-strict") && context.Bool("non-strict") == true)) if err != nil { return err } diff --git a/commands/commands.go b/commands/commands.go index c1424a0..29e28a2 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -156,6 +156,15 @@ func (cmds *Commands) createCommand(c *cli.Context, srv *host.Host, options *opt args = append(args, cmds.bin) args = append(args, fmt.Sprintf("-F %s", ssh.Config.Path)) + if len(srv.Options) > 0 { + for k, v := range srv.Options { + if options.verbose == true { + fmt.Printf("%s: %s\r\n", ansi.Color(k, "green"), v) + } + args = append(args, fmt.Sprintf("-o %s=%s", k, v)) + } + } + if pk := srv.PrivateKey(); pk != "" { tf, err := ioutil.TempFile("", "") defer os.Remove(tf.Name()) @@ -192,6 +201,7 @@ func (cmds *Commands) createCommand(c *cli.Context, srv *host.Host, options *opt args = append(args, fmt.Sprintf("-i %s", srv.IdentityFile)) } + if options.verbose == true { args = append(args, "-v") } diff --git a/ssh/config.go b/ssh/config.go index e44ad16..6114d9c 100644 --- a/ssh/config.go +++ b/ssh/config.go @@ -32,7 +32,7 @@ type sshConfig struct { cfg *ssh_config.Config } -func Parse(path string) (conf *sshConfig, err error) { +func Parse(path string, nonStrict bool) (conf *sshConfig, err error) { conf = &sshConfig{Path: path} if _, err := os.Stat(conf.Path); os.IsNotExist(err) == false { @@ -48,32 +48,34 @@ func Parse(path string) (conf *sshConfig, err error) { } for _, h := range conf.cfg.Hosts { - for _, pattern := range h.Patterns { - if maskPatternRegexp.MatchString(pattern.String()) == false { - conf.Hosts = append(conf.Hosts, pattern.String()) + if h.EOLComment == " -- added by sshed" || nonStrict == true { + for _, pattern := range h.Patterns { + if maskPatternRegexp.MatchString(pattern.String()) == false { + conf.Hosts = append(conf.Hosts, pattern.String()) + } } - } - for _, node := range h.Nodes { - switch node.(type) { - case *ssh_config.KV: - key := node.(*ssh_config.KV).Key - if key != "IdentityFile" { - continue - } + for _, node := range h.Nodes { + switch node.(type) { + case *ssh_config.KV: + key := node.(*ssh_config.KV).Key + if key != "IdentityFile" { + continue + } - path = node.(*ssh_config.KV).Value - exists := false - for _, v := range conf.Keys { - v = convertTilde(v) - path = convertTilde(path) - if v == path { - exists = true - break + path = node.(*ssh_config.KV).Value + exists := false + for _, v := range conf.Keys { + v = convertTilde(v) + path = convertTilde(path) + if v == path { + exists = true + break + } + } + if exists == false { + conf.Keys = append(conf.Keys, path) } - } - if exists == false { - conf.Keys = append(conf.Keys, path) } } }