0

I have to get user inputs of multiple domain names until the user is done with inputs. Input can be just one domain name or more than one. and the domain name should be separated by a comma ,.

How can I pass the domain names to the below command. Kindly please help me.

keytool -genkey -keystore tc_keystore.jks -keysize 2048 -keypass password -storepass password -keyalg RSA \
 -dname "CN=domain1.com,OU=Devteam,O=Softech,L=Chicago,ST=IL,C=US" \
 -alias domain1.com -ext san=dns:domain2,domain3,domain4,domain5,domain6,domain7

As I am new to shell script, I am reading a single user input and store it in a variable and call the variable. like the below one. But When it comes to multiple user input that should be separated by comma, I am stuck.

keytool -genkey -keystore $keystore -keysize 2048 -keypass $password -storepass $password -keyalg RSA \
 -dname "CN=$domain1,OU=Devteam,O=Softech,L=Chicago,ST=IL,C=US" \
 -alias $domain1 -ext san=dns:domain2,domain3,domain4,domain5,domain6,domain7
0

1 Answer 1

1

A common way of letting users supply data to a script is via its command-line arguments. In this case, it would, for example, be convenient to call your script with a list of domain names. Since the user needs to provide a password and some other key-related parameter, we could develop a script that takes two options and a list of domains.

./script -p 'password' -k 'keystore' domain1 domain2 domain3 ...

I don't know the keytool utility that you are using, but it seems that a user must supply a list of at least two domains rather than just one. The first domain name is used with the -dname and -alias options, while the -ext option takes the second and any other domain name in its value.

The following code uses the idiomatic way of parsing the command-line options and validates that the keystore and password variables have values. It then calls the keytool utility with the data given by the user.

#!/bin/sh

unset -v keystore password

while getopts k:p: opt; do
        case $opt in
                k)
                        keystore=$OPTARG
                        ;;
                p)
                        password=$OPTARG
                        ;;
                *)
                        echo 'Error' >&2
                        exit 1
        esac
done

shift "$(( OPTIND - 1 ))"

if [ -z "$keystore" ] || [ -z "$password" ]; then
        echo 'Missing keystore (-k) and/or password (-p)' >&2
        exit 1
fi

if [ "$#" -lt 2 ]; then
        echo 'Expecting at least two domain names' >&2
        echo '(a main one, and one for the -ext option)' >&2
        exit 1
fi

domain1=$1
shift

IFS=,

keytool -genkey \
        -keystore "$keystore"   \
        -keysize 2048   \
        -keypass "$password"    \
        -storepass "$password"  \
        -keyalg RSA     \
        -dname "CN=$domain1,OU=Devteam,O=Softech,L=Chicago,ST=IL,C=US"  \
        -alias "$domain1"       \
        -ext "san=dns:$*"

After the first shift statement, the first domain name will be in $1 and the others in $2, $3, and so on. We assign $1 to the domain1 variable and call shift. After the second shift, we may use "$*" with IFS set to a comma to generate a comma-delimited string of the rest of the domain names. We use this in the value for the -ext option.

1
  • 1
    Thank you so much. It is working now.
    – Kumar
    Commented Oct 1, 2021 at 22:12

You must log in to answer this question.

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