Skip to main content
Solved

Script to remove specifically named printers...


Forum|alt.badge.img+18
  • Esteemed Contributor
  • 831 replies

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

Best answer by greps

Something like

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

could work.

View original
Did this topic help you find an answer to your question?

9 replies

Forum|alt.badge.img+7
  • Contributor
  • 50 replies
  • Answer
  • May 30, 2017

Something like

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

could work.


Forum|alt.badge.img+18
  • Author
  • Esteemed Contributor
  • 831 replies
  • May 30, 2017

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


Forum|alt.badge.img+10
  • Valued Contributor
  • 224 replies
  • May 30, 2017

Refer to my script Here


Forum|alt.badge.img+7
  • Contributor
  • 50 replies
  • May 30, 2017

@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
Forum|alt.badge.img+3

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

Forum|alt.badge.img+18
  • Author
  • Esteemed Contributor
  • 831 replies
  • May 30, 2017

@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


Forum|alt.badge.img+7
  • Contributor
  • 50 replies
  • May 30, 2017

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


Forum|alt.badge.img+18
  • Author
  • Esteemed Contributor
  • 831 replies
  • May 31, 2017

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


Forum|alt.badge.img+7
  • Contributor
  • 50 replies
  • June 1, 2017

@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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings