Terminal Asks for Email and Password, how do I Programmatically fill it out (in Ruby)?
I am running a command to push files to Google App Engine, and it might ask me for 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 am running that in Ruby right now using this: %x[appcfg.py update .]
. How can I fill out the email and password? I have 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 set that up without it.
What's the best way to fill out things the command line asks for programmatically?
Another issue is that if I run the command through ruby, I can see output as the command runs, but it never shows me the "Email: " line, it stops here:
Application: my-cdn; version: 3.
S开发者_StackOverflow社区erver: appengine.google.com.
Scanning files on local disk.
Initiating update.
# ... can't see "Email: "
Thanks for the tips.
Generally, 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 require "open3"
first).
However that usually does not work with programs that ask for a password (because they access the terminal directly instead of simply reading from stdin). In that case, you need to use PTY (require "pty"
) instead of open3. PTY.getpty
will spawn a shell and return an array containing the shell's output stream, input stream and pid. You can use those to invoke commands and read their input (be aware that the output you read will also include the shell prompt and the command invocation).
精彩评论