Skip to content

Instantly share code, notes, and snippets.

@sammy2077
Created April 10, 2021 12:54
Show Gist options
  • Save sammy2077/a5c10cb25062e68060058be59ef26297 to your computer and use it in GitHub Desktop.
Save sammy2077/a5c10cb25062e68060058be59ef26297 to your computer and use it in GitHub Desktop.
File to import certificate to keystore
#!/usr/bin/env sh
KEYSTORE_FILE=./keystore.jks
KEYSTORE_PASS=agweria
HOST=$1
PORT=$2
# get the SSL certificate
openssl s_client -connect "${HOST}":"${PORT}" </dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' >"${HOST}".cert
# delete the old alias and then import the new one
keytool -delete -keystore ${KEYSTORE_FILE} -storepass ${KEYSTORE_PASS} -alias "${HOST}" &>/dev/null
# create a keystore and import certificate
keytool -import -noprompt -trustcacerts \
-alias "${HOST}" -file "${HOST}".cert \
-keystore ${KEYSTORE_FILE} -storepass ${KEYSTORE_PASS}
rm "${HOST}".cert
@sammy2077
Copy link
Author

Usage

./import_cert.sh HOST PORT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment