LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to monitor certificate expiry date using a linux script (https://www.linuxquestions.org/questions/programming-9/how-to-monitor-certificate-expiry-date-using-a-linux-script-721787/)

jefn 04-26-2009 08:29 AM

How to monitor certificate expiry date using a linux script
 
Hi,

I face a problem to compare time and date. I need to convert the format of the output of "date" command with the following default format:

Mon Apr 27 02:29:57 EST 2009

to the following Format:

Apr 27 02:29:57 2009 GMT

The purpose of this is to design a script to monitor if a certificate expires or not. The latest format "Apr 27 02:29:57 2009 GMT" is the date format of the certificate which I cannot compare with "date".

How can I compare if it is expired? can you help me please ?


Thanks all in advance,
Jef

choogendyk 04-26-2009 01:38 PM

Try checking out `man convdate`. Put both in the form of seconds since the epoch, and then do a simple numerical comparison.

Alternatively, mon (http://mon.wiki.kernel.org/index.php/Main_Page) has a monitor module that checks web certs for expiration. With terms like "mon" and "monitor", that can be wickedly difficult to google. ;) So, I have to remember "kernel.org" to even find them, and then follow links from there or ask on their mailing list. However, I particularly like mon for its simplicity -- http://blogs.umass.edu/choogend/2007...nitoring-tool/

Asy 04-26-2009 03:38 PM

look to man date and use the [+Format....]
in the format you can use %Mon for month etc.

jlinkels 04-26-2009 07:48 PM

Forget about comparing dates this way. Sooner or later it goes wrong because there is one character different or the time zone doesn't match.

Use:
Code:

date -d "Mon Apr 27 02:29:57 EST 2009" +%s
and
Code:

date -d "Apr 27 02:29:57 2009 GMT" +%s
To get the time difference (or equality), use this expression:
Code:

donald_pc:~$ diff=$(echo "$(date -d "Apr 27 02:29:57 2009 GMT" +%s) $(date -d "Mon Apr 27 02:29:57 EST 2009" +%s) - p" | dc)
donald_pc:~$ echo $diff
-18000

The time difference is in seconds and 0 if there is no time difference. Note that these expression currently do not take DST into account, because I don't have tz_dsttime set on my computer. Maybe you should check that yourself on your own computer.

jlinkels

jefn 04-27-2009 04:36 AM

Quote:

Originally Posted by choogendyk (Post 3521635)
Try checking out `man convdate`. Put both in the form of seconds since the epoch, and then do a simple numerical comparison.

Alternatively, mon (http://mon.wiki.kernel.org/index.php/Main_Page) has a monitor module that checks web certs for expiration. With terms like "mon" and "monitor", that can be wickedly difficult to google. ;) So, I have to remember "kernel.org" to even find them, and then follow links from there or ask on their mailing list. However, I particularly like mon for its simplicity -- http://blogs.umass.edu/choogend/2007...nitoring-tool/


Thanks mate :)

jefn 04-27-2009 04:36 AM

Quote:

Originally Posted by Asy (Post 3521728)
look to man date and use the [+Format....]
in the format you can use %Mon for month etc.

Thanks mate ;)

jefn 04-27-2009 04:37 AM

Quote:

Originally Posted by jlinkels (Post 3521876)
Forget about comparing dates this way. Sooner or later it goes wrong because there is one character different or the time zone doesn't match.

Use:
Code:

date -d "Mon Apr 27 02:29:57 EST 2009" +%s
and
Code:

date -d "Apr 27 02:29:57 2009 GMT" +%s
To get the time difference (or equality), use this expression:
Code:

donald_pc:~$ diff=$(echo "$(date -d "Apr 27 02:29:57 2009 GMT" +%s) $(date -d "Mon Apr 27 02:29:57 EST 2009" +%s) - p" | dc)
donald_pc:~$ echo $diff
-18000

The time difference is in seconds and 0 if there is no time difference. Note that these expression currently do not take DST into account, because I don't have tz_dsttime set on my computer. Maybe you should check that yourself on your own computer.

jlinkels

I think this is what I am looking for.

Thanks alot mate,


All times are GMT -5. The time now is 05:23 PM.