From: Einar Jørgen Haraldseid Date: Fri, 3 Mar 2017 14:54:23 +0000 (+0100) Subject: A script to automatically add and configure network printer at NTNU X-Git-Url: https://git.slaskete.net/einar-bin/commitdiff_plain/ddec4238ec865cf90439380f90899737c9144198?hp=--cc A script to automatically add and configure network printer at NTNU --- ddec4238ec865cf90439380f90899737c9144198 diff --git a/addfollowmeprint.sh b/addfollowmeprint.sh new file mode 100755 index 0000000..7dce648 --- /dev/null +++ b/addfollowmeprint.sh @@ -0,0 +1,275 @@ +#!/bin/bash +# This script installs the FollowMe print queue at NTNU on Linux (and possibly Mac) systems. +# The targeted and tested distros are: Debian, Ubuntu (and derivates), Fedora, CentOS, OpenSUSE and Mint +# Copyright © 2017 einar.haraldseid@ntnu.no + +# Documentation +function usage { + echo "Usage: ./$(basename "${0}") [OPTIONS]" + echo "Options:" + echo " -m, --model {ricoh|generic} Printer model to install (default: generic)" + echo " -d, --driver {pcl,postscript} Printer driver to use (default: postscript)" + echo " -f, --force Force running script as if on Linux systems" + echo " -h, --help Display this help text" +} + +# Print errors to STDERR +function printerror() { + echo "${*}" 1>&2 +} + +# Test for root +if [ ${UID} -ne 0 ] ; then + echo "This script must be run as root, relaunching with sudo:" + sudo bash "$0" "$@" + exit $? +fi + +# Set default options that may be overridden by passed options below +Model="generic" +Driver="postscript" + +# Set other default options +PrintServer="followprint.win.ntnu.no" +PrintName="FollowMe" +Workgroup="WIN-NTNU-NO" + +while [[ $# -gt 0 ]]; do + Key="${1}" + + case ${Key} in + -m|--model) + Model=$(echo "${2}" | tr "[:upper:]" "[:lower:]") + if [ "${Model}" != "ricoh" ] && [ "${Model}" != "generic" ]; then + printerror "Unknown model ${Model}, please choose one of ricoh or generic" + exit 1 + fi + shift # Jump to next argument + ;; + -f|--force) + Force="YES" + shift # Jump to next argument + ;; + -d|--driver) + Driver=$(echo "${2}" | tr "[:upper:]" "[:lower:]") + if [ "${Driver}" != "postscript" ] && [ "${Driver}" != "pcl" ]; then + printerror "Unknown driver ${Driver}, plase choose one of postscript or pcl" + exit 1 + fi + shift # Jump to next option + ;; + -h|--help) + usage + exit 0 + ;; + *) + # unknown option + echo "Unknown option: ${Key}" + exit 1 + ;; + esac + + shift # past argument or value +done + +# Test for supported OS +Uname=$(uname | tr "[:upper:]" "[:lower:]") +if [ "${Uname}" != "darwin" ] && [ "${Uname}" != "linux" ] && [ "${Force}" != "YES" ]; then + printerror "I don't think I can run on ${Uname}, but if you insist, please use -f to force Linux detection. This may not work, though." + exit 1 +fi + +# Set Uname to linux if we are forcing running the script +if [ "${Force}" != "YES" ]; then + Uname="linux" +fi + +# Set printer share based on model (generic goes to ntnuprint-xerox) +if [ "${Model}" = "ricoh" ]; then + PrintShare="ntnuprint-ricoh" +else + PrintShare="ntnuprint-xerox" +fi + +# Set printer driver path based on driver (we will override these if we're specific distros +if [ "${Driver}" = "postscript" ]; then + DriverPath="drv:///sample.drv/generic.ppd" +else + DriverPath="drv:///sample.drv/generpcl.ppd" +fi + +# Test for CUPS +if ! command -v lpadmin > /dev/null 2>&1; then + printerror "You must have CUPS installed to add printers. Please install CUPS." + exit 1 +fi + +# Tests for Linux systems +if [ "${Uname}" = "linux" ]; then + # Test if smbclient is installed + if ! command -v smbclient >/dev/null 2>&1; then + printerror "You must have smbclient installed to print to NTNU followprint. Please install smbclient." + exit 1 + fi + + # Try to determine if the necessary printer drivers are installed, and set alternate driver paths + # This should match OpenSUSE, and probably SUSE Enterprise, and other derivates that use yast2 as the primary package + if command -v yast2 >/dev/null 2>&1; then + if [ "${Driver}" = "postscript" ]; then + if ! rpm -q OpenPrintingPPDs-postscript >/dev/null 2>&1; then + printerror "You don't seem to have the correct printer drivers installed, please run:" + printerror " sudo zypper install OpenPrintingPPDs-postscript" + printerror "first, or use the generic driver instead." + exit 1 + else + if [ "${Model}" = "ricoh" ]; then + DriverPath="OpenPrintingPPDs/postscript/Ricoh-MP_C6003.Postscript-Ricoh.ppd.gz" + else + DriverPath="OpenPrintingPPDs/postscript/Generic-PostScript_Printer.Postscript.ppd.gz" + fi + fi + else + if ! rpm -q OpenPrintingPPDs-ghostscript >/dev/null 2>&1; then + printerror "You don't seem to have the correct printer drivers installed, please run:" + printerror " sudo zypper install OpenPrintingPPDs-ghostscript" + printerror "first, or use the generic driver instead." + exit 1 + else + if [ "${Model}" = "ricoh" ]; then + DriverPath="OpenPrintingPPDs/ghostscript/Ricoh-MP_C6003.pxlcolor-Ricoh.ppd.gz" + else + DriverPath="OpenPrintingPPDs/ghostscript/Generic-PCL_6_PCL_XL_Printer.pxlcolor.ppd.gz" + fi + fi + fi + fi + # The rest of the tests ignore the model test (all other distros support drv:///) + if [ "${Model}" != "generic" ]; then + # This should match Fedora and other modern rpm based systems that have dnf as the primary package manager + if command -v dnf >/dev/null 2>&1; then + if ! rpm -q foomatic-db-ppds 2>&1; then + printerror "You don't seem to have the correct printer drivers installed, please run:" + printerror " sudo dnf install foomatic-db-ppds" + printerror "first, or use the generic driver instead." + exit 1 + else + if [ "${Driver}" = "postscript" ]; then + DriverPath="foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz" + else + DriverPath="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz" + fi + fi + fi + # This should match CentOS, RHEL and other RHEL based distros that have yum as the primary package manager + if command -v yum >/dev/null 2>&1; then + if ! rpm -q foomatic-db-ppds >/dev/null 2>&1; then + printerror "You don't seem to have the correct printer drivers installed, please run:" + printerror " sudo yum install foomatic-db-ppds" + printerror "first, or use the generic driver instead." + exit 1 + else + if [ "${Driver}" = "postscript" ]; then + DriverPath="foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz" + else + DriverPath="foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz" + fi + fi + fi + # This should match Debian, Ubuntu and most if not all derivates that use dpkg as the primary package manager + if command -v dpkg >/dev/null 2>&1; then + if ! dpkg -s openprinting-ppds > /dev/null 2>&1; then + printerror "You must have the correct printer drivers installed, please run:" + printerror " sudo apt-get install openprinting-ppds" + printerror "first, or use the generic driver instead." + exit 1 + else + if [ "${Driver}" = "postscript" ]; then + DriverPath="openprinting-ppds:0/ppd/openprinting/Ricoh/PS/Ricoh-MP_C6003_PS.ppd" + else + DriverPath="openprinting-ppds:0/ppd/openprinting/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd" + fi + fi + fi + fi +fi + +echo "This script will add a new printer called ${PrintName}, connecting to the +print server ${PrintServer} using your normal user name and +password from NTNU. + +NOTE: Your credentials will be stored in plaintext in /etc/cups/printers.conf. +This file us usually not readable for normal users, but still: +Do not use this script on multi user systems!" + +# Get username and password +printf "User name: " +read Username +printf "Password: " +Settings=$(stty -g) +stty -echo +read Password +stty "${Settings}" + +echo "" + +# Some further tests for Linux systems +# TODO: Add similar tests for macOS +if [ "${Uname}" = "linux" ]; then + # Test for valid username and password if on Linux + # Bonus: find out if the print share is actually available + PrintServerIP=$(getent ahostsv4 ${PrintServer} | head -n 1 | cut -d " " -f 1) + smbclient -U "${Workgroup}/${Username}%${Password}" -L "//${PrintServer}" -I "${PrintServerIP}" > /dev/null 2>&1 + if [ $? -ne 0 ]; then + printerror "User name or password incorrect, or no contact with the print server ${PrintServer}." + exit 1 + fi + ShareFound=$(smbclient -U "${Workgroup}/${Username}%${Password}" -L "//${PrintServer}" -I "${PrintServerIP}" -g 2>/dev/null | grep ${PrintShare} | cut -d "|" -f 1) + if [ "${ShareFound}" != "Printer" ]; then + printerror "Could not find printer share called ${PrintShare} on the server" + printerror "This script must be broken or outdated. Please contact orakel@ntnu.no for further assistance." + exit 1 + fi +fi + +# Finally we can add the printer + +# The Linux way +if [ "${Uname}" = "linux" ]; then + lpadmin -p ${PrintName} \ + -D "FollowMe print queue at NTNU" \ + -v "smb://${Username}:${Password}@${Workgroup}/${PrintServer}/${PrintShare}" \ + -m "${DriverPath}" \ + -u allow:all -E +fi + +# The OSX way +if [ "${Uname}" = "darwin" ]; then + lpadmin -p ${PrintName} \ + -D "FollowMe print queue at NTNU" \ + -v "smb://${Workgroup};${Username}:${Password}@${Workgroup}/${PrintServer}/${PrintShare}" \ + -m "${DriverPath}" \ + -u allow:all -E +fi + +if [ $? -ne 0 ]; then + printerror "Could not connect to printer share. See above error for details." + exit 1 +fi + +# Set correct paper size and enable the duplexer option +lpadmin -p ${PrintName} -o PageSize=A4 -o Option1=True + +if [ $? -ne 0 ]; then + printerror "Could not set default options on the print queue ${PrintName}. See above error for details." + exit 1 +fi + +# Set as default +lpadmin -d ${PrintName} + +if [ $? -ne 0 ]; then + printerror "Could not set the print queue ${PrintName} as default printer. See above error for details." + exit 1 +fi + +echo "Printer successfully installed."