Skip to main content
Solved

Bash Script to Add Printers


Forum|alt.badge.img+10
  • Valued Contributor
  • 224 replies

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:

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.

Best answer by tlarkin

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.

1#!/bin/bash
2
3# set multiple site enrollment via one quick add package
4
5getSite() {
6
7theSite=$(osascript <<AppleScript
8
9set mySites to {"Minneapolis", "Cupertino", "New York", "Hong Kong", "Amsterdam", "Eau Claire"}
10
11set selectedSite to {choose from list mySites}
12
13AppleScript
14
15echo "${theSite}"
16)
17}
18
19getSite
20
21echo "${theSite}"
22
23# now select invitation codes
24# put site names with spaces in single quotes
25
26case ${theSite} in
27 Minneapolis)
28 invCode='1234567890';;
29 Cupertino)
30 invCode='0987654321';;
31 'New York')
32 invCode='6789012345';;
33 'Hong Kong')
34 invCode='8901234567';;
35 Amsterdam)
36 invCode='3456789012';;
37 'Eau Claire')
38 invCode='4567890123';;
39esac
40
41echo "put your enrollment command here with code ${invCode}"
42
43exit 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:

View original
Did this topic help you find an answer to your question?

14 replies

Forum|alt.badge.img+7
  • Contributor
  • 98 replies
  • September 1, 2017

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
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3564 replies
  • September 1, 2017

@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).


Forum|alt.badge.img+10
  • Author
  • Valued Contributor
  • 224 replies
  • September 1, 2017

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)

1#!/bin/bash
2# Alex Snyder
3#North Fayette Valley CSD
4#9/1/17
5
6
7
8#Setup Printer Functions
9
10
11ES_Office () {
12
13cd /Users/Shared
14
15curl -LO http://myjss/CasperShare/Packages/bizhub_554e_109.pkg
16
17sudo installer -pkg bizhub_554e_109.pkg -target /
18
19
20lpadmin -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
21
22rm /Users/Shared/bizhub_554e_109.pkg
23}
24
25
26ES_Basement () {
27
28cd /Users/Shared
29
30curl -LO http://myjss/CasperShare/Packages/bizhub_C554_C364_109.pkg
31
32sudo installer -pkg bizhub_C554_C364_109.pkg -target /
33
34lpadmin -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
35
36rm /Users/Shared/bizhub_C554_C364_109.pkg
37}
38
39
40HS_Library () {
41
42cd /Users/Shared
43
44curl -LO http://myjss/CasperShare/Packages/bizhub_554e_109.pkg
45
46sudo installer -pkg bizhub_554e_109.pkg -target /
47
48lpadmin -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
49
50rm /Users/Shared/bizhub_554e_109.pkg
51}
52
53
54
55
56HS_Office () {
57
58cd /Users/Shared
59
60curl -LO http://myjss/CasperShare/Packages/bizhub_754_109.pkg
61
62sudo installer -pkg bizhub_754_109.pkg -target /
63
64lpadmin -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
65
66rm /Users/Shared/bizhub_754_109.pkg
67}
68
69
70F_Office () {
71
72cd /Users/Shared
73
74curl -LO http://myjss/CasperShare/Packages/bizhub_554e_109.pkg
75
76sudo installer -pkg bizhub_554e_109.pkg -target /
77
78
79lpadmin -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
80
81rm /Users/Shared/bizhub_554e_109.pkg
82}
83
84
85D_Office () {
86
87cd /Users/Shared
88
89curl -LO http://myjss/CasperShare/Packages/bizhub_C554_C364_109.pkg
90
91sudo installer -pkg bizhub_C554_C364_109.pkg -target /
92
93lpadmin -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
94
95rm /Users/Shared/bizhub_C554_C364_109.pkg
96}
97
98Remove_Printers () {
99
100####################################################
101
102# Delete old Copiers
103
104echo "--Current printers"
105lpstat -a | cut -d" " -f1 | while read PRTA
106do
107echo "-"$PRTA
108done
109
110lpstat -a | cut -d" " -f1 | grep "HS_Library" | while read PRTB
111do
112echo "-- Library printer detected attempting to delete: "$PRTB
113lpadmin -x $PRTB
114sleep 1
115done
116
117lpstat -a | cut -d" " -f1 | grep "Office" | while read PRTB
118do
119echo "-- Library printer detected attempting to delete: "$PRTB
120lpadmin -x $PRTB
121sleep 1
122done
123
124lpstat -a | cut -d" " -f1 | grep "Copier" | while read PRTB
125do
126echo "-- Library printer detected attempting to delete: "$PRTB
127lpadmin -x $PRTB
128sleep 1
129done
130
131echo "--Remaining printers"
132lpstat -a | cut -d" " -f1 | while read PRTC
133do
134echo "-"$PRTC
135done
136
137##########################################################
138}
139
140# A simple menu system
141names='HS_Office HS_Library ES_Office ES_Basement F_Office D_Office Delete_Printers Quit'
142PS3='Select Printer: '
143select name in $names
144do
145if [ $name == 'Quit' ]
146then
147break
148fi
149if [ $name == 'HS_Office' ]
150then
151HS_Office
152elif [ $name == 'HS_Library' ]
153then
154HS_Library
155elif [ $name == 'ES_Office' ]
156then
157ES_Office
158elif [ $name == 'ES_Basement' ]
159then
160ES_Basement
161elif [ $name == 'F_Office' ]
162then
163F_Office
164elif [ $name == 'D_Office' ]
165then
166D_Office
167elif [ $name == 'Delete_Printers' ]
168then
169Remove_Printers
170fi
171done
172echo Bye
173
174exit

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:


Forum|alt.badge.img+9
  • Contributor
  • 57 replies
  • September 1, 2017

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!


Forum|alt.badge.img+10
  • Author
  • Valued Contributor
  • 224 replies
  • September 1, 2017

@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.


Forum|alt.badge.img+9
  • Contributor
  • 57 replies
  • September 1, 2017

@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... :)


Forum|alt.badge.img+10
  • Author
  • Valued Contributor
  • 224 replies
  • September 1, 2017

@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.


Forum|alt.badge.img+8
  • New Contributor
  • 28 replies
  • September 1, 2017

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.)


Forum|alt.badge.img+24
  • Honored Contributor
  • 341 replies
  • September 2, 2017

@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
Forum|alt.badge.img+9
  • Contributor
  • 47 replies
  • September 3, 2017

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


Forum|alt.badge.img+31
  • Honored Contributor
  • 2721 replies
  • Answer
  • September 3, 2017

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.

1#!/bin/bash
2
3# set multiple site enrollment via one quick add package
4
5getSite() {
6
7theSite=$(osascript <<AppleScript
8
9set mySites to {"Minneapolis", "Cupertino", "New York", "Hong Kong", "Amsterdam", "Eau Claire"}
10
11set selectedSite to {choose from list mySites}
12
13AppleScript
14
15echo "${theSite}"
16)
17}
18
19getSite
20
21echo "${theSite}"
22
23# now select invitation codes
24# put site names with spaces in single quotes
25
26case ${theSite} in
27 Minneapolis)
28 invCode='1234567890';;
29 Cupertino)
30 invCode='0987654321';;
31 'New York')
32 invCode='6789012345';;
33 'Hong Kong')
34 invCode='8901234567';;
35 Amsterdam)
36 invCode='3456789012';;
37 'Eau Claire')
38 invCode='4567890123';;
39esac
40
41echo "put your enrollment command here with code ${invCode}"
42
43exit 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:


Forum|alt.badge.img+10
  • Author
  • Valued Contributor
  • 224 replies
  • September 5, 2017

@tlarkin I ended up with:

1#!/bin/bash
2
3#Variables
4
5
6#Setup Printer Functions
7
8ES_Office () {
9
10cd /Users/Shared
11
12curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_554e_109.pkg
13
14sudo installer -pkg bizhub_554e_109.pkg -target /
15
16
17lpadmin -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
18
19rm /Users/Shared/bizhub_554e_109.pkg
20}
21
22
23ES_Basement () {
24
25cd /Users/Shared
26
27curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_C554_C364_109.pkg
28
29sudo installer -pkg bizhub_C554_C364_109.pkg -target /
30
31lpadmin -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
32
33rm /Users/Shared/bizhub_C554_C364_109.pkg
34}
35
36
37HS_Library () {
38
39cd /Users/Shared
40
41curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_554e_109.pkg
42
43sudo installer -pkg bizhub_554e_109.pkg -target /
44
45lpadmin -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
46
47rm /Users/Shared/bizhub_554e_109.pkg
48}
49
50
51
52
53HS_Office () {
54
55cd /Users/Shared
56
57curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_754_109.pkg
58
59sudo installer -pkg bizhub_754_109.pkg -target /
60
61lpadmin -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
62
63rm /Users/Shared/bizhub_754_109.pkg
64}
65
66
67F_Office () {
68
69cd /Users/Shared
70
71curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_554e_109.pkg
72
73sudo installer -pkg bizhub_554e_109.pkg -target /
74
75
76lpadmin -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
77
78rm /Users/Shared/bizhub_554e_109.pkg
79}
80
81
82D_Office () {
83
84cd /Users/Shared
85
86curl -LO http://casper.n-fayette.k12.ia.us/CasperShare/Packages/bizhub_C554_C364_109.pkg
87
88sudo installer -pkg bizhub_C554_C364_109.pkg -target /
89
90lpadmin -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
91
92rm /Users/Shared/bizhub_C554_C364_109.pkg
93}
94
95Remove_Printers () {
96
97####################################################
98
99# Delete old Copiers
100
101echo "--Current printers"
102lpstat -a | cut -d" " -f1 | while read PRTA
103do
104echo "-"$PRTA
105done
106
107lpstat -a | cut -d" " -f1 | grep "HS_Library" | while read PRTB
108do
109echo "-- Library printer detected attempting to delete: "$PRTB
110lpadmin -x $PRTB
111sleep 1
112done
113
114lpstat -a | cut -d" " -f1 | grep "Office" | while read PRTB
115do
116echo "-- Library printer detected attempting to delete: "$PRTB
117lpadmin -x $PRTB
118sleep 1
119done
120
121lpstat -a | cut -d" " -f1 | grep "Copier" | while read PRTB
122do
123echo "-- Library printer detected attempting to delete: "$PRTB
124lpadmin -x $PRTB
125sleep 1
126done
127
128echo "--Remaining printers"
129lpstat -a | cut -d" " -f1 | while read PRTC
130do
131echo "-"$PRTC
132done
133
134##########################################################
135}
136
137
138
139
140
141
142# set multiple site enrollment via one quick add package
143
144getSite() {
145
146theSite=$(osascript <<AppleScript
147
148set mySites to {"ES Office", "ES Basement", "HS Office", "HS Library", "D Office", "Fayette", "Remove Printers"}
149
150set selectedSite to {choose from list mySites}
151
152AppleScript
153
154echo "${theSite}"
155)
156}
157
158getSite
159
160echo "${theSite}"
161
162# now select invitation codes
163# put site names with spaces in single quotes
164
165case ${theSite} in
166 'ES Office')
167 invCode='ES_Office';;
168 'ES Basement')
169 invCode='ES_Basement';;
170 'HS Office')
171 invCode='HS_Office';;
172 'HS Library')
173 invCode='HS_Library';;
174 'D Office')
175 invCode='D_Office';;
176 'Fayette')
177 invCode='F_Office';;
178 'Remove Printers')
179 invCode='Remove_Printers';;
180esac
181
182${invCode}
183
184exit 0

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


stevewood
Forum|alt.badge.img+35
  • Hall of Fame
  • 1798 replies
  • September 5, 2017

@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:

1printers=($(lpstat -p | awk '{print $2}' | sed '/^$/d'))
2for i in "${printers[@]}"
3do
4 if [[ ${i} == *"HS_Library"* ]]; then
5 lpadmin -x ${i}
6 elif [[ ${i} == *"Office"* ]]; then
7 lpadmin -x ${i}
8 elif [[ ${i} == *"Copier"* ]]; then
9 lpadmin -x ${i}
10 fi
11done

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.


Forum|alt.badge.img+10
  • Author
  • Valued Contributor
  • 224 replies
  • September 5, 2017

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings