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

How do I automate inputs to interactive programs? #143

Open
danielporto opened this issue Nov 28, 2022 · 4 comments
Open

How do I automate inputs to interactive programs? #143

danielporto opened this issue Nov 28, 2022 · 4 comments

Comments

@danielporto
Copy link

Every so often, I need to automate tasks that need to provide answers to interactive programs
For instance, to simulate user's sessions or just because the program does not provide
a non-interactive interface.
Normally, I use "expect" for that (ex: https://phoenixnap.com/kb/linux-expect)

How can the same feature be implemented with go script?
Thanks

@bitfield
Copy link
Owner

bitfield commented Nov 28, 2022

If the program reads from standard input, then running it via Exec will pass it the contents of the pipe as though the user had typed it on the terminal:

script.Echo("hello").Exec("cat").Stdout()
// Output:
// hello

@bitfield
Copy link
Owner

Note that this probably won't work for programs which ignore their process's configured standard input stream, and read directly from the terminal device, as some do to avoid echoing characters (when typing passwords, for example). If you have a use case like that, I'd be interested to hear about it.

@ryan-n-may
Copy link
Contributor

Hello,

script.Echo("Password").Exec("sudo apt-get update").Tee().Wait()

Doesn't seem to work for sudo password. Maybe this is a good thing, however, this operation doesn't work either:

script.Echo("Y").Exec("sudo apt-get remove python3").Tee().Wait()

Is this an error on my part or an example of a program reading directly from the terminal device?

@bitfield
Copy link
Owner

I think the latter; try sudo -S (which tells it to read from stdin).

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

3 participants