Write a script that checks SSL certificate expiry for a list of domains and warns under 30 days.
AdvancedHands-onBash scripting tasks
Answer
#!/usr/bin/env bash
set -euo pipefail
WARN_DAYS=30
while read -r domain; do
end=$(echo | openssl s_client -servername "$domain" -connect "$domain:443" 2>/dev/null \
| openssl x509 -noout -enddate | cut -d= -f2)
end_epoch=$(date -d "$end" +%s)
days=$(( (end_epoch - $(date +%s)) / 86400 ))
if (( days < WARN_DAYS )); then
echo "WARN: $domain expires in $days days ($end)"
else
echo "OK: $domain ($days days)"
fi
done < domains.txt
Key trick → -servername sends SNI. Without it, a shared host returns its default certificate and you check the wrong expiry. (openssl x509 -checkend 2592000 is a shortcut that gives a yes/no answer.)
More on Bash scripting tasks
Write the safe header you start every Bash script with, and explain each part.BeginnerWrite a script that alerts when any filesystem is above 80% usage.BeginnerParse an Nginx access log and print the top 10 client IPs by request count.BeginnerWrite a backup script that tars a directory with a timestamp and keeps only the last 7 backups.IntermediateWrite a health-check loop that polls a URL until it returns 200, with a timeout.IntermediateParse command-line flags `-e <env>`, `-v` (verbose) and `-h` in a Bash script.Intermediate