Terminal asks for email and password, how do I programmatically fill it in (in Ruby)?
I am running a command to move files to Google App Engine and it can ask me my email and password:
$ appcfg.py update .
Application: my-cdn; version: 3. # printed out
Server: appengine.google.com. # printed out
Scanning files on local disk. # printed out
Initiating update. # printed out
Email: email@gmail.com # now it asks me...
Password:
I run this in Ruby right now, using it: %x[appcfg.py update .]
. How can I fill in my email and password? I've seen something like this with capistrano:
%x[appcfg.py update .] do |channel, stream, data|
channel.send_data "#{yaml['production']['email']}\n" if data =~ /^Email:/
end
... but haven't figured out how to install this without it.
What's the best way to fill in what the program line asks for?
Another problem is that if I run the command through ruby, I can see the output when the command is run, but it never shows me the "Email:" line, it stops here:
Application: my-cdn; version: 3.
Server: appengine.google.com.
Scanning files on local disk.
Initiating update.
# ... can't see "Email: "
Thanks for the advice.
source to share
Typically, you can use Open3.popen3(command) do |input, output, error| ... end
to invoke a command, write to its input stream, and read from its output and error streams (you need to first require "open3"
).
However, this usually does not work with programs that ask for a password (because they access the terminal directly, rather than just reading from stdin). In this case, you need to use PTY ( require "pty"
) instead of open3. PTY.getpty
will spawn the wrapper and return an array containing the wrapper's output stream, input stream and pid. You can use them to invoke commands and read their input (be aware that read output also includes a shell prompt and command invocation).
source to share