Source

echo -n
  | openssl s_client -connect $HOST:$PORTNUMBER -servername $SERVERNAME \
  | openssl x509 > /tmp/$SERVERNAME.cert