gexpect
gexpect copied to clipboard
Expect hangs with interactive command
Program hangs when I try to execute Expect with unexpected string
child, err := gexpect.Spawn("passwd test")
if err != nil {
log.Fatal(err)
}
err = child.Expect("YOU WILL NEVER FIND ME")
if err != nil {
log.Println("should print error", err) // should print error, but it never execute
}
I want to do this because there could be different results for one command. For example, ssh $hostname ls /tmp
may output the directory information of tmp
directly if the ssh public key has copied to the host, or get the prompt for the password, I found it only works well with non-interactive command.
I found ExpectTimeout is a way to do this, but it still hangs with child.Wait(), it seems I must use child.Close() to kill the process, here is the modified code:
child, err := gexpect.Spawn("passwd test")
if err != nil {
log.Fatal(err)
}
err = child.ExpectTimeout("YOU WILL NEVER FIND ME", time.Second*1)
if err != nil {
log.Println("should print error", err)
child.Close()
}
child.Wait()
Is there a better way to do it?