-1

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
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Burnt Frets
  • 43
  • 1
  • 5
  • It's not clear where you are stuck, can you please [edit] to clarify, and probably include an excerpt of the data you are trying to process as well as the expected output (not as images, not as links to somewhere, not as alliterating lyrical poetry emoting feelings to suggest what the output should look like) – tripleee Jan 31 '22 at 12:20
  • @tripleee It's a follow-up question of https://stackoverflow.com/q/70803111/3387716 – Fravadona Jan 31 '22 at 12:46
  • That doesn't clarify at all what the output looks like or where the OP is stuck. – tripleee Jan 31 '22 at 12:49
  • @tripleee Right, the question isn't clear about the output. IMO he just want to get the _alias name_ alongside the remaining _expiration time (in days or seconds)_ in the loop – Fravadona Jan 31 '22 at 13:04

1 Answers1

0

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
Fravadona
  • 13,917
  • 1
  • 23
  • 35