Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expect hangs with interactive command #31

Open
ansiz opened this issue Apr 2, 2017 · 1 comment
Open

Expect hangs with interactive command #31

ansiz opened this issue Apr 2, 2017 · 1 comment

Comments

@ansiz
Copy link

ansiz commented Apr 2, 2017

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.

@ansiz
Copy link
Author

ansiz commented Apr 3, 2017

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant