views:

278

answers:

4

I am creating a bash 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?

+1  A: 

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
Bertrand Marron
+3  A: 

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)

hop
After perusing the man files I learned how to pass the required fields either as command line params or in a config file. Thanks.
StackedCrooked
A: 

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).

Marcelo Cantos
A: 

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).

David Gelhar