Home > Back-end >  Adding the Alias name from keytool to my working script
Adding the Alias name from keytool to my working script

Time:02-01

I have the following working script however, I need to get the Alias name: and add this into my script, I have been down many rabbit holes and think I have confused myself and need some guidance.

#!/bin/sh

until=$(keytool -list -v -keystore /usr/java/jdk1.8.0_301- 
amd64/jre/lib/security/cacerts -storepass changeit | grep "until:" | 
sed 's/^.*until: //')

now_seconds=`date  %s`
now_days=$((now_seconds / 86400))
IFS=$'\n'

for line in $until 
do
    #if [ echo $line | grep -i alias ]
    #then cert_name=${$line}
    #else detail =
    certdate_secs=$(date  %s --date="$line")
    certdate_days=$((certdate_secs / 86400))
    expiry_date=$(($certdate_days - $now_days))
    if [[ $expiry_date -le 30 ]];
then
    echo -e "The Server hostng the cert is $HOSTNAME\n"
    echo -e "#############################################\n"
    echo -e "This is the keytool cert expiry in seconds: 
$certdate_secs\n"
    echo -e "This is the keytool cert expiry in days: 
$certdate_days\n"
    echo -e "#############################################\n" #> 
$file
    echo -e "The cert will expiry in $expiry_date days\n" #> $file
    echo -e "********************************************\n" #> $file
fi
done

CodePudding user response:

You can use GNU awk for preprocessing keytool output:

#!/bin/bash

keystore=/usr/java/jdk1.8.0_301-amd64/jre/lib/security/cacerts
storepass='<PWD>'

keytool -list -v -keystore "$keystore" -storepass "$storepass" |
awk '
    BEGIN { now = systime() }
    /^Alias name:/ { alias = $3 }
    ! /^Valid from:/ { next }
    { sub(/.*: /,"") }
    { $2 = (index("JanFebMarAprMayJunJulAugSepOctNovDec",$2)   2) / 3 }
    { print alias,int((mktime($6" "$2" "$3" "gensub(/:/," ","g",$4)" "$5)-now)/86400) }
' |
while read -r cert_alias expiry_days
do
    if (( expiry_days < 30 ))
    then
        echo "$cert_alias will expire in $expiry_days days"
    fi
done
  •  Tags:  
  • Related