Thursday, November 27, 2025

lpstat

# === STEP 1 – Run this block only once ===
mkdir -p ~/lpstat_history

cat > ~/lpstat_history/check_lpstat_and_mail.sh <<'EOF'
#!/bin/sh
# Printer Status Checker with Beautiful HTML Email
# Author: Abdul Muqeet

HOST=$(hostname)
DIR=~/lpstat_history
PRE="${DIR}/${HOST}_PRE.txt"
NOW="${DIR}/${HOST}_NOW.txt"
DATE=$(date '+%Y-%m-%d %H:%M:%S')

# Capture current printer status
lpstat -t > "$NOW"

# First run → save baseline
if [ ! -f "$PRE" ]; then
    cp "$NOW" "$PRE"
    cat <<MSG

════════════════════════════════════════
  BASELINE SAVED (Before Activity)
════════════════════════════════════════
File : $PRE
Time : $DATE
User : $(whoami)
Server: $HOST

Run the same command again AFTER the activity:
   ~/lpstat_history/check_lpstat_and_mail.sh

MSG
    exit 0
fi

# Second run → compare and send HTML email
if diff -q "$PRE" "$NOW" >/dev/null 2>&1; then
    STATUS="IDENTICAL"
    COLOR="#d4edda"
    BORDER="#28a745"
    ICON="Checkmark"
    MESSAGE="<strong>No changes detected.</strong><br>All printers are exactly the same as before the activity."
else
    STATUS="CHANGED"
    COLOR="#f8d7da"
    BORDER="#dc3545"
    ICON="Warning"
    MESSAGE="<strong>Printer configuration has changed!</strong><br>See detailed difference below:"
    DIFF=$(diff -u "$PRE" "$NOW" | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g')
fi

# Send HTML email
cat <<HTML | /usr/bin/mailx -t
From: Printer-Checker@$HOST <${USER}@${HOST}>
To: your.email@company.com
Subject: Printer Check – $STATUS – $HOST
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Printer Status – $HOST</title>
</head>
<body style="font-family: Arial, sans-serif; background:#f9f9f9; margin:0; padding:20px;">
  <div style="max-width:900px; margin:30px auto; background:white; border:3px solid $BORDER; border-radius:12px; overflow:hidden; box-shadow:0 4px 12px rgba(0,0,0,0.1);">
    <div style="background:$BORDER; color:white; padding:20px; text-align:center;">
      <h1>$ICON Printer Status Check</h1>
      <h2>$HOST – $STATUS</h2>
    </div>
    <div style="padding:25px; background:$COLOR;">
      <p><strong>Checked by :</strong> $(whoami)</p>
      <p><strong>Time       :</strong> $DATE</p>
      <p><strong>Server     :</strong> $HOST</p>
      <hr style="border:1px solid #ddd;">
      <p style="font-size:1.2em;">$MESSAGE</p>

      ${STATUS:+"CHANGED" && echo "<h3>Detailed Difference:</h3><pre style='background:#fff; padding:15px; border:1px solid #ccc; border-radius:8px; overflow:auto;'>$DIFF</pre>"}

      <hr style="margin:30px 0;">
      <p style="text-align:center; color:#555; font-size:0.9em;">
        Printer validation script v1.0<br>
        <strong>Author: Abdul Muqeet</strong>
      </p>
    </div>
  </div>
</body>
</html>
HTML

# Update baseline for next time
cp "$NOW" "$PRE"

echo ""
echo "════════════════════════════════════"
echo "   HTML EMAIL SENT → $STATUS"
echo "   Author: Abdul Muqeet"
echo "════════════════════════════════════"
echo "Ready for next check anytime."
EOF

chmod 700 ~/lpstat_history/check_lpstat_and_mail.sh

echo ""
echo "STEP 1 COMPLETED – Script installed successfully by Abdul Muqeet"
echo "Now proceed to Step 2 whenever you want to check printers."

No comments:

Post a Comment