Bash Script to Add Printers

Asnyder
Contributor III

I currently have a bash script to add printers for teachers, but would like to make it more user friendly.
I'd like to use applescript or automator to make a more friendly GUI. Also does anyone know how to insert a users pin into the account track settings of a printer? so far my script looks like:
0cc5ec835dd941a4882bf1754291ce0e

I have a function setup for each of the choices that installs the appropriate printer. Again what I'd like to do is make checkboxes or something so they can install multiple printers, along with having text box for them to input their pin# used for the copier, and take that pin and input it to the account track settings of the printer and save it.

1 ACCEPTED SOLUTION

tlarkin
Honored Contributor

I wrote this a super long time ago when I worked at Jamf. It was a POC to do a pop up to have users select their site to enroll into via an Applescript.

#!/bin/bash

# set multiple site enrollment via one quick add package

getSite() {

theSite=$(osascript <<AppleScript

set mySites to {"Minneapolis", "Cupertino", "New York", "Hong Kong", "Amsterdam", "Eau Claire"}

set selectedSite to {choose from list mySites}

AppleScript

echo "${theSite}"
)
}

getSite

echo "${theSite}"

# now select invitation codes
# put site names with spaces in single quotes

case ${theSite} in
    Minneapolis) 
        invCode='1234567890';;
    Cupertino)
        invCode='0987654321';;
    'New York')
        invCode='6789012345';;
    'Hong Kong')
        invCode='8901234567';;
    Amsterdam)
        invCode='3456789012';;
    'Eau Claire')
        invCode='4567890123';;
esac

echo "put your enrollment command here with code ${invCode}"

exit 0

You could just modify your code to do the Applescript part. Note I have only tested this locally not via the JSS or anything.

Here is what it looks like:

7c650fe85a864f90b5500c6f42e9d369

View solution in original post

14 REPLIES 14

Cornoir
Contributor II

For checkboxes you might want to use Xcode to create a GUI as well as install PPD files if the printer drivers are not already on the Macs. Xcode might sounds intimidating but it is just shell commands with a nice interface for the most part.

sdagley
Esteemed Contributor II

@Asnyder Is there a reason you don't want to have your users install printers by using a Jamf Pro policy they select in Self Service? If your print tracking software requires a hard coded user ID you'd still have to create a script to do that part of the configuration for you, but I don't have any specific advice on that (my site uses PaperCut which automatically pulls the logged in user's AD credentials to authenticate/bill the print job).

Asnyder
Contributor III

This is what I have so far, which works fine. I'm new to the job (started in April) and I'm pretty sure the guy before me didn't use self service to do it for a reason. Probably because you have to enter their account information manually anyway. (Most of our teachers wouldn't be able to handle that, to them updating their java is a hard task even with self service doing all the work)

#!/bin/bash
# Alex Snyder
#North Fayette Valley CSD
#9/1/17



#Setup Printer Functions


ES_Office () {

cd /Users/Shared

curl -LO http://myjss/CasperShare/Packages/bizhub_554e_109.pkg

sudo installer -pkg bizhub_554e_109.pkg -target /


lpadmin -p WUELEM_Office_Copier -E -v lpd://10.140.124.19/queue -L WUELEM_Office -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTAC554e.gz -o printer-is-shared=false -o PaperSources=PC410 -o Finisher=FS534_ZeusX -o KMPunchUnit=PK520-23_ZeusX -o PublicUserAccess=True -o KMDuplex=Single -o KMSection=True

rm /Users/Shared/bizhub_554e_109.pkg
}


ES_Basement () {

cd /Users/Shared

curl -LO http://myjss/CasperShare/Packages/bizhub_C554_C364_109.pkg

sudo installer -pkg bizhub_C554_C364_109.pkg -target / 

lpadmin -p WUELEM_Basement_Copier -E -v lpd://10.140.121.23/queue -L WUELEM_Basement -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTA364e.gz -o printer-is-shared=false -o PublicUserAccess=True -o KMSection=True -o KMDuplex=Single

rm /Users/Shared/bizhub_C554_C364_109.pkg
}


HS_Library () {

cd /Users/Shared

curl -LO http://myjss/CasperShare/Packages/bizhub_554e_109.pkg

sudo installer -pkg bizhub_554e_109.pkg -target /

lpadmin -p NFVHS_Library_Copier -E -v lpd://10.140.104.15 -L NFVHS_Library -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTA554e.gz -o printer-is-shared=false -o PaperSources=PC410 -o Finisher=FS534_ZeusX -o KMPunchUnit=PK520-23_ZeusX -o PublicUserAccess=True -o KMDuplex=Single -o KMSection=True

rm /Users/Shared/bizhub_554e_109.pkg
}




HS_Office () {

cd /Users/Shared

curl -LO http://myjss/CasperShare/Packages/bizhub_754_109.pkg

sudo installer -pkg bizhub_754_109.pkg -target /

lpadmin -p NFVHS_Office_Copier -E -v lpd://10.140.104.14 -L NFVHS_Office -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTA754e.gz -o printer-is-shared=false -o Finisher=FS534SD -o KMPunchUnit=PK520-23 -o PublicUserAccess=True -o KMDuplex=Single -o KMSection=True

rm /Users/Shared/bizhub_754_109.pkg
}


F_Office () {

cd /Users/Shared

curl -LO http://myjss/CasperShare/Packages/bizhub_554e_109.pkg

sudo installer -pkg bizhub_554e_109.pkg -target /


lpadmin -p Fayette_Office_Copier -E -v lpd://10.141.144.20/queue -L Fayette_Office -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTA454e.gz -o printer-is-shared=false -o PublicUserAccess=True -o KMDuplex=Single -o KMSection=True

rm /Users/Shared/bizhub_554e_109.pkg
}


D_Office () {

cd /Users/Shared

curl -LO http://myjss/CasperShare/Packages/bizhub_C554_C364_109.pkg

sudo installer -pkg bizhub_C554_C364_109.pkg -target / 

lpadmin -p District_Office_Copier -E -v lpd://10.140.130.15/queue -L District_Office -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTAC364e.gz -o printer-is-shared=false -o PublicUserAccess=True -o KMDuplex=Single -o KMSection=True -o Finisher=FS533

rm /Users/Shared/bizhub_C554_C364_109.pkg
}

Remove_Printers () {

####################################################

# Delete old Copiers

echo "--Current printers"
lpstat -a | cut -d" " -f1 | while read PRTA
do
echo "-"$PRTA
done

lpstat -a | cut -d" " -f1 | grep "HS_Library" | while read PRTB
do
echo "-- Library printer detected attempting to delete: "$PRTB
lpadmin -x $PRTB
sleep 1
done

lpstat -a | cut -d" " -f1 | grep "Office" | while read PRTB
do
echo "-- Library printer detected attempting to delete: "$PRTB
lpadmin -x $PRTB
sleep 1
done

lpstat -a | cut -d" " -f1 | grep "Copier" | while read PRTB
do
echo "-- Library printer detected attempting to delete: "$PRTB
lpadmin -x $PRTB
sleep 1
done

echo "--Remaining printers"
lpstat -a | cut -d" " -f1 | while read PRTC
do
echo "-"$PRTC
done

##########################################################
}

# A simple menu system
names='HS_Office HS_Library ES_Office ES_Basement F_Office D_Office Delete_Printers Quit'
PS3='Select Printer: '
select name in $names
do
if [ $name == 'Quit' ]
then
break
fi
if [ $name == 'HS_Office' ]
then
HS_Office
elif [ $name == 'HS_Library' ]
then
HS_Library
elif [ $name == 'ES_Office' ]
then
ES_Office
elif [ $name == 'ES_Basement' ]
then
ES_Basement
elif [ $name == 'F_Office' ]
then
F_Office
elif [ $name == 'D_Office' ]
then
D_Office
elif [ $name == 'Delete_Printers' ]
then
Remove_Printers
fi
done
echo Bye

exit

We don't use AD here. Everything is local on the Teachers Machine and we don't allow students to print from their laptops.

The place we put their pin's is here:

5e9b90f17f2c4c35b4352cf73a2d73f7

cgiordano
Contributor

If authentication isn't required then that should make it 100x easier to get them to adopt Self Service. If it's scoped to them then it'll just show up in whatever category that you have the policy under. If you want to make life even easier for them, you could make it a "featured" policy so it shows up on the home page of Self Service.

Obviously, it's easier for me to just say "do it this way," but I know it's not actually that easy. I might suggest opening a ticket with your TAM to see if they have any pointers. You're paying Jamf thousands per year, you might as well have them work for you. It might not be possible to grab the pin but it could just be a prefs file that you need to lay down after the printer and the driver(s).

For a simple printer install, here's the guide from the most current version of Jamf Pro v9.100:
http://docs.jamf.com/9.100.0/casper-suite/administrator-guide/Managing_Printers.html

I hope that helps. Good luck!

Asnyder
Contributor III

@cgiordano I think the main issue is that only some of the staff are enrolled. My boss doesn't like having teachers enrolled because it costs a license and licenses are expensive. ($30/per I think?). So that's another reason self service is a bit of a rough option. I think only my High school teachers are enrolled right now and I can't get him to budge for more licenses.

cgiordano
Contributor

@Asnyder Ah, yeah. That makes total sense then. If not everyone is enrolled then it could definitely be difficult to have it go through the JSS.

Looking at it with that lens, are you having the teachers enter the name or are you having them enter the number. I might suggest having them enter the number as opposed to the name so you minimize typos or use an "or" state in your IF statement to allow either or.

Just my two cents... :)

Asnyder
Contributor III

@cgiordano It uses the number. I'm trying to find a way to just have checkboxes to select which functions to run as opposed to having to use terminal to do.

canopimp
New Contributor III

I would recommend this https://github.com/haircut/self-service-printer-installer It takes a little bit to setup but once you have it configured, very easy for the user to select printers. I have our printers broken down by Manufacturer so there is a button in self service that runs the python script for our HP and another script attached to a button to install our xerox printers. If you wanted to have them all in one script you could. We just have a lot of printers on our campus. As long as the user can identify the printer in the list by name then it should be easy for them to add in the printer. (Might want to put label on the printer.)

mscottblake
Valued Contributor

@Asnyder You appear to know Bash well enough that I'll just leave this here. https://github.com/MScottBlake/mac_scripts/tree/master/installPrinterFromWindowsPrintServer

It should be easy to adapt this to use without a print server. Once that's done, you just create a different policy for each printer and you can offer them in Self Service.

jyergatian
Contributor

We use bash scripts inside policies to install printers. The bash scripts have a number of variables for the tech to fill in, making them easy to adapt for each printer. 1 Policy per printer keeps things sane.

https://github.com/macfanatic-ca/macOS_scripts/tree/master/Printers

tlarkin
Honored Contributor

I wrote this a super long time ago when I worked at Jamf. It was a POC to do a pop up to have users select their site to enroll into via an Applescript.

#!/bin/bash

# set multiple site enrollment via one quick add package

getSite() {

theSite=$(osascript <<AppleScript

set mySites to {"Minneapolis", "Cupertino", "New York", "Hong Kong", "Amsterdam", "Eau Claire"}

set selectedSite to {choose from list mySites}

AppleScript

echo "${theSite}"
)
}

getSite

echo "${theSite}"

# now select invitation codes
# put site names with spaces in single quotes

case ${theSite} in
    Minneapolis) 
        invCode='1234567890';;
    Cupertino)
        invCode='0987654321';;
    'New York')
        invCode='6789012345';;
    'Hong Kong')
        invCode='8901234567';;
    Amsterdam)
        invCode='3456789012';;
    'Eau Claire')
        invCode='4567890123';;
esac

echo "put your enrollment command here with code ${invCode}"

exit 0

You could just modify your code to do the Applescript part. Note I have only tested this locally not via the JSS or anything.

Here is what it looks like:

7c650fe85a864f90b5500c6f42e9d369

Asnyder
Contributor III

@tlarkin I ended up with:

#!/bin/bash

#Variables


#Setup Printer Functions

ES_Office () {

cd /Users/Shared

curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_554e_109.pkg

sudo installer -pkg bizhub_554e_109.pkg -target /


lpadmin -p WUELEM_Office_Copier -E -v lpd://10.140.124.19/queue -L WUELEM_Office -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTAC554e.gz -o printer-is-shared=false -o PaperSources=PC410 -o Finisher=FS534_ZeusX -o KMPunchUnit=PK520-23_ZeusX -o PublicUserAccess=True -o KMDuplex=Single -o KMSection=True

rm /Users/Shared/bizhub_554e_109.pkg
}


ES_Basement () {

cd /Users/Shared

curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_C554_C364_109.pkg

sudo installer -pkg bizhub_C554_C364_109.pkg -target / 

lpadmin -p WUELEM_Basement_Copier -E -v lpd://10.140.121.23/queue -L WUELEM_Basement -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTA364e.gz -o printer-is-shared=false -o PublicUserAccess=True -o KMSection=True -o KMDuplex=Single

rm /Users/Shared/bizhub_C554_C364_109.pkg
}


HS_Library () {

cd /Users/Shared

curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_554e_109.pkg

sudo installer -pkg bizhub_554e_109.pkg -target /

lpadmin -p NFVHS_Library_Copier -E -v lpd://10.140.104.15 -L NFVHS_Library -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTA554e.gz -o printer-is-shared=false -o PaperSources=PC410 -o Finisher=FS534_ZeusX -o KMPunchUnit=PK520-23_ZeusX -o PublicUserAccess=True -o KMDuplex=Single -o KMSection=True

rm /Users/Shared/bizhub_554e_109.pkg
}




HS_Office () {

cd /Users/Shared

curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_754_109.pkg

sudo installer -pkg bizhub_754_109.pkg -target /

lpadmin -p NFVHS_Office_Copier -E -v lpd://10.140.104.14 -L NFVHS_Office -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTA754e.gz -o printer-is-shared=false -o Finisher=FS534SD -o KMPunchUnit=PK520-23 -o PublicUserAccess=True -o KMDuplex=Single -o KMSection=True

rm /Users/Shared/bizhub_754_109.pkg
}


F_Office () {

cd /Users/Shared

curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_554e_109.pkg

sudo installer -pkg bizhub_554e_109.pkg -target /


lpadmin -p Fayette_Office_Copier -E -v lpd://10.141.144.20/queue -L Fayette_Office -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTA454e.gz -o printer-is-shared=false -o PublicUserAccess=True -o KMDuplex=Single -o KMSection=True

rm /Users/Shared/bizhub_554e_109.pkg
}


D_Office () {

cd /Users/Shared

curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_C554_C364_109.pkg

sudo installer -pkg bizhub_C554_C364_109.pkg -target / 

lpadmin -p District_Office_Copier -E -v lpd://10.140.130.15/queue -L District_Office -P /Library/Printers/PPDs/Contents/Resources/KONICAMINOLTAC364e.gz -o printer-is-shared=false -o PublicUserAccess=True -o KMDuplex=Single -o KMSection=True -o Finisher=FS533

rm /Users/Shared/bizhub_C554_C364_109.pkg
}

Remove_Printers () {

####################################################

# Delete old Copiers

echo "--Current printers"
lpstat -a | cut -d" " -f1 | while read PRTA
do
echo "-"$PRTA
done

lpstat -a | cut -d" " -f1 | grep "HS_Library" | while read PRTB
do
echo "-- Library printer detected attempting to delete: "$PRTB
lpadmin -x $PRTB
sleep 1
done

lpstat -a | cut -d" " -f1 | grep "Office" | while read PRTB
do
echo "-- Library printer detected attempting to delete: "$PRTB
lpadmin -x $PRTB
sleep 1
done

lpstat -a | cut -d" " -f1 | grep "Copier" | while read PRTB
do
echo "-- Library printer detected attempting to delete: "$PRTB
lpadmin -x $PRTB
sleep 1
done

echo "--Remaining printers"
lpstat -a | cut -d" " -f1 | while read PRTC
do
echo "-"$PRTC
done

##########################################################
}






# set multiple site enrollment via one quick add package

getSite() {

theSite=$(osascript <<AppleScript

set mySites to {"ES Office", "ES Basement", "HS Office", "HS Library", "D Office", "Fayette", "Remove Printers"}

set selectedSite to {choose from list mySites}

AppleScript

echo "${theSite}"
)
}

getSite

echo "${theSite}"

# now select invitation codes
# put site names with spaces in single quotes

case ${theSite} in
    'ES Office') 
        invCode='ES_Office';;
    'ES Basement')
        invCode='ES_Basement';;
    'HS Office')
        invCode='HS_Office';;
    'HS Library')
        invCode='HS_Library';;
    'D Office')
        invCode='D_Office';;
    'Fayette')
        invCode='F_Office';;
    'Remove Printers')
        invCode='Remove_Printers';;
esac

${invCode}

exit 0

Is there any way you know of with applescript to allow it to choose multiple "sites" from the box that pops up?

stevewood
Honored Contributor II
Honored Contributor II

@Asnyder you might be able to clean up your removal portion a little bit so that you're only callinglpstat once. This is a portion of script I use for looping through and removing printers:

printers=($(lpstat -p | awk '{print $2}' | sed '/^$/d'))
for i in "${printers[@]}"
do
    if [[ ${i} == *"HS_Library"* ]]; then
        lpadmin -x ${i}
    elif [[ ${i} == *"Office"* ]]; then
        lpadmin -x ${i}
    elif [[ ${i} == *"Copier"* ]]; then
        lpadmin -x ${i}
    fi
done

Using the asterisk (*) in front and behind each name will cause a wildcard match, so if you have the name that is in parenthesis anywhere in a printer name, that printer will get removed as well. You can remove the asterisks for an exact match.

Asnyder
Contributor III

@stevewood Thanks! That portion was written by my predecessor, I'll clean it up for sure.