2016-10-10 28 views
0

我想檢查一個網站的證書在標準Ubuntu 14.04服務器上運行的bash腳本中有效的天數。 openssl可用。確定證書在bash腳本中仍然有效的天數

我已經想通了,我可以使用OpenSSL來獲得目標日期

$ echo | openssl s_client -connect google.com:443 2>/dev/null|openssl x509 -noout -enddate 
notAfter=Dec 22 16:37:00 2016 GMT 

但我怎麼解析結果日期和減去當前的?或者可能有更好的解決方案?

回答

2

隨着GNU date%j拿到一年的一天,算術擴展的減法:

$ echo $(($(date -d "$(cut -d= -f2 <(echo 'notAfter=Dec 22 16:37:00 2016 GMT'))" '+%j') - $(date '+%j'))) 
73 
  • $(date -d "$(cut -d= -f2 <(echo 'notAfter=Dec 22 16:37:00 2016 GMT'))" '+%j')會讓我們從我們已經拿到了日起一年的一天,取代德echo命令裏面的進程替換,<(echo 'notAfter=Dec 22 16:37:00 2016 GMT')與您最初使用的命令openssl

  • $(date '+%j')讓我們今天日期一年

  • $(())的一天用於減去整數

2
date1="Dec 22 16:37:00 2016 GMT"       # Future date 
date2=$(date)            # Current date 
diff=$(($(date -d "$date1" +%j)-$(date -d "$date2" +%j))) #Diff between two date, %j is to show day of the year. 
echo $diff            #Display difference    
73 

或者只是在一條線:

echo $(($(date -d "Dec 22 16:37:00 2016 GMT" +%j)-$(date +%j))) 
73 
相關問題