]>
git.slaskete.net - einar-bin/blob - addfollowmeprint.sh
828cb32c59958f23e3a15e32650d589fb009358f
2 # This script installs the FollowMe print queue at NTNU on Linux (and possibly Mac) systems.
3 # The targeted and tested distros are: Debian, Ubuntu (and derivates), Fedora, CentOS, OpenSUSE and Mint
4 # Copyright © 2017-2019 einar.haraldseid@ntnu.no
8 echo "Usage: ./ $(basename "${0}") [OPTIONS]"
10 echo " -m, --model {ricoh|generic} Printer model to install (default: generic)"
11 echo " -d, --driver {pcl,postscript} Printer driver to use (default: postscript)"
12 echo " -f, --force Force running script as if on Linux systems"
13 echo " -p, --plaintext Store credentials as plaintext in /etc/cups/printers.conf (Linux only)"
14 echo " -h, --help Display this help text"
17 # Print errors to STDERR
18 function printerror
() {
23 if [ ${UID} -ne 0 ] ; then
24 echo "This script must be run as root, relaunching with sudo:"
29 # Set default options that may be overridden by passed options below
33 # Set other default options
34 PrintServer
= "followprint.win.ntnu.no"
35 PrintFile
= "ntnuprint-ricoh"
36 Workgroup
= "WIN-NTNU-NO"
39 while [[ $# -gt 0 ]]; do
44 Model
= $(echo "${2}" | tr "[:upper:]" "[:lower:]")
45 if [ " ${Model} " != "ricoh" ] && [ " ${Model} " != "generic" ]; then
46 printerror
"Unknown model ${Model} , please choose one of ricoh or generic"
49 shift # Jump to next argument
53 shift # Jump to next argument
56 Driver
= $(echo "${2}" | tr "[:upper:]" "[:lower:]")
57 if [ " ${Driver} " != "postscript" ] && [ " ${Driver} " != "pcl" ]; then
58 printerror
"Unknown driver ${Driver} , plase choose one of postscript or pcl"
61 shift # Jump to next argument
65 shift # Jump to next argument
73 echo "Unknown argument: ${Key} "
78 shift # past argument or value
81 # Test for supported OS
82 Uname
= $(uname | tr "[:upper:]" "[:lower:]")
83 if [ " ${Uname} " != "darwin" ] && [ " ${Uname} " != "linux" ] && [ " ${Force} " != "YES" ]; then
84 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."
88 # Set Uname to linux if we are forcing running the script
89 if [ " ${Force} " = "YES" ]; then
93 # Set printer driver path based on driver (we will override these if we're specific distros
94 if [ " ${Driver} " = "postscript" ]; then
95 DriverPath
= "drv:///sample.drv/generic.ppd"
97 DriverPath
= "drv:///sample.drv/generpcl.ppd"
101 if ! command -v lpadmin
> / dev
/ null
2 >& 1 ; then
102 printerror
"You must have CUPS installed to add printers. Please install CUPS."
106 # Tests for Linux systems
107 if [ " ${Uname} " = "linux" ]; then
108 # Test if smbclient is installed
109 if ! command -v smbclient
>/ dev
/ null
2 >& 1 ; then
110 printerror
"You must have smbclient installed to print to NTNU followprint. Please install smbclient."
114 # Test if secret-tool is installed
115 if ! command -v secret
-tool >/ dev
/ null
2 >& 1 ; then
116 printerror
"You must have secret-tool installed for this script to work. Please install libsecret-tools (Debian-systems) or secret-tool (Fedora/CentOS)."
120 # Try to determine if the necessary printer drivers are installed, and set correct driver paths
122 # This should match OpenSUSE, and probably SUSE Enterprise, and other derivates that use zypper as the primary package manager
123 if command -v zypper
>/ dev
/ null
2 >& 1 ; then
124 if [ " ${Driver} " = "postscript" ]; then
125 if [ " ${Model} " = "ricoh" ]; then
126 if ! rpm
-q OpenPrintingPPDs
-postscript >/ dev
/ null
2 >& 1 ; then
127 printerror
"You don't seem to have the correct printer drivers installed, please run:"
128 printerror
" sudo zypper install OpenPrintingPPDs-postscript"
129 printerror
"first, or use the generic driver instead."
132 DriverPath
= "OpenPrintingPPDs/postscript/Ricoh-MP_C6003.Postscript-Ricoh.ppd.gz"
135 DriverPath
= "Postscript.ppd.gz"
138 if ! rpm
-q OpenPrintingPPDs
-ghostscript >/ dev
/ null
2 >& 1 ; then
139 printerror
"You don't seem to have the correct printer drivers installed, please run:"
140 printerror
" sudo zypper install OpenPrintingPPDs-ghostscript"
141 printerror
"first, or use the generic driver instead."
144 if [ " ${Model} " = "ricoh" ]; then
145 DriverPath
= "OpenPrintingPPDs/ghostscript/Ricoh-MP_C6003.pxlcolor-Ricoh.ppd.gz"
147 DriverPath
= "OpenPrintingPPDs/ghostscript/Generic-PCL_6_PCL_XL_Printer.pxlcolor.ppd.gz"
152 # The rest of the systems can use drv:/// paths for the generic drivers, but we need to detect various distros for advice on installing Ricoh drivers
153 if [ " ${Model} " = "ricoh" ]; then
154 # This should match Fedora and other modern rpm based systems that have dnf as the primary package manager
155 if command -v dnf
>/ dev
/ null
2 >& 1 ; then
156 if ! rpm
-q foomatic
-db-ppds 2 >& 1 ; then
157 printerror
"You don't seem to have the correct printer drivers installed, please run:"
158 printerror
" sudo dnf install foomatic-db-ppds"
159 printerror
"first, or use the generic driver instead."
162 if [ " ${Driver} " = "postscript" ]; then
163 DriverPath
= "foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz"
165 DriverPath
= "foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz"
169 # This should match CentOS, RHEL and other RHEL based distros that have yum as the primary package manager
170 if command -v yum
>/ dev
/ null
2 >& 1 ; then
171 if ! rpm
-q foomatic
-db-ppds >/ dev
/ null
2 >& 1 ; then
172 printerror
"You don't seem to have the correct printer drivers installed, please run:"
173 printerror
" sudo yum install foomatic-db-ppds"
174 printerror
"first, or use the generic driver instead."
177 if [ " ${Driver} " = "postscript" ]; then
178 DriverPath
= "foomatic-db-ppds/Ricoh/PS/Ricoh-MP_C6003_PS.ppd.gz"
180 DriverPath
= "foomatic-db-ppds/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd.gz"
184 # This should match Debian, Ubuntu and most if not all derivates that use dpkg as the primary package manager
185 if command -v dpkg
>/ dev
/ null
2 >& 1 ; then
186 if ! dpkg
-s openprinting
-ppds > / dev
/ null
2 >& 1 ; then
187 printerror
"You must have the correct printer drivers installed, please run:"
188 printerror
" sudo apt-get install openprinting-ppds"
189 printerror
"first, or use the generic driver instead."
192 if [ " ${Driver} " = "postscript" ]; then
193 DriverPath
= "openprinting-ppds:0/ppd/openprinting/Ricoh/PS/Ricoh-MP_C6003_PS.ppd"
195 DriverPath
= "openprinting-ppds:0/ppd/openprinting/Ricoh/PXL/Ricoh-MP_C6003_PXL.ppd"
203 echo "This script will add a new printer called ${QueueName} , connecting to the
204 print server ${PrintServer} using your user name and password from NTNU."
206 # Get username and password
217 # Some further tests for Linux systems
218 if [ " ${Uname} " = "linux" ]; then
219 # Test for valid username and password
220 # Bonus: find out if the print share is actually available
221 PrintServerIP
= $(getent ahostsv4 ${PrintServer} | head -n 1 | cut -d " " -f 1)
222 if ! smbclient
-U " ${Workgroup} / ${Username} % ${Password} " -L "// ${PrintServer} " -I " ${PrintServerIP} " > / dev
/ null
2 >& 1 ; then
223 printerror
"User name or password incorrect, or no contact with the print server ${PrintServer} ."
226 ShareFound
= $(smbclient -U "${Workgroup}/${Username}%${Password}" -L "//${PrintServer}" -I "${PrintServerIP}" -g 2>/dev/null | grep ${PrintFile} | cut -d "|" -f 1)
227 if [ " ${ShareFound} " != "Printer" ]; then
228 printerror
"Could not find printer share called ${PrintFile} on the server"
229 printerror
"This script must be broken or outdated. Please contact orakel@ntnu.no for further assistance."
234 # Similar tests for OSX
235 if [ " ${Uname} " = "darwin" ]; then
236 if ! smbutil view
-A "// ${Workgroup} ; ${Username} : ${Password} @ ${PrintServer} " > / dev
/ null
2 >& 1 ; then
237 printerror
"User name or password incorrect, or no contact with the print server ${PrintServer} ."
240 ShareFound
= $(smbutil view "//${Workgroup};${Username}:${Password}@${PrintServer}" 2>/dev/null | grep ${PrintFile} | cut -d " " -f 1)
241 if [ " ${ShareFound} " != " ${PrintFile} " ]; then
242 printerror
"Could not find printer share called ${PrintFile} on the server"
243 printerror
"This script must be broken or outdated. Please contact orakel@ntnu.no for further assistance."
248 # Finally we can add the printer, let's remove any existing printer share with the same name first
249 lpadmin
-x ${QueueName} > / dev
/ null
2 &> 1
252 if [ " ${Uname} " = "linux" ]; then
253 if [ " ${Plaintext} " = "YES" ]; then
254 PrinterShare
= "smb:// ${Username} : ${Password} @ ${Workgroup} / ${PrintServer} / ${PrintFile} "
256 echo -e " \n NOTE: Your credentials will be stored in plaintext in /etc/cups/printers.conf. \n This is usually only necessary on headless systems or on systems that don't run a dbus-daemon and/or a keyring that can provide the org.freedesktop.secrets service. \n Needless to say, this is not a good idea on multi-user systems. \n "
258 PrinterShare
= "smb:// ${Workgroup} / ${PrintServer} / ${PrintFile} "
259 AuthInfo
= "username,password"
261 if ! lpadmin
-p ${QueueName} \
262 -D "FollowMe print queue at NTNU" \
263 -v " ${PrinterShare} " \
265 -o auth
-info-required = " $AuthInfo " \
266 -u allow
: all
-E ; then
267 printerror
"Could not connect to printer share. See above error for details."
270 if [ " ${Plaintext} " != "YES" ]; then
271 # Add credentials to the keyring
272 if ! echo -n " ${Password} " | secret
-tool store \
273 --label "ipp://localhost:631/printers/ $QueueName " \
274 uri
"ipp://localhost:631/printers/ ${QueueName} " \
275 user
" ${Username} " ; then
276 printerror
"Could not store credentials to the keyring, see above error for details. A workaround can be to use the option --plaintext"
282 if [ " ${Uname} " = "darwin" ]; then
283 if ! lpadmin
-p ${QueueName} \
284 -D "FollowMe print queue at NTNU" \
285 -v "smb:// ${PrintServer} / ${PrintFile} " \
287 -o printer
-is-shared = false
-o printer
-op-policy = authenticated \
288 -u allow
: all
-E ; then
289 printerror
"Could not connect to printer share. See above error for details."
293 cupsenable
" ${QueueName} "
294 cupsaccept
" ${QueueName} "
296 # Add credentials to the keychain if they are missing
297 # Shamelessly stolen^W^WBorrowed from https://github.com/Orakeltjenesten/scripts/blob/33abfb353524f449f0bbdee27adb2f1f0a9756a2/print/ntnuprint-mac.sh
298 # TODO: Since we should have a known-good username and password at this stage it's unwise to re-use the existing credentials, can we simply drop the test?
299 if ! security
find -internet-password -s ${PrintServer} >/ dev
/ null
2 >& 1 ; then
300 security
-v add
-internet-password -a " ${Workgroup} \\ ${Username} " -s ${PrintServer} \
301 -w " ${Password} " -D "Network Password" -r "smb " -l " ${QueueName} " \
302 -T / System
/ Library
/ CoreServices
/ NetAuthAgent.app
-T 'group://NetAuth' \
303 -T / System
/ Library
/ CoreServices
/ NetAuthAgent.app
/ Contents
/ MacOS
/ NetAuthSysAgent
>/ dev
/ null
2 >& 1
307 # Set correct paper size and enable the duplexer option
308 if ! lpadmin
-p ${QueueName} -o PageSize
= A4
-o Option1
= True
; then
309 printerror
"Could not set default options on the print queue ${QueueName} . See above error for details."
314 if ! lpadmin
-d ${QueueName} ; then
315 printerror
"Could not set the print queue ${QueueName} as default printer. See above error for details."
319 echo "Printer successfully installed. You may need to restart cups for the changes to take effect."