#/bin/bash host=$1 port=443 end_date=`openssl s_client -servername $host -host $host -port $port -showcerts </dev/null 2>/dev/null | sed -n '/BEGIN CERTIFICATE/,/END CERT/p' | openssl x509 -text 2>/dev/null | sed -n 's/ *Not After : *//p'` if [ -n "$end_date" ] then end_date_seconds=`date '+%s' --date "$end_date"` # date指令format字符串時間。 now_seconds=`date '+%s'` echo "($end_date_seconds-$now_seconds)/24/3600" | bc fi
把上面的腳本保存到check_ssl.sh文件裏,而後執行sh check_ssl.sh www.baidu.com便可檢查知道證書的有效期bash
注意-servername 這個參數,若是沒有這個參數,則是檢查這個ip的證書ide