X-Git-Url: https://git.slaskete.net/einar-bin/blobdiff_plain/d18833ffbcb51a68973c973175c6e522624685c2..7cde7cf8f272d512d27ee5ec17e155692a4e9ac6:/addfollowmeprint.sh diff --git a/addfollowmeprint.sh b/addfollowmeprint.sh index 915d0a1..d435289 100755 --- a/addfollowmeprint.sh +++ b/addfollowmeprint.sh @@ -31,7 +31,7 @@ Driver="postscript" # Set other default options PrintServer="followprint.win.ntnu.no" -PrintName="FollowMe" +QueueName="FollowMe" Workgroup="WIN-NTNU-NO" while [[ $# -gt 0 ]]; do @@ -193,13 +193,9 @@ if [ "${Uname}" = "linux" ]; then fi fi -echo "This script will add a new printer called ${PrintName}, connecting to the +echo "This script will add a new printer called ${QueueName}, 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!" +password from NTNU." # Get username and password printf "User name: " @@ -213,8 +209,8 @@ stty "${Settings}" echo "" # Some further tests for Linux systems -# TODO: Add similar tests for macOS if [ "${Uname}" = "linux" ]; then + echo -e "\nNOTE: Your credentials will be stored in plaintext in /etc/cups/printers.conf.\nThis file us usually not readable for normal users, but still:\nDo not use this script on multi user systems!" # 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) @@ -231,11 +227,26 @@ if [ "${Uname}" = "linux" ]; then fi fi +# Similar tests for OSX +if [ "${Uname}" = "darwin" ]; then + smbutil view -A "//${Workgroup};${Username}:${Password}@${PrintServer}" > /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=$(smbutil view "//${Workgroup};${Username}:${Password}@${PrintServer}" 2>/dev/null | grep ${PrintShare} | cut -d " " -f 1) + if [ "${ShareFound}" != "${PrintShare}" ]; 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} \ + lpadmin -p ${QueueName} \ -D "FollowMe print queue at NTNU" \ -v "smb://${Username}:${Password}@${Workgroup}/${PrintServer}/${PrintShare}" \ -m "${DriverPath}" \ @@ -244,11 +255,24 @@ fi # The OSX way if [ "${Uname}" = "darwin" ]; then - lpadmin -p ${PrintName} \ + lpadmin -p ${QueueName} \ -D "FollowMe print queue at NTNU" \ - -v "smb://${Workgroup};${Username}:${Password}@${Workgroup}/${PrintServer}/${PrintShare}" \ + -v "smb://${PrintServer}/${PrintShare}" \ -m "${DriverPath}" \ + -o printer-is-shared=false -o printer-op-policy=authenticated \ -u allow:all -E + + cupsenable "${QueueName}" + cupsaccept "${QueueName}" + + # Add credentials to the keychain if they are missing + # Shamelessly stolen from https://github.com/Orakeltjenesten/scripts/blob/33abfb353524f449f0bbdee27adb2f1f0a9756a2/print/ntnuprint-mac.sh + if ! security find-internet-password -s ${PrintServer} >/dev/null 2>&1; then + security -v add-internet-password -a "${Workgroup}\\${Username}" -s ${PrintServer} \ + -w "${Password}" -D "Network Password" -r "smb " -l "${QueueName}" \ + -T /System/Library/CoreServices/NetAuthAgent.app -T 'group://NetAuth' \ + -T /System/Library/CoreServices/NetAuthAgent.app/Contents/MacOS/NetAuthSysAgent >/dev/null 2>&1 + fi fi if [ $? -ne 0 ]; then @@ -257,18 +281,18 @@ if [ $? -ne 0 ]; then fi # Set correct paper size and enable the duplexer option -lpadmin -p ${PrintName} -o PageSize=A4 -o Option1=True +lpadmin -p ${QueueName} -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." + printerror "Could not set default options on the print queue ${QueueName}. See above error for details." exit 1 fi # Set as default -lpadmin -d ${PrintName} +lpadmin -d ${QueueName} if [ $? -ne 0 ]; then - printerror "Could not set the print queue ${PrintName} as default printer. See above error for details." + printerror "Could not set the print queue ${QueueName} as default printer. See above error for details." exit 1 fi