Shell script to raise csr by using openssl

 #!/bin/bash


# Set variables

CA_SERVER="ca.example.com"

CA_PORT="443"

CA_USERNAME="username"

CA_PASSWORD="password"

CSR_FILE="mydomain.csr"

CERT_FILE="mydomain.crt"

KEY_FILE="mydomain.key"


# Prompt user for certificate details

printf "Enter the common name (e.g. example.com): "

read COMMON_NAME

printf "Enter the two-letter country code (e.g. US): "

read COUNTRY

printf "Enter the state or province name: "

read STATE

printf "Enter the locality or city name: "

read LOCALITY

printf "Enter the organization name: "

read ORGANIZATION

printf "Enter the organizational unit name: "

read ORG_UNIT


# Generate private key

openssl genrsa -out "$KEY_FILE" 2048


# Create CSR

openssl req -new -key "$KEY_FILE" -out "$CSR_FILE" -subj "/C=$COUNTRY/ST=$STATE/L=$LOCALITY/O=$ORGANIZATION/OU=$ORG_UNIT/CN=$COMMON_NAME"


echo "CSR generated and saved to $CSR_FILE"


# Display CSR and prompt user to send it to CA and receive certificate

echo "CSR contents:"

cat "$CSR_FILE"

printf "\n"

echo "Send the above CSR to your CA and receive the signed certificate."

printf "Save the signed certificate to a file (e.g. mydomain.crt) and press Enter to continue."

read


# Check if certificate received

if [ ! -s "$CERT_FILE" ]; then

  echo "Error: certificate not received"

  exit 1

fi


echo "Certificate received and saved to $CERT_FILE"




This version of the script uses printf instead of read to prompt the user for certificate details. After generating the CSR, the script displays the contents of the CSR and prompts the user to send it to their CA and receive the signed certificate manually. Once the user has received the signed certificate and saved it to a file, they can press Enter to continue and the script will check if the certificate file exists and display a message indicating success.

Note that this version of the script assumes that the user knows how to send the CSR to their CA and receive the signed certificate, and it doesn't perform any validation or error checking for these steps.




Please try below as well

#!/bin/bash # Set variables CA_SERVER="ca.example.com" CA_PORT="443" CA_USERNAME="username" CA_PASSWORD="password" CSR_FILE="mydomain.csr" CERT_FILE="mydomain.crt" CERT_PEM_FILE="mydomain.pem" KEY_FILE="mydomain.key" NGINX_CONF_FILE="/etc/nginx/conf.d/mydomain.conf" NGINX_SERVER_NAME="mydomain.com" # Generate private key openssl genrsa -out "$KEY_FILE" 2048 # Create CSR openssl req -new -key "$KEY_FILE" -out "$CSR_FILE" -subj "/CN=$NGINX_SERVER_NAME" echo "CSR generated and saved to $CSR_FILE" # Prompt user to send CSR to CA echo "Please send the following CSR to your CA:" cat "$CSR_FILE" # Wait for user to send CSR and receive signed certificate echo "Please save the signed certificate to $CERT_FILE and press Enter when finished" read if [ ! -s "$CERT_FILE" ]; then echo "Error: certificate not received" exit 1 fi echo "Certificate received and saved to $CERT_FILE" # Convert signed certificate to PEM format openssl x509 -in "$CERT_FILE" -out "$CERT_PEM_FILE" -outform PEM echo "Certificate converted to PEM format and saved to $CERT_PEM_FILE" # Configure Nginx to use signed certificate cat > "$NGINX_CONF_FILE" <<EOF server { listen 80; server_name $NGINX_SERVER_NAME; # Redirect HTTP to HTTPS return 301 https://\$host\$request_uri; } server { listen 443 ssl; server_name $NGINX_SERVER_NAME; ssl_certificate $CERT_PEM_FILE; ssl_certificate_key $KEY_FILE; # Other Nginx configuration directives... } EOF echo "Nginx configuration updated in $NGINX_CONF_FILE"


Comments

Popular posts from this blog

Shell script to find out aliases and their expiration in cacerts

shell script to scp multiple file to multiple destinations

How to resolve port conflict in websphere