From 20df8b538bb3ffce32fa55d08a9c645b99fb44e1 Mon Sep 17 00:00:00 2001 From: Robert Gerus Date: Sat, 24 Nov 2018 23:18:53 +0100 Subject: [PATCH] Fix specifying multiple hosts Iterator and CredentialConfig returned from range were changed "underneath" the launched goroutine. Fixes #18 --- util/main.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/util/main.go b/util/main.go index 5b46e77..3cb4e72 100644 --- a/util/main.go +++ b/util/main.go @@ -16,7 +16,6 @@ import ( "net/http" "regexp" "strings" - "sync" "time" "golang.org/x/crypto/ssh" @@ -178,19 +177,16 @@ func ParseQuery(w http.ResponseWriter, r *http.Request) (*regexp.Regexp, error) // func BatchExecute(c *Config, p *regexp.Regexp) (Config, error) { - var done sync.WaitGroup t := make(chan bool) for i, v := range c.Scripts { if p.MatchString(v.Name) != true { c.Scripts[i].Ignored = true } else { - go executeScript(v.Script, v.Pattern, &c.Scripts[i].Credentials, &done, t) + go executeScript(v.Script, v.Pattern, &c.Scripts[i].Credentials, t) } } - done.Wait() - for _, v := range c.Scripts { if !v.Ignored { for _, _ = range v.Credentials { @@ -271,13 +267,13 @@ func adjustConfig(c Config) (Config, error) { // // TLDR executeScript runs the given script in parallel on all hosts. // -func executeScript(script, pattern string, creds *[]CredentialConfig, done *sync.WaitGroup, t chan bool) { +func executeScript(script, pattern string, creds *[]CredentialConfig, t chan bool) { match, _ := regexp.Compile(pattern) - for i, c := range *creds { - done.Add(1) - go func() { + for ri, rc := range *creds { + go func(i int, c CredentialConfig) { + result, status, err := executeScriptOnHost(c.Host, c.Port, c.User, c.KeyFile, script) (*creds)[i].ScriptReturnCode = status @@ -294,9 +290,7 @@ func executeScript(script, pattern string, creds *[]CredentialConfig, done *sync } t <- true - - done.Done() - }() + }(ri, rc) } }