Staying Secure & Up to Date – Part 5: Automation – Weekly Scan & Alerts Without Lifting a Finger
1. A script (a text file with instructions) runs the scan.
2. A system timer (systemd) starts the script automatically.
3. On findings, the script sends a Telegram message to your phone.
Step 1 – Create a Telegram bot (10 minutes, once)
1. In Telegram, search for @BotFather (official bot).
2. /newbot → pick name and username → you get a token (like
123456789:AA…) – the key to your bot.
3. Find your chat ID: message your bot once; your ID is then listed via the Telegram API (search
“Telegram getUpdates chat_id”).
4. Store it securely on your server:
sudo mkdir -p /etc/security sudo bash -c 'printf "TELEGRAM_BOT_TOKEN=YOUR_TOKEN\nTELEGRAM_CHAT_ID=YOUR_ID\n" > /etc/security/telegram.env' sudo chmod 600 /etc/security/telegram.env
Important: tokens are like passwords – don’t share them or let them appear in screenshots or logs.
Step 2 – The scan script
Create /usr/local/sbin/trivy-weekly.sh (as root) and make it executable. It scans all
running containers, writes a log, and reports new critical CVE IDs via Telegram:
#!/usr/bin/env bash
DATE="$(date +%Y-%m-%d)"
LOG="/var/log/security/trivy-${DATE}.log"
mkdir -p /var/log/security /var/cache/trivy
export TRIVY_CACHE_DIR=/var/cache/trivy
{
echo "===== Trivy-Scan $(hostname) $(date) ====="
for img in $(docker ps --format '{{.Image}}' | sort -u); do
echo "----- $img -----"
trivy image --severity HIGH,CRITICAL --no-progress --format table "$img" \
| grep -E 'Total:|CVE-|GHSA-' | sort -u
done
} > "$LOG" 2>&1
grep -oE 'CVE-[0-9]{4}-[0-9]+' "$LOG" | sort -u > /tmp/trivy-today.txt
[ -f /tmp/trivy-yesterday.txt ] && comm -13 /tmp/trivy-yesterday.txt /tmp/trivy-today.txt > /tmp/trivy-new.txt || cp /tmp/trivy-today.txt /tmp/trivy-new.txt
cp /tmp/trivy-today.txt /tmp/trivy-yesterday.txt
if [ -s /tmp/trivy-new.txt ] && [ -s /etc/security/telegram.env ]; then
. /etc/security/telegram.env
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
--data-urlencode "chat_id=${TELEGRAM_CHAT_ID}" \
--data-urlencode "text=🛡️ Alert $(hostname): $(wc -l < /tmp/trivy-new.txt) new critical CVEs. Log: $LOG" >/dev/null
fi
sudo chmod 755 /usr/local/sbin/trivy-weekly.sh sudo /usr/local/sbin/trivy-weekly.sh # test run – a Telegram message should arrive
Step 3 – Set up the weekly timer
Two tiny files and one command – done:
/etc/systemd/system/trivy-weekly.service
[Unit] Description=Trivy weekly scan [Service] Type=oneshot ExecStart=/usr/local/sbin/trivy-weekly.sh
/etc/systemd/system/trivy-weekly.timer
[Unit] Description=Weekly Trivy scan [Timer] OnCalendar=Mon *-*-* 06:30:00 Persistent=true [Install] WantedBy=timers.target
sudo systemctl daemon-reload sudo systemctl enable --now trivy-weekly.timer
From now on, the scan runs every Monday at 06:30 – whether you’re awake or not.
(Persistent=true means missed runs are caught up.)
Bonus: Watch the “actively exploited” list (KEV)
The weekly Trivy scan shows potentially affected CVEs. To answer “is it being exploited right now?”, a second little script fetches the CISA KEV list weekly and compares it to your scan results:
curl -sfL -o /var/cache/kev.json https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json
python3 - /var/cache/kev.json <(cat /var/log/security/trivy-*.txt 2>/dev/null | sort -u) <<'PY'
import json, sys
kev = {v["cveID"] for v in json.load(open(sys.argv[1]))["vulnerabilities"]}
lokal = {l for l in open(sys.argv[2]).read().split()}
for c in sorted(kev & lokal):
print(c)
PY
Hits = “actively exploited flaw in your installed software” – the highest priority level from part 1. Automate this with the same timer mechanism.
Pro Corner
• OnCalendar syntax: Mon *-*-* 06:30:00 = every Monday;
*-*-01 07:00:00 = 1st of month, 07:00. Validate with systemd-analyze calendar '…'.
• Logs & state: systemctl list-timers | grep trivy and
journalctl -u trivy-weekly.
• Report diffs, not everything: first run = baseline; then report only new CVE IDs so you aren’t reminded of the same 20 open issues every week. Known issues belong on a separate “known” list.
• Multiple hosts: the script is host-agnostic — deploy it on each server, and all alerts land in one Telegram chat.
• Large estates: trivy server / OCI artifacts scale better, but for a
homelab the timer solution is plenty.
Why this matters: because “security” is not an event – it’s a rhythm. And from now on, the machine keeps the rhythm for you.
Next part: Part 6 – The foundation: host patching, kernel & Ubuntu Pro.