Send Commands To All Installed Printers

Send Commands To All Installed Printers

Using a simple for  loop, you can send a PJL command to all of the printers installed on your machine.  For instance, if you wanted to change the display message on all your networked HP printers, you could loop through all the IP addresses and send it a command.

Finding The Printers

The command below will show you only the IP addresses of the printers (that are installed on your Mac), which you can then use in the for  loop.

lpstat -s | grep 'lpd\|ipp' | awk -F'/' '{print $3}'

If you want to find the types of devices on your network and their information, you will need something like nmap.

If you wanted to get just the USB printers, you could use this command:

lpstat -s | grep usb | awk -F':' '{print $NF}'

or if you just want to get every printer name:

lpstat -p | awk '{print $2}'

Change Settings On Physical Printers

If you want to change the settings on the printer itself–as you might do through a Web interface–you can send PJL commands, which will change the printer settings on the device.

Send PJL Commands To Every Networked Printer

Make a loop to send the PJL file to each IP address of the printers you have installed on your Mac.  This sends the PJL file to the printer on port 9100 using netcat.  The printer will recognize the file as commands and will execute them instead of printing the file.

allPrinterIPs=$(lpstat -s | grep lpd | awk -F'/' '{print $3}')
for p in ${allPrinterIPs[@]}
do
	cat changeReadyMessage.pjl | nc "$p" 9100
done

PJL commands can modify a lot of things: anything from duplex settings to changing serial numbers.

Change Printer Settings On A Computer

Alternatively, you can send out a script that will change the preferences of all installed printers on someone’s Mac.  You may run into issues if the printer does not support the command, but this can be useful in environments where all the printers are similar.

Enable Duplexing

You can use also lpadmin  to adjust settings of all installed printers.  Be careful here, too, as the Duplex command is probably different for every printer.  The command below is for a Brother 5340D as it was a simple example to illustrate how to change it.  If you want a more detailed example, check out this macmule article.

allPrinterNames=$(lpstat -p | awk '{print $2}')
for p in ${allPrinterNames[@]}
do
	# Use lpoptions -p PRINTER_NAME -l to see all available options for your printer--it will probably be different that what's below
	lpadmin -p "$p" -o Duplex=None
done

If you want to see what options are available to change on a specific printer, use this command:

lpoptions -p PRINTER_NAME -l

Disable Printer Sharing

If you wanted to systematically test if the printers are working and have the correct IP address by sending all printers a test page (showing the IP address of the machine), you could do this:

allPrinterNames=$(lpstat -p | awk '{print $2}')
for p in ${allPrinterNames[@]}
do
	lpadmin -p "$p" -o printer-is-shared=false
done

Systematically Print A Test Page On Every Networked Printer

If you wanted to systematically test if the printers are working and have the correct IP address by sending all printers a test page (showing the IP address of that device), you could do this:

allPrinterIPs=$(lpstat -s | grep lpd | awk -F'/' '{print $3}')
for p in ${allPrinterIPs[@]}
do
	echo "I think my IP adress is: $p" | lpr -P "$p" -p
done

Much easier than creating a document and manually sending it to each printer!