Skip to content

Instantly share code, notes, and snippets.

@cyphunk
Last active February 7, 2019 17:40
Show Gist options
  • Save cyphunk/a0d19a17a893cca2a54530459407737c to your computer and use it in GitHub Desktop.
Save cyphunk/a0d19a17a893cca2a54530459407737c to your computer and use it in GitHub Desktop.
sick of printer managers
#!/usr/bin/env bash
# setup and destroy printer
# sudo call integrity check: only root should be able to change script
l=($(/usr/bin/ls -l `/usr/bin/readlink -f $0`))
[ ${l[0]:2:1} != "-" ] && [ "${l[2]}" != "root" ] ||
[ ${l[0]:5:1} != "-" ] && [ "${l[3]}" != "root" ] ||
[ ${l[0]:8:1} != "-" ] && { echo -e "only root should be able to modify\n${l[@]}"; exit 1;}
read -p "Setup printer? [y]/n " YN
if [ "$YN" != "n" ]; then
echo "Select backend"
select BACKEND in snmp usb; do break; done
IFS=$'\n'
if [[ "$BACKEND" == "snmp" ]]; then
# search for printers
# in future replace with sudo lpinfo -v ?
sudo avahi-daemon &
while [ 1 ]; do
sleep 2
echo -en "."
PRINTERIPS=$(
avahi-browse --resolve --terminate --all --parsable | fgrep -i 'printer' | grep ^= | gawk -F\; '{print $8}' | sort -u )
test -n "$PRINTERIPS" && echo && break
done
sudo killall avahi-daemon
sleep 1
echo -e "\nSelect printer IP address:"
select IP in $PRINTERIPS; do break; done
backend=$(/usr/lib/cups/backend/snmp $IP)
else
backend=$(sudo /usr/lib/cups/backend/usb)
fi
#echo "$backend"
URI=$(echo "$backend" | awk '{print $2}')
NAME=$(echo "$backend" | awk -F '"' '{print $2}')
test -n "$NAME" || NAME="ERROR"
echo "uri: $URI"
echo "name: $NAME"
sudo systemctl start org.cups.cupsd.service
DRIVERS=$(lpinfo -m | grep -i "$NAME")
select _DRIVER in $DRIVERS; do break; done
DRIVER=$(echo "$_DRIVER" | awk '{print $1}')
echo -e "driver:\n$DRIVER"
read -p "continue?"
set -x
sudo lpadmin -p PRINTER -E -v $URI -m $DRIVER
set +x
echo
echo "OPTIONS"
lpoptions -d PRINTER -l
echo
fi
sudo systemctl start org.cups.cupsd.service
echo "use:"
echo lpr -P PRINTER -o ColorModel=CMYGray -o PageSize=A4 -o page-ranges=1 FILE
echo
YN="n"
read -p "Stop/del printing? y/[n] " YN
if [ "$YN" == "y" ]; then
sudo lpadmin -x PRINTER
lpstat -s #list queues
sudo systemctl stop org.cups.cupsd.service
fi
# check queue
#lpq -a
# remove entry
#lprm
#lprm - # remove all
# non standard driver packages
#ap list foomatic-db-nonfree-ppds # no hp
#ap list foomatic-db-nonfree # no hp
#ap list foomatic-db-gutenprint-ppds # not installed yet
#ap list hplip # not installed yet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment