2016-01-08 43 views
-2

我接近這..我需要得到日期數學釘下來,這是相當馬虎的權利 - 我知道。 :)Java - 批處理文件試圖找到證書即將過期使用keytool

REM this is using routines from: http://www.robvanderwoude.com/datetimentmath.php 

:: Strip the day of the week from the current date 
FOR %%A IN (%Date%) DO SET Today=%%A 
:: Parse the date, prefix day and month with an extra leading zero 
FOR /F "tokens=1-3 delims=/-" %%A IN ("%Today%") DO (
    REM For European date format DD-MM-YYYY use SET Day=0%%A and SET Month=0%%B instead 
    SET Day=0%%B 
    SET Month=0%%A 
    SET Year=%%C 
) 
:: Remove excess leading zeroes 
SET Day=%Day:~-2% 
SET Month=%Month:~-2% 
:: Display the results 
SET Day 
SET Month 
SET Year 
:: Convert to Julian date 
CALL :JDate %Year% %Month% %Day% 
:: Display the result 
SET /A sdate=JDate 

REM BECAUSE WE ARE POTENTIALLY GOING INTO MULTIPLE KEYSTORES FOR JAVA'S KEYTOOL, WE CAN JUST BY-PASS THE PASSWORD 
ECHO.|"%JAVA_HOME%\bin\keytool" -list -v -alias "company.com" -keystore "%JAVA_HOME%\jre\lib\security\cacerts" > results.txt 

FOR /F "tokens=1-15 delims= " %%a IN ('TYPE results.txt ^| FIND "until:" ') DO (
if %%k EQU Jan set mn=01 
if %%k EQU Feb set mn=02 
if %%k EQU Mar set mn=03 
if %%k EQU Apr set mn=04 
if %%k EQU May set mn=05 
if %%k EQU Jun set mn=06 
if %%k EQU Jul set mn=07 
if %%k EQU Aug set mn=08 
if %%k EQU Sep set mn=09 
if %%k EQU Oct set mn=10 
if %%k EQU Nov set mn=11 
if %%k EQU Dec set mn=12 

CALL :Jdatea %%o %mn% %%l 
SET /A edate=JDatea 
) 

Set /A=Result==%eDate%-%sDate% 

REM if less than 30 days, send an alert 
if %Result% LSS 30 blat -f "[email protected]" -t "[email protected]" -server 1.2.3.4 -subject "certificate for %computername% is about to expire in less than 30 days!" -q 


:JDate 
:: Convert date to Julian 
:: Arguments : YYYY MM DD 
:: Returns : Julian date 
:: 
:: First strip leading zeroes 
SET MM=%2 
SET DD=%3 
IF %MM:~0,1% EQU 0 SET MM=%MM:~1% 
IF %DD:~0,1% EQU 0 SET DD=%DD:~1% 
:: 
:: Algorithm based on Fliegel-Van Flandern 
:: algorithm from the Astronomical Almanac, 
:: provided by Doctor Fenton on the Math Forum 
:: (http://mathforum.org/library/drmath/view/51907.html), 
:: and converted to batch code by Ron Bakowski. 
SET /A Month1 = (%MM% - 14)/12 
SET /A Year1 = %1 + 4800 
SET /A JDate = 1461 * (%Year1% + %Month1%)/4 + 367 * (%MM% - 2 -12 * %Month1%)/12 - (3 * ((%Year1% + %Month1% + 100)/100))/4 + %DD% - 32075 
SET Month1= 
SET Year1= 
GOTO:EOF 


:JDatea 
:: Convert date to Julian 
:: Arguments : YYYY MM DD 
:: Returns : Julian date 
:: 
:: First strip leading zeroes 
SET MM=%2 
SET DD=%3 
IF %MM:~0,1% EQU 0 SET MM=%MM:~1% 
IF %DD:~0,1% EQU 0 SET DD=%DD:~1% 
:: 
:: Algorithm based on Fliegel-Van Flandern 
:: algorithm from the Astronomical Almanac, 
:: provided by Doctor Fenton on the Math Forum 
:: (http://mathforum.org/library/drmath/view/51907.html), 
:: and converted to batch code by Ron Bakowski. 
SET /A Month1 = (%MM% - 14)/12 
SET /A Year1 = %1 + 4800 
SET /A JDate = 1461 * (%Year1% + %Month1%)/4 + 367 * (%MM% - 2 -12 * %Month1%)/12 - (3 * ((%Year1% + %Month1% + 100)/100))/4 + %DD% - 32075 
SET Month1= 
SET Year1= 
GOTO:EOF 


PAUSE 

謝謝!

+0

你能運行Java程序呢?看看這個問題:http://stackoverflow.com/questions/9513257/checking-certificates-expiration-dates-in-java-keystore –

+0

儘管我想運行一個Java程序,但我仍然使用Keytool並已經整理出如何使用Keytool來完成它,並且回答了我自己的帖子。 – Leptonator

+0

請將您的答案作爲答案發布並接受,因此將此問題從「未解決的問題」隊列中刪除。 – Stephan

回答

0

答:我已經想通了這一點..

有一些評論創建/使用Java程序文件要做到這一點,但是這將正常工作。

REM ROUTINES ARE FROM: http://www.robvanderwoude.com/files/datediff_nt.txt 
SET DEST=D:\WORK\SCRIPTS\results.txt 

REM BECAUSE WE ARE POTENTIALLY GOING INTO MULTIPLE KEYSTORES FOR JAVA'S KEYTOOL, WE CAN JUST BY-PASS THE PASSWORD 
ECHO.|"%JAVA_HOME%\bin\keytool" -list -v -alias "Some-Alias" -keystore "%JAVA_HOME%\jre\lib\security\cacerts" > %DEST% && SET alias="Some-Alias" && CALL :datecheck 
ECHO.|"%JAVA_HOME%\bin\keytool" -list -v -alias "someother" -keystore "C:\anotherfolder\keystore.ks" > %DEST% && SET alias="someother" && CALL :datecheck 

:datecheck 
FOR /F "tokens=11-15 delims= " %%a IN ('TYPE %DEST%^| FINDSTR "until:"') DO (
    IF %%a EQU Jan SET eDate=01/%%b/%%e 
    IF %%a EQU Feb SET eDate=02/%%b/%%e 
    IF %%a EQU Mar SET eDate=03/%%b/%%e 
    IF %%a EQU Apr SET eDate=04/%%b/%%e 
    IF %%a EQU May SET eDate=05/%%b/%%e 
    IF %%a EQU Jun SET eDate=06/%%b/%%e 
    IF %%a EQU Jul SET eDate=07/%%b/%%e 
    IF %%a EQU Aug SET eDate=08/%%b/%%e 
    IF %%a EQU Sep SET eDate=09/%%b/%%e 
    IF %%a EQU Oct SET eDate=10/%%b/%%e 
    IF %%a EQU Nov SET eDate=11/%%b/%%e 
    IF %%a EQU Dec SET eDate=12/%%b/%%e 
) 

CALL :ParseDate %eDate% 
REM CALL :ParseDate 05/27/2017 

REM :: Parse the first date 
REM CALL :ParseDate %1 

:: Convert the parsed Gregorian date to Julian 
CALL :JDate %GYear% %GMonth% %GDay% 

:: Save the resulting Julian date 
SET JDate1=%JDate% 

:: Use today if no second date was specified 
IF "%~2"=="" (
    FOR %%A IN (%Date%) DO SET Date2=%%A 
) ELSE (
    SET Date2=%2 
) 

:: Parse the second date 
CALL :ParseDate %Date2% 

:: Convert the parsed Gregorian date to Julian 
CALL :JDate %GYear% %GMonth% %GDay% 

:: Calculate the absolute value of the difference in days 
IF %JDate% GTR %JDate1% (
    SET /A DateDiff = %JDate% - %JDate1% 
) ELSE (
    SET /A DateDiff = %JDate1% - %JDate% 
) 

:: Format output for singular or plural 
SET Days=days 
IF %DateDiff% EQU 1 SET Days=day 

:: Prefix value with a minus sign if negative 
IF %JDate% GTR %JDate1% SET DateDiff=-%DateDiff% 

:: Display the result 
ECHO First date : %1 
ECHO Second date : %Date2% 
ECHO Difference : %DateDiff% %Days% 

IF %DateDiff% LSS 30 D:\WORK\SCRIPTS\Blat.exe -server 1.2.3.4-to [email protected] -f [email protected] -s "Certficiate is to expire in less than 30 days" -body "Certficiate is to expire in less than %DateDiff% days for %COMPUTERNAME% for %alias%" -q 

:: Return the result in a variable named after this batch file 
ENDLOCAL & SET %~n0=%DateDiff% 
GOTO:EOF 


::===================================:: 
::         :: 
:: - S u b r o u t i n e s - :: 
::         :: 
::===================================:: 


:JDate 
:: Convert date to Julian 
:: Arguments : YYYY MM DD 
:: Returns : Julian date 
:: 
:: First strip leading zeroes; a logical error in this 
:: routine was corrected with help from Alexander Shapiro 
SET MM=%2 
SET DD=%3 
IF 1%MM% LSS 110 SET MM=%MM:~1% 
IF 1%DD% LSS 110 SET DD=%DD:~1% 
:: 
:: Algorithm based on Fliegel-Van Flandern 
:: algorithm from the Astronomical Almanac, 
:: provided by Doctor Fenton on the Math Forum 
:: (http://mathforum.org/library/drmath/view/51907.html), 
:: and converted to batch code by Ron Bakowski. 
SET /A Month1 = (%MM% - 14)/12 
SET /A Year1 = %1 + 4800 
SET /A JDate = 1461 * (%Year1% + %Month1%)/4 + 367 * (%MM% - 2 -12 * %Month1%)/12 - (3 * ((%Year1% + %Month1% + 100)/100))/4 + %DD% - 32075 
FOR %%A IN (Month1 Year1) DO SET %%A= 
GOTO:EOF 


:ParseDate 
:: Parse (Gregorian) date depending on registry's date format settings 
:: Argument : Gregorian date in local date format, 
:: Requires : sDate (local date separator), iDate (local date format number) 
:: Returns : GYear (4-digit year), GMonth (2-digit month), GDay (2-digit day) 
:: 
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ('ECHO.%1') DO (
    SET GYear=%%C 
    SET GMonth=%%A 
    SET GDay=%%B 
) 
GOTO:EOF 

感謝