0

I am trying to a write a bash script which would take out put of openssl -enddate command and compare it with dateutil.ddiff now and wanted to know how many days are remaining.

SSLDATE=`openssl x509 -noout -in wild.crt -enddate | sed -e 's/notAfter=//g' | awk '{print $4"-""03""-"$2}'`

Now I have installed dateutils package which provides me this facility. However I am not getting how do I calculate the with $SSLDATE - $now?

Plus is there a way since openssl -enddate gives Month in alphabet format while dateutils accepts in numeric format

Any idea how to achieve it? For example

dateutils.ddiff 2021-04-12 2022-04-15 -f '%dd'
**368d**

Or is there any other alternative for this?

Blason R
  • 5
  • 2

1 Answers1

0

Alternate solution not requiring dateutils

https://stackoverflow.com/a/66921470/13982210


I am not familiar with openssl nor dateutils. However, the solution I can offer (which may not be the best or most efficient) is:

#!/usr/bin/env bash
# line broken with \ for readability
ssldate=$(openssl x509 -noout -in example.crt -enddate \
    | awk -F '=' '{print $2}')
    # alternatively
  # | cut -d = -f 2

# use the variable `$ssldate` in your `dateutils` command
dateutils.ddiff today "$(date --date="$ssldate" '+%F')" -f '%dd'

Things to note

  • UPPERCASE variables are not preferred in scripting to avoid clashing with system/shell env variables. refer this answer
  • the backtick syntax `` is deprecated in favour of $() which supports nesting

Explanations

Sorry I do not have a list of references for further reading on any of the concepts. Hopefully, man pages and/or search engines can fill in.

  • $(command) command substitution. newer syntax of `` to convert output of command within to string that can be used as argument in another command or stored in a variable
  • openssl ... | awk ... the | pipe operator takes the standard output from the LHS and passes it as the standard input to the RHS
  • awk works on the data received via piped standard input in the absence of a filename argument
    • usually works on fields. think of text as a table of rows and columns.
    • -F '=' sets the field separator as = - i.e. the character that splits each line into fields (space by default)
    • '{print $2}' prints the second field
  • alt: cut same principle. -d sets the delimiter that does the field splitting. -f the field number
  • date [-d | --date=]<DATESTRING> '+%F' (GNU date) - format as %F full date (YYYY-MM-DD)
kevinnls
  • 728
  • 4
  • 19
  • Yep - That works fine. However only glitch I see is openssl actual enddate show in below format and %m or Month I entered manually just for testing purpose wondering how do I convert the current month at run time. ``` openssl x509 -noout -in wild.crt -enddate | sed -e 's/notAfter=//g' Mar 14 16:43:04 2022 GMT ``` – Blason R Apr 15 '21 at 05:29
  • updated the answer to suit your case @BlasonR – kevinnls Apr 15 '21 at 05:35
  • Great Thanks man!! – Blason R Apr 15 '21 at 05:40
  • let me know if it works as expected – kevinnls Apr 15 '21 at 05:42
  • Unfortunately nope - it did not. This command gives me the current date openssl x509 -noout -in wild.crt -enddate | awk -F '= '{print $2}' | date '+%Y-%m-%d' 2021-04-15 – Blason R Apr 15 '21 at 05:45
  • updated and checked. should work as expected @BlasonR – kevinnls Apr 15 '21 at 06:00
  • 1
    Yep - Looks like its working. Though I am testing it carefully :) – Blason R Apr 15 '21 at 06:07
  • 1
    Thanks - Its working as expected. Keen to know what exactly done here. I am not pro in bash scripting awk -F '=' '{print $2}') – Blason R Apr 15 '21 at 06:41