0

I am trying to generate a private key using keytool command, but it asks me for many details like my key passphrase, my name and my organization details. I want to pass all the inputs in one command.

For an instance:

###SHELL###
$  keytool -genkey -v -keystore TestApp.keystore -alias TestApp -keyalg RSA -keysize 2048 -validity 10000
    Enter passphrase: my  passphrase here
    Repeat the passphrase: my passphrase again
    Enter your full name: my name
    Enter your organization unit: my org. unit
    Enter your organization name: my org. name
    Enter password for keystore (press return for same as key passphrase): i press enter here

   Success!!

$ _  

Here, I want to pipe all these inputs (passphrase, name, etc.) into the STDIN,, so that I shouldn't be asked for these inputs after entering the command.

Is this possible? If yes then please tell how. I am new to Bash scripting so please bear with me :)

2
  • Have you tried (echo "correct horse battery stable"; echo "correct horse battery stable"; echo "Ashish"; echo "Super User"; echo "Stack Exchange"; echo) | keytool -genkey -v -keystore TestApp.keystore -alias TestApp -keyalg RSA -keysize 2048 -validity 10000? Commented Feb 23, 2015 at 21:39
  • 1
    Have you read the fine manual? Commented Feb 23, 2015 at 22:08

2 Answers 2

0

You can use the tool called "xdotool" to act in place of you typing in the commands. For instance if you want to enter the following information into a user input:

1.) First name 2.) Last name 3.) Username 4.) Password 5.) Organization

You can set variables for these things in a script like this example:

#!/bin/bash

FNAME="first name"
LNAME="last name"
UNAME="username"
PWORD="password"
ONAME="Organization name"

## This moves the mouse into the specific screen coordinates.
xdotool mouse move # #

## This types the first name and hits enter.
xdotool type "$FNAME"
xdotool key Return

## This types the last name and hits enter.
xdotool type "$LNAME"
xdotool key Return

## This types the username and hits enter.
xdotool type "$UNAME"
xdotool key Return

## This types the password and hits enter.
xdotool type "$PWORD"
xdotool key Return

## This types the organization and hits enter.
xdotool type "$ONAME"
xdotool key Return

What xdotool does, is sends fake commands as mouse movement and action as well as send keyboard inputs. It is as if you are typing all of this out when you are not. You can set a script such as this to be called each time you are prompted to enter such information. In order to get the screen coordinates you will move the mouse to where ever the window you need to type into is and use this command:

xdotool getmouselocation

This will give you the specific coordinates for that mouse position. Then the...

xdotool click 1

...command will click inside that window and make it your active window so that the script can execute properly. It is a pretty nice little tool that can be used as a macro for any task that requires mouse and keyboard. Just be advised that xdotool isn't pre-installed in all linux distros so you may have to install it first.

0

You can use expect for that. The expect script would look something like this:

```

#!/usr/bin/expect -f
spawn keytool -genkey -v -keystore TestApp.keystore -alias TestApp -keyalg RSA -keysize 2048 -validity 10000
expect "Enter keystore password:"
send "my_passphrase\n"
expect "Re-enter new password:"
send "my_passphrase\n"
#and so on with the other prompts

```

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .