Repository navigation
Expand file tree
/
Copy pathsetup-printer
More file actions
executable file
·46 lines (37 loc) · 1.54 KB
/
Copy pathsetup-printer
File metadata and controls
executable file
·46 lines (37 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
set -euo pipefail
echo "==> Starting printer setup..."
# Ensure CUPS is running
echo "==> Ensuring CUPS service is enabled and started..."
sudo systemctl enable --now cups.service
# Prompt for manual or discovered IP
read -rp "Do you want to auto-discover printers via mDNS? [y/N] " autodiscover
if [[ "$autodiscover" =~ ^[Yy]$ ]]; then
echo "==> Scanning for mDNS printers on the network (using avahi)..."
avahi-browse -rt _ipp._tcp | grep '=' | grep IPv4 | awk -F';' '{ print $4 " (" $8 ")" }'
echo ""
read -rp "Enter the hostname of the printer from above (e.g., 'HP-OfficeJet.local'): " PRINTER_HOST
URI="ipp://$PRINTER_HOST/ipp/print"
else
read -rp "Enter the IP address of the printer (e.g., 192.168.1.100): " IP
read -rp "Protocol to use (ipp/socket) [ipp]: " PROTOCOL
PROTOCOL=${PROTOCOL:-ipp}
if [[ "$PROTOCOL" == "socket" ]]; then
URI="socket://$IP"
else
URI="ipp://$IP/ipp/print"
fi
fi
# Prompt for name and driver
read -rp "Enter a name for your printer (e.g., OfficePrinter): " PRINTER_NAME
read -rp "Enter a description (optional): " DESCRIPTION
read -rp "Enter the PPD or driver path (or leave blank for automatic): " DRIVER_PATH
echo "==> Adding printer with lpadmin..."
if [[ -z "$DRIVER_PATH" ]]; then
sudo lpadmin -p "$PRINTER_NAME" -E -v "$URI" -D "$DESCRIPTION" -m everywhere
else
sudo lpadmin -p "$PRINTER_NAME" -E -v "$URI" -D "$DESCRIPTION" -P "$DRIVER_PATH"
fi
echo "==> Setting printer as default..."
sudo lpoptions -d "$PRINTER_NAME"
echo "✅ Printer '$PRINTER_NAME' set up and made default."