func main() { waitChan := make(chan string) fmt.Printf("Starting screen.. \n") child, err := gexpect.Spawn("screen") if err != nil { panic(err) } sender, reciever := child.AsyncInteractChannels() go func() { waitString := "" count := 0 for { select { case waitString = <-waitChan: count++ case msg, open := <-reciever: if !open { return } fmt.Printf("Recieved: %s\n", msg) if strings.Contains(msg, waitString) { if count >= 1 { waitChan <- msg count -= 1 } } } } }() wait := func(str string) { waitChan <- str <-waitChan } fmt.Printf("Waiting until started.. \n") wait(" ") fmt.Printf("Sending Enter.. \n") sender <- "\n" wait("$") fmt.Printf("Sending echo.. \n") sender <- "echo Hello World\n" wait("Hello World") fmt.Printf("Received echo. \n") }
func main() { fmt.Printf("Starting python.. \n") child, err := gexpect.Spawn("python") if err != nil { panic(err) } fmt.Printf("Expecting >>>.. \n") child.Expect(">>>") fmt.Printf("print 'Hello World'..\n") child.SendLine("print 'Hello World'") child.Expect(">>>") fmt.Printf("Interacting.. \n") child.Interact() fmt.Printf("Done \n") child.Close() }
func main() { log.Printf("Testing Ftp... ") child, err := gexpect.Spawn("ftp ftp.openbsd.org") if err != nil { panic(err) } child.Expect("Name") child.SendLine("anonymous") child.Expect("Password") child.SendLine("*****@*****.**") child.Expect("ftp> ") child.SendLine("cd /pub/OpenBSD/3.7/packages/i386") child.Expect("ftp> ") child.SendLine("bin") child.Expect("ftp> ") child.SendLine("prompt") child.Expect("ftp> ") child.SendLine("pwd") child.Expect("ftp> ") log.Printf("Success\n") }