Goal: Secure my Ubuntu system with regular virus scans using ClamAV.
What I Did:
Installed and configured ClamAV.
Wrote a bash script using
clamdscanto scan key directories.Scheduled the script to run weekly via
cron.Script logs results and quarantines infected files.
Skills Used: Bash scripting, Cron jobs, Log file management.
Challenges Solved: Making sure the scan didn’t overload system resources and keeping logs organized for review.
#!/bin/bash
# === ClamAV Automated Scan & Cleanup ===
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
LOG_FILE="$HOME/clamav_auto_scan.log"
SCAN_TARGET="/home/v"
START_TIME=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$START_TIME] 🔍 Starting system cleanup and scan..." >> "$LOG_FILE"
# --- System Cleanup ---
echo "Cleaning system packages..." >> "$LOG_FILE"
sudo apt clean && sudo apt autoremove -y >> "$LOG_FILE" 2>&1
sudo journalctl --vacuum-time=7d >> "$LOG_FILE" 2>&1
# --- ClamAV SCAN (Primary: clamdscan | Fallback: clamscan) ---
echo "[$START_TIME] 🔍 Starting scan of $SCAN_TARGET..." >> "$LOG_FILE"
run_zenity_success() {
END_TIME=$(date '+%Y-%m-%d %H:%M:%S')
zenity --info --title="✅ ClamAV Scan Complete" \
--text="Scan finished successfully at:\n$END_TIME"
}
run_zenity_error() {
END_TIME=$(date '+%Y-%m-%d %H:%M:%S')
zenity --warning --title="❌ ClamAV Scan Failed" \
--text="Scan failed at:\n$END_TIME\nSee log for details."
}
# Try clamdscan first
if clamdscan --fdpass "$SCAN_TARGET" >> "$LOG_FILE" 2>&1; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✅ clamdscan completed successfully." >> "$LOG_FILE"
run_zenity_success
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ⚠️ clamdscan failed. Trying clamscan..." >> "$LOG_FILE"
if clamscan -r "$SCAN_TARGET" >> "$LOG_FILE" 2>&1; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✅ clamscan completed successfully." >> "$LOG_FILE"
run_zenity_success
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ❌ Both scans failed." >> "$LOG_FILE"
run_zenity_error
fi
fi
Comments
Post a Comment