Bash scripting keyboard input
I am creating a b开发者_如何学编程ash script for generating certificates. The openssl command that creates a certificate asks for keyboard input. This is every time the same sequence of keys: seven times [ENTER] followed by two times ['y' + ENTER]. How can I do this programmatically?
Update
I was able to reduce eliminate the required keyboard input using the command line parameters:
-config FILE
to specify a config file-passin PWD
and-passout PWD
to specify a password
For more details you can have a look at my experiments. This url is checkout-able with subversion.
One way to simulate user interaction is expect.
With OpenSSL specifically, you could just write a configuration file that does not require any input for the task you want to perform. (see man 5ssl config
)
For a good example of how to script opennssl, see CACerts CSRGenerator script.
Since it's asking for keyboard input, just give it something to read.
I suppose yes | your_script
would work, otherwise you could just write the following sequence to its input:
\n\n\n\n\n\n\ny\ny\n
Expect is pretty good at this kind of thing. Other languages will let you do this (far less conveniently) through their popen-style facilities.
You might be able to pipe the characters in as @tusbar suggests, but tools like openssl may insist on you typing it in (something Expect gets around by setting up pseudo-terminals).
You can also use a here document to include input directly in your bash script:
interactive-program <<LimitString
command #1
command #2
...
LimitString
(But the configuration file option suggested by @hop is still the best idea).
精彩评论