Posted on 05-23-2023 03:20 PM
Hello,
I am currently cleaning up our printers on our Macs. I have created a script to remove printers using lpadmin -x command. I created a script for six printers and noticed two of them do not get deleted. I am using the CUPs name in the script to delete unwanted printers. I believe these two printers that are not getting removed are very old and the CUPs name is not accurate so its just skipping the step to delete the printer.
These two printers are no longer in Jamf and all our employees are remote so trying to get the actual CUPs name is a bit difficult. Is there a script I can run to either delete the printers based on the Device URI ie: lpd://10.1.1.1 or another way where I can run a script to collect the CUPs name of the printers that won't delete using my current script? If I can somehow get the actual CUPs name of the printers then I can revise my current script to delete those two printers that are still hanging around.
Posted on 05-23-2023 04:49 PM
I ended up figuring it out. It helps to read the logs after you run your script. haha :)
Posted on 10-02-2023 10:04 AM
Can you provide me with the script that you were using? I know the name and the IP address of my printer that I am trying to remove but not having any luck with removing just one printer. I can get the script to remove all to work, but would like to just remove the ones I need to.
Posted on 10-02-2023 10:39 AM
@mramola either of the scripts I shared below would work.
For the first script, replace the IP address in the grep statement with the IP of the printer. If that IP address is found on the system it will get removed via the if/then statement. The 'oldPrinter' variable is running 'lpstat -s' to get the list of printers and their corresponding IP addresses. It then uses grep to look for the one specific IP you want.
The second script, change the "printer_name" variable in the if/then statement to be the name of the printer you are trying to remove. The *"pritner_name"* is looking for anything with "printer_name" in it and *"printer name"* is looking for anything with "printer name" in case someone added that printer with a space in the name.
The third script, just replace the entries in the "printer_array" with the name of the printer(s) that you want to remove.
Posted on 05-24-2023 09:05 AM
I know you solved this, but just to give you some ideas... you can use the IP address of the printer to delete it if you know the address. There is most likely an easier way to do this, but this is what I did back in the day and it worked:
#!/bin/bash
oldPrinter=$(/usr/bin/lpstat -s | grep 10.75.12.75 | awk {'print $3'} | sed s'/.$//')
if [[ ${oldPrinter} ]]; then
${lpa} -x ${oldPrinter}
fi
If you know the name of the printer, but not the upper/lower of the name, or if there are spaces in the name, you can use wildcards to delete.
#!/bin/bash
printers=$(lpstat -p | awk '{print $2}' | sed '/^$/d')
for i in "${printers[@]}"
do
if [[ ${i} == *"printer_name"* ]] || [[ ${i} == *"printer name"* ]]; then
lpadmin -x ${i}
fi
done
And I eventually came up with using a function and an array of printers for our scripts:
#!/bin/bash
function remove_printers {
# looping function to remove all printers passed
printer=("$@")
printers=$(/usr/bin/lpstat -p | awk '{print $2}' | sed '/^$/d')
echo "*** List: $printers"
for i in "${printer[@]}"
do
for j in "${printers[@]}"
do
if [[ ${j} == *"$i"* ]]; then
echo "*** Removing: $j"
/usr/sbin/lpadmin -x ${j} 2>/dev/null
fi
done
done
}
printer_array=(
"Printer 1"
"Printer 2"
)
remove_printers "${printer_array[@]}"