Skip to content

Instantly share code, notes, and snippets.

@FredCox3
Last active February 18, 2016 23:45
Show Gist options
  • Save FredCox3/73dfbba489c2d5a832d2 to your computer and use it in GitHub Desktop.
Save FredCox3/73dfbba489c2d5a832d2 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Printer Destroyer Script
# Freddie Cox - January 2016 for Knox County Schools
#
# Use Paramter 4 on JSS Script to remove a specific IP address.
echo "[INFO] printer_destroyer script. Parameter 4 Value: ${4}"
# Get full list of printers & search for socket connection using IP
printer=$(lpstat -s | grep 'socket.*'${4} | awk -F'/' '{print $3}')
# If Printer variable is empty, then don't act. Else. Try to remove
if [ -z $printer ]; then
# I didn't find anything. End it here.
echo "[INFO] Printer Not Found. No removal necessary."
else
# Printer IP was found. Lets try to remove it.
echo "[INFO] $printer Printer Found. Attempting to remove."
# Use lpstat command to find that printer again and get the name,
# then clean up the string by removing the trailing colon.
# After that, use the lpadmin -x command to remove the printer.
remove_printer=$(lpstat -s | grep 'socket.*'${4} | awk -F ' ' '{print $3}' | sed s/://g)
lpadmin -x $remove_printer
# Check if what we did actually worked.
printer_check=$(lpstat -s | grep 'socket.*'${4} | awk -F'/' '{print $3}')
if [ -z $printer_check ]; then
echo "[INFO] Success! Old Printer Removed."
else
echo "[ERROR] Something went wrong. Likly two printers with 1 IP. Otherwise, I'm all out of ideas"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment