shell script to retrieve ssl certificate by using openssl s_client
shell script to retrieve ssl certificate by using openssl client
#!/bin/bash
# Function to retrieve certificate for a given hostname and port
function get_cert {
HOSTNAME="$1"
PORT="$2"
# Retrieve the certificate and extract the end-entity certificate
CERT=$(echo | openssl s_client -showcerts -servername "$HOSTNAME" -connect "$HOSTNAME":"$PORT" 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | sed -n '1,/END CERTIFICATE/p')
# Save the certificate in PEM format to a file
echo "$CERT" > site_cert.pem
echo "Certificate for $HOSTNAME:$PORT saved to site_cert.pem"
}
# Array of sites and corresponding ports
SITES=(
"example.com:443"
"google.com:443"
"yahoo.com:443"
"github.com:443"
"reddit.com:443"
"twitter.com:443"
"linkedin.com:443"
"stackoverflow.com:443"
)
# Print the list of sites as options
printf "Select one or more options by number:\n"
for i in "${!SITES[@]}"; do
printf "%s) %s\n" "$((i+1))" "${SITES[$i]}"
done
# Prompt the user to enter options
printf "Enter one or more options (e.g. 1 3 5): "
read OPTIONS
# Loop through the options and call get_cert for each corresponding hostname and port
for OPT in $OPTIONS; do
INDEX=$(( $OPT - 1 ))
if [ $INDEX -ge 0 ] && [ $INDEX -lt ${#SITES[@]} ]; then
HOSTNAME=$(echo "${SITES[$INDEX]}" | cut -d':' -f1)
PORT=$(echo "${SITES[$INDEX]}" | cut -d':' -f2)
get_cert "$HOSTNAME" "$PORT"
else
printf "Invalid option: %s\n" "$OPT"
fi
done
Comments
Post a Comment