Skip to content

printer_test_connection.sh

Corey Watson edited this page Dec 1, 2025 · 1 revision

printer_test_connection.sh

Tests network connectivity to configured printers with optional email alerting for failures.

Overview

This script performs connectivity tests to network printers by pinging their hostnames or IP addresses. It logs all results, reports success/failure for each printer, and can optionally send email alerts if any printers are unreachable.

Purpose

Provides automated printer connectivity monitoring for proactive support. Useful as a scheduled task or RMM script to verify printer availability before users experience printing issues.

Prerequisites

  • Operating System: macOS or Linux
  • Network: Access to printer hosts
  • Utilities: ping, host (for DNS diagnostics)
  • Email (optional): sendmail command for alerts

Configuration

Required Inputs

Edit these variables in the script before running:

PRINTERS Array:

PRINTERS=(
    "printer1.example.com"
    "printer2.example.com"
    "192.168.1.100"
)

List of printer hostnames or IP addresses to test.

Email Settings (optional):

  • SEND_EMAIL: Set to "true" to enable email alerts (default: "false")
  • MAIL_TO: Recipient email address (e.g., "admin@example.com")
  • MAIL_FROM: Sender email address (e.g., "noreply@example.com")
  • MAIL_SUBJECT: Email subject line (default includes hostname)

Leave MAIL_TO empty or set SEND_EMAIL to "false" to disable email alerts.

Behavior

  1. Header Display:
    • Shows hostname, date, and number of printers to test
  2. Connectivity Testing Loop:
    • For each printer in PRINTERS array:
      • Sends 2 ping packets with 5-second timeout
      • Logs PASS or FAIL to temporary log file
      • For failures: Performs DNS lookup and logs results
      • Displays result to console
  3. Failure Tracking:
    • Counts failed printers
    • Builds comma-separated list of failed hosts
  4. Email Alert (if enabled and failures occurred):
    • Checks for sendmail availability
    • Sends email with:
      • Subject line including hostname
      • List of failed printers
      • Full test log including DNS diagnostics
  5. Final Status:
    • If all tests pass: Displays success, deletes log, exits 0
    • If any test fails: Displays failure count and list, preserves log, exits 1

The script uses set -euo pipefail but handles ping failures gracefully.

Security Notes

  • No special privileges required
  • Log files stored in /tmp/ with timestamp
  • Email sent through local sendmail (no credentials in script)
  • Failed test logs preserved for troubleshooting
  • No sensitive data collected beyond network connectivity

Exit Codes

  • 0: All printers reachable
  • 1: One or more printers unreachable

Example Output

All Printers Reachable:

[ PRINTER CONNECTION TEST ]
--------------------------------------------------------------
Hostname   : workstation.example.com
Date       : Mon Jun  1 14:30:22 EDT 2025
Printers   : 3

[ TESTING CONNECTIVITY ]
--------------------------------------------------------------
Testing printer1.example.com... OK
Testing printer2.example.com... OK
Testing 192.168.1.100... OK

[ FINAL STATUS ]
--------------------------------------------------------------
Result : SUCCESS
All 3 printer(s) are reachable

[ SCRIPT COMPLETED ]
--------------------------------------------------------------

Some Printers Unreachable:

[ PRINTER CONNECTION TEST ]
--------------------------------------------------------------
Hostname   : workstation.example.com
Date       : Mon Jun  1 14:30:22 EDT 2025
Printers   : 3

[ TESTING CONNECTIVITY ]
--------------------------------------------------------------
Testing printer1.example.com... OK
Testing printer2.example.com... FAILED
Testing 192.168.1.100... FAILED

[ SENDING ALERT ]
--------------------------------------------------------------
Alert sent to admin@example.com

[ FINAL STATUS ]
--------------------------------------------------------------
Result : FAILURE
Failed : 2 printer(s) unreachable
Failed : printer2.example.com, 192.168.1.100
Log    : /tmp/printer_test_20250601_143022.log

[ SCRIPT COMPLETED ]
--------------------------------------------------------------

Version History

  • v1.0.0: Initial release
    • Multi-printer connectivity testing
    • Ping-based reachability checks
    • DNS diagnostics for failures
    • Optional email alerting
    • Detailed logging with timestamps
    • Support for hostnames and IP addresses

Links

Clone this wiki locally