Skip to content

Instantly share code, notes, and snippets.

@mikebridge
Created January 8, 2019 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebridge/3eef786ba8f9bce43b49a5b83d360921 to your computer and use it in GitHub Desktop.
Save mikebridge/3eef786ba8f9bce43b49a5b83d360921 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#?
# SSL Info - Displays information about the provided URL's SSL certificate.
#
# USAGE
# sslinfo [OPTIONS] URL
#
# ARGUMENTS
# 1. URL (string): URL to display SSL certificate information for. Should
# not have a scheme components (ex., https://).
#
# Ex: www.noahh.io
#
# If a value matching the expression: /-*[Hh](?:elp)?$/
# is provided this help text will be printed.
# OPTIONS
# --trim,-t Removes math garble (ex., 00:ae:86:12:f2:53:71:57:11)
# from the output. Text that a human would most likely
# not be able to make sense of.
#
# --page,-p Page the potentially long output. The $PAGER
# environment variable must be configured.
#
# BEHAVIOR
# Prints ssl certificate information to stdout
#?
# Options
while [ ! -z "$1" ]; do
case "$1" in
--trim|-t)
trim=true
;;
--page|-p)
# Check $PAGER environment variable is configured
if [[ -z "$PAGER" ]]; then
echo "Error: Page option provided but \$PAGER environment variable not set" >&2
exit 1
fi
page=true
;;
--help|-h)
show-help "$0"
exit 1
;;
*)
host="$1"
;;
esac
shift
done
# Arguments
if [ -z "$host" ]; then
echo "Error: host argument must be provided" >&2
exit 1
elif ! [[ "$host" =~ ^([a-zA-Z0-9]+\.)+[a-zA-Z0-9]+$ ]]; then
echo "Error: Invalid host \"$host\"" >&2
exit 1
fi
# Openssl trick for getting cert info from SO:
# https://serverfault.com/a/661982
info=$(echo | \
openssl s_client -showcerts -servername "$host" -connect "$host:443" 2>/dev/null | \
openssl x509 -inform pem -noout -text)
# Trim if requested
if [[ "$trim" ]]; then
info=$(echo "$info" | sed -e '/\([0-9a-fA-F]\{2\}:\)\{2,\}/d')
fi
# Output info
# Page if requested
if [[ "$page" ]]; then
echo "$info" | $PAGER
else
echo "$info"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment