-
Notifications
You must be signed in to change notification settings - Fork 5
printer_test_connection.sh
Tests network connectivity to configured printers with optional email alerting for failures.
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.
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.
- Operating System: macOS or Linux
- Network: Access to printer hosts
- Utilities: ping, host (for DNS diagnostics)
- Email (optional): sendmail command for alerts
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.
-
Header Display:
- Shows hostname, date, and number of printers to test
-
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
- For each printer in PRINTERS array:
-
Failure Tracking:
- Counts failed printers
- Builds comma-separated list of failed hosts
-
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
-
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.
- 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
- 0: All printers reachable
- 1: One or more printers unreachable
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 ]
--------------------------------------------------------------
-
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
- View Script Source
- Scripts - Back to script index