Last active
August 15, 2019 11:53
-
-
Save drmason13/b95292ea7a30c1a2c4f15adc3307742d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
######################################## | |
# command line argument helper functions | |
######################################## | |
# display usage | |
usage() | |
{ | |
echo " | |
Usage: $0 -s SUBJECT [-d DAYS] HOSTNAME | |
All options must be specified before HOSTNAME | |
Defaults: | |
SUBJECT=\"/C=GB/L=London/O=IT/CN=\$HOSTNAME\" | |
DAYS=3650" | |
exit 2 | |
} | |
# set a variable and exit with error if it already exists | |
set_variable() | |
{ | |
local varname=$1 | |
shift | |
if [ -z "${!varname}" ]; then | |
eval "$varname=\"$@\"" | |
else | |
echo "Error: $varname already set" | |
usage | |
fi | |
} | |
# set a variable unless it already exists | |
set_default() | |
{ | |
local varname=$1 | |
shift | |
if [ -z "${!varname}" ]; then | |
eval "$varname=\"$@\"" | |
fi | |
} | |
ARG_COUNT=1 | |
# fail if first argument is not an option and there are more than $ARG_COUNT positional arguments | |
[[ $1 =~ ^-.+ ]] || [[ "$#" == "$ARG_COUNT" ]] || usage | |
while getopts 's:d:?hv' c | |
do | |
case $c in | |
s) set_variable SUBJECT $OPTARG ;; | |
d) set_variable DAYS $OPTARG ;; | |
v) set -x ;; | |
h|?) usage ;; esac | |
done | |
shift $((OPTIND-1)) | |
set_variable HOST $1 | |
set_default SUBJECT "/C=GB/L=London/O=IT/CN=$HOST" | |
set_default DAYS 3650 | |
################################### | |
# Main script starts here | |
# Enter unofficial bash strict mode | |
################################### | |
set -euo pipefail | |
IFS=$'\n\t' | |
openssl req -new -nodes -x509 -subj "$SUBJECT" -days ${DAYS} -keyout "${HOST}.key" -out "${HOST}.crt" -extensions v3_ca |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment