将 keytool 中的别名添加到我的工作脚本中

Adding the Alias name from keytool to my working script

我有以下工作脚本,但是,我需要获取别名:并将其添加到我的脚本中,我遇到了很多麻烦,觉得自己很困惑,需要一些指导。

#!/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

您可以使用 GNU awk 进行预处理 keytool 输出:

#!/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 =  }
    ! /^Valid from:/ { next }
    { sub(/.*: /,"") }
    {  = (index("JanFebMarAprMayJunJulAugSepOctNovDec",) + 2) / 3 }
    { print alias,int((mktime(" "" "" "gensub(/:/," ","g",)" ")-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