StackedCrooked
StackedCrooked

Reputation: 35485

Bash scripting keyboard input

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?

Update

I was able to reduce eliminate the required keyboard input using the command line parameters:

For more details you can have a look at my experiments. This url is checkout-able with subversion.

Upvotes: 2

Views: 4920

Answers (4)

user3850
user3850

Reputation:

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.

Upvotes: 5

Bertrand Marron
Bertrand Marron

Reputation: 22210

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

Upvotes: 1

David Gelhar
David Gelhar

Reputation: 27900

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

Upvotes: 0

Marcelo Cantos
Marcelo Cantos

Reputation: 185842

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

Upvotes: 0

Related Questions