Skip to content

Instantly share code, notes, and snippets.

@mbierman
Last active April 8, 2024 07:45
Show Gist options
  • Save mbierman/0f3f5efee909bd19e56a248b46e88906 to your computer and use it in GitHub Desktop.
Save mbierman/0f3f5efee909bd19e56a248b46e88906 to your computer and use it in GitHub Desktop.
#!/bin/bash
#v 1.0.2
URL=$1
# Function to get the description for the certificate result
get_certificate_description() {
case $certificate_result in
0) echo "The certificate verification was successful." ;;
1) echo "The certificate could not be found." ;;
2) echo "The certificate could not be verified for some reason (e.g., it's self-signed or issued by an unknown CA)." ;;
3) echo "The certificate is valid, but the certificate authority (CA) is not known." ;;
4) echo "The certificate has expired." ;;
5) echo "The certificate is not yet valid." ;;
6) echo "The certificate chain is incomplete." ;;
7) echo "The certificate's issuer (CA) certificate is not available." ;;
*) echo "Unknown certificate verification result" ;;
esac
}
# Function to perform the curl request and print the output
perform_curl_request() {
local url="$URL"
local output
output=$(curl -s -o /dev/null -w "HTTP status code: %{http_code}\nFinal URL: %{url_effective}\nLast modified: $last\nNumber of redirects: %{num_redirects}\nCertificate Result: %{ssl_verify_result}\nRemote IP: %{remote_ip}\n" -L "$url")
echo "$output"
}
last=$(curl -sI "$1" | grep -i 'Last-Modified' || echo "Not found")
ipinfo=$(curl -s http://ip-api.com/json/$url | jq -r '{ country, city, timezone, isp }' | sed 's/[,{}"]//g' | grep -v '^$' | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2))}1')
# Main script
if [[ "$URL" == "URL" || ! "$URL" =~ ^https?:// ]]; then
echo "No URL provided."
exit 1
fi
# Perform the curl request and store the output
output=$(perform_curl_request "$url")
# Extract the ssl_verify_result value from the output and print the certificate result with its description
certificate_result=$(echo "$output" | awk '/Certificate Result:/ {print $NF; exit}')
if [[ -z "$certificate_result" ]]; then
echo "No certificate result found."
exit 1
fi
certificate_description=$(get_certificate_description "$certificate_result")
# Print the output without certificate result
echo "$(echo -e "\n$output\n$ipinfo" | grep -v 'Certificate Result:')"
# Print the certificate result with its description
echo -e "Certificate Result: $certificate_result ($certificate_description)\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment