Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

when using goroutines, the connection is timed out #72

Open
GrimAnEye opened this issue Sep 1, 2021 · 0 comments
Open

when using goroutines, the connection is timed out #72

GrimAnEye opened this issue Sep 1, 2021 · 0 comments

Comments

@GrimAnEye
Copy link

I am having a problem when using goroutines. If i make connections one by one - the connections are successful, I get the data.
But if I run many goroutines at the same time, then they freeze and die by timeout. And I tracked down - freezes occur after sending the password. What am I doing wrong?

import (
	"fmt"
	"log"
	"regexp"
	"sync"
	"time"
	expect "github.com/google/goexpect"
)
const (
	timeout = 30 * time.Second
)
var (
	userRE   = regexp.MustCompile("UserName:")
	passRE   = regexp.MustCompile("PassWord:")
	promptRE = regexp.MustCompile("admin#")

	list []string = []string{"dlink1.domain.com",
                                         "dlink2.domain.com",
                                         "dlink3.domain.com",
                                         "dlink4.domain.com"
                                         }
)

func main() {
	log.Println("Telnet example start")

	// Running multiple goroutines
	var wg sync.WaitGroup
	wg.Add(len(list))

	for _, router := range list {
        // the problem occurs here
	/* go */	CallToRouter("username", "pass", router, "1a:1a:1a:1a:1a:1a", searcher, &wg)
	}
	wg.Wait()
	log.Println("Done!")

}

func CallToRouter(user, pass, router, macForSearch string, regexp *regexp.Regexp, wg *sync.WaitGroup) {
	defer wg.Done()

	// Connect to router
	e, _, err := expect.Spawn(fmt.Sprintf("telnet %s", router), -1)
	if err != nil {
		log.Panic(err)
		return
	}
       defer e.Close()

	// Authorizer, exec command, exit
	_, _, err = e.Expect(userRE, timeout)
	if err != nil {
		log.Panic(err)
		return
	}
	err = e.Send(user + "\n")
	if err != nil {
		log.Panic(err)
		return
	}
	_, _, err = e.Expect(passRE, timeout)
	if err != nil {
		log.Panic(err)
		return
	}
	err = e.Send(pass + "\n")
	if err != nil {
		log.Panic(err)
		return
	}
	_, _, err := e.Expect(promptRE, timeout)
	if err != nil {
		log.Panic(err)
		return
	}

	err = e.Send("sh fdb mac " + macForSearch + "\n")
	if err != nil {
		log.Panic(err)
		return
	}
	result, _, _ := e.Expect(promptRE, timeout)
	if err != nil {
		log.Panic(err)
		return
	}
	err = e.Send("logout\n")
	if err != nil {
		log.Panic(err)
		return
	}

	log.Printf("%s:\n%s", router, result)
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant