Script to remove specifically named printers...

GabeShack
Valued Contributor III

Im sure there is a grep and lpadmin command that will allow this, but before reinventing the wheel I figured I'd ask if anyone has already created this.

I have printers that all start with "HS-" added on our staff computers which we now need to delete so we can let users add the printers with our newer print server in self service.

So I was trying to get a script to just target the ones with those names and leave their others in tact.

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools
1 ACCEPTED SOLUTION

geoffrepoli
Contributor

Something like

lpstat -v | awk '/HS-/{print substr($3,1,length($3)-1)}' | xargs -I{} lpadmin -x {}

could work.

View solution in original post

9 REPLIES 9

geoffrepoli
Contributor

Something like

lpstat -v | awk '/HS-/{print substr($3,1,length($3)-1)}' | xargs -I{} lpadmin -x {}

could work.

GabeShack
Valued Contributor III

Syntax error in regular expression HS-{print substr($3)-1)} at } source line number 1
Is the error I get with that.

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools

Asnyder
Contributor III

Refer to my script Here

geoffrepoli
Contributor

@gshackney there's no regexp issues in that snippet above unless something was lost or formatted from copying and pasting it into your Terminal.

fwiw you can try this as well, which is functionally the same

lpstat -v | awk '/HS-/{gsub(/:/,"");print $3}' | xargs -I{} lpadmin -x {}

francktournant
New Contributor II

Not a "one line solution" but it works here :

#! /bin/bash

PRINTER_LIST=$(lpstat -v | grep "HS-")
PRINTER_LIST_COUNT=$(echo "${PRINTER_LIST}" | wc -l | bc)
for (( i = 1 ; i <= "${PRINTER_LIST_COUNT}" ; i++ )) ; do
    CURRENT_PRINTER_NAME=$(echo "${PRINTER_LIST}" | tail -n $i | head -n 1 | awk -F "device for " '{ print $2 }' | awk -F ": " '{ print $1 }')
    lpadmin -x "${CURRENT_PRINTER_NAME}"
done

GabeShack
Valued Contributor III

@grepoli You were correct, I had muffed the copy paste. Worked perfectly.
Actually looking at this I wonder if I can specify anything actually on the wrong print server. This is neat thanks all!

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools

geoffrepoli
Contributor

@gshackney np, what do you mean re: specifying anything? also say hi to Pam in the English dept for me. thats my mom!

GabeShack
Valued Contributor III

So 1st of all, I said hi and......
you need to call your mom more often...

2nd,
Can we grep what printer server the currently added printers are pointed to and only delete those? (However the printer server is not reflected in the name of the printer). The goal is we have an older printer server thats got "pk12" in the name and a newer printer server that has "hsps" in the name and we want to eliminate the printers that are added from the older server so we can have them re add the printers from the newer server using self service.

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools

geoffrepoli
Contributor

@gshackney , mission accomplished.

for the second part, unfortunately I don't really have any experience with print servers, so really don't know how to pull that info with the cups command line tools. anything useful when you run lpstat -h server:port ? Other than that, I've got nothing