Skip to content

Instantly share code, notes, and snippets.

@Kirill888
Created March 1, 2015 01:28
Show Gist options
  • Save Kirill888/ca21dc1a2e57ba9f00ad to your computer and use it in GitHub Desktop.
Save Kirill888/ca21dc1a2e57ba9f00ad to your computer and use it in GitHub Desktop.
prints server certificate
#!/bin/sh
exit_with_error () {
rr=$1; shift
>&2 echo "$@"
exit $rr
}
is_installed () {
cmd="$1"
hash "${cmd}" 2> /dev/null || exit_with_error 1 "Missing ${cmd}"
}
parse_cert () {
awk 'BEGIN { f = 0; }
/^-----BEGIN CERTIFICATE-----/ || f == 1 { f = 1; print; }
/^-----END CERTIFICATE-----/ { exit;}'
}
get_cert () {
server="$1"
if ct_data=$(openssl s_client -connect "${server}" </dev/null 2> /dev/null)
then
echo "${ct_data}" | parse_cert
else
exit_with_error $? "Failed to connect to ${server}"
fi
}
has_right_tools () {
is_installed awk && is_installed openssl
}
has_right_tools && get_cert $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment