lab_tools/csrgen.sh

42 lines
1.2 KiB
Bash

#!/bin/bash
# Check for required arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <hostname> <ip>"
exit 1
fi
HOSTNAME=$1
IP=$2
DEST_FOLDER="~/LAB/CA" # Use the environment variable
CONF_FILE="newcert.cnf"
KEY_FILE="${HOSTNAME}-key.pem"
CSR="${HOSTNAME}.csr"
# Use sed to replace the hostname in the configuration file
sed -i.bak "s/[a-z]\+\.local\.naxslabs\.com/${HOSTNAME}.local.naxslabs.com/g" "$CONF_FILE"
# Update the IP address in the configuration file
sed -i.bak "s/[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/${IP}/g" "$CONF_FILE"
echo "Updated $CONF_FILE with hostname: $HOSTNAME"
# Generate SSL certificate request
openssl req -new -newkey rsa:2048 -nodes -keyout "$KEY_FILE" -out "$CSR" -config "$CONF_FILE" -extensions v3_req
echo "Generated SSL certificate and key: $KEY_FILE and $CSR"
# Create a new directory for the hostname within the destination folder
HOST_DIR="${DEST_FOLDER}/${HOSTNAME}"
mkdir -p "$HOST_DIR"
# Check if generated files exist and move them to the hostname directory
for file in "$CSR" "$KEY_FILE"; do
if [ -f "$file" ]; then
mv "$file" "$HOST_DIR"
echo "Moved $file to $HOST_DIR"
else
echo "$file does not exist."
fi
done