Shell script to find out aliases and import new cert in jdk

 #!/bin/bash


# Function to display the available aliases in the cacerts file

function display_aliases {

    printf "Available aliases in $cacerts_path:\n"

    keytool -list -keystore $cacerts_path -storepass changeit | grep "Alias name:" | sed 's/^.*Alias name: //'

}


# Function to import a certificate into the cacerts file

function import_certificate {

    printf "Enter the path to the certificate file: "

    read cert_path

    printf "Enter an alias for the certificate: "

    read cert_alias

    cd cacerts cacerts_bkp

    keytool -import -alias $cert_alias -file $cert_path -keystore $cacerts_path -storepass changeit

    printf "Certificate imported with alias $cert_alias.\n"

}


# Function to delete a certificate from the cacerts file

function delete_certificate {

    printf "Enter the alias of the certificate to delete: "

    read cert_alias

    keytool -delete -alias $cert_alias -keystore $cacerts_path -storepass changeit

    printf "Certificate with alias $cert_alias deleted.\n"

}


# Function to view the expiration date of a certificate in the cacerts file

function view_expiration {

    printf "Enter the alias of the certificate: "

    read cert_alias

    expiration=$(keytool -list -v -alias $cert_alias -keystore $cacerts_path -storepass changeit | grep Valid\ until | sed 's/^.*until: //')

    printf "Expiration date for certificate with alias $cert_alias: $expiration\n"

}


# Prompt the user for the path to the cacerts file

printf "Enter the path to the cacerts file: "

read cacerts_path


# Verify that the file exists

if [ ! -f $cacerts_path ]; then

    printf "The specified file does not exist.\n"

    exit 1

fi


# Display the available options

while true

do

    printf "Select an option:\n"

    printf "1. Display available aliases\n"

    printf "2. Import a certificate\n"

    printf "3. Delete a certificate\n"

    printf "4. View the expiration date of a certificate\n"

    printf "5. Exit\n"

    printf "Enter an option: "

    read option


    case $option in

        1)

            display_aliases

            ;;

        2)

            import_certificate

            ;;

        3)

            delete_certificate

            ;;

        4)

            view_expiration

            ;;

        5)

            exit 0

            ;;

        *)

            printf "Invalid option.\n"

            ;;

    esac

done










 we have defined four functions: display_aliases, import_certificate, delete_certificate, and view_expiration. We have also added a while loop that displays a menu of options and prompts the user to select an option. Depending on the user's selection, the appropriate function is called.


Option 1 displays the available aliases in the cacerts file, option 2 prompts the user to import a certificate into the cacerts file, option 3 prompts the user to delete a certificate from the cacerts file, option 4 prompts the user to view the expiration date of a certificate in the cacerts file, and option 5 exits the script.


Note that the import_certificate function assumes that the certificate file is in the PEM format. If the certificate file is in a different format, you may need to modify the function accordingly. Additionally, the delete_certificate function does not prompt the user to confirm the deletion, so use it with caution.

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