Posted on 02-08-2016 02:18 PM
Hi All,
I'm trying to install a brew install wget command as the current user and wrap it into a Self Service policy, i have this script but it always fails, the Mac has homebrew installed, any ideas?
#!/bin/sh
sudo -u $3 'brew install wget'
Thanks,
Solved! Go to Solution.
Posted on 02-09-2016 02:57 PM
@Sachin_Parmar I finally had a few minutes today to test this. I made the following minor change and it appeared to work for me:
#!/bin/sh
loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
echo $loggedInUser
sudo -u $loggedInUser brew install wget
I think that your install script was actually choking on the single quotes around the brew command. Once I removed the quote I was able to get brew to run and try to install wget for me. You can see the log:
Running script brewTest...
Script exit code: 0
Script result: swood
No entry for terminal type "unknown";
using dumb terminal settings.
Warning: wget-1.17.1 already installed
Hope that helps.
Posted on 02-08-2016 02:25 PM
@Sachin_Parmar the $3 variable is only populated during a login, I believe, so you would not be able to use that variable if the user is already logged in. Instead, use one of the many ways you can find on here to find the logged in user, like:
currentUser=`/usr/bin/last -1 -t console | awk '{print $1}'`
or
loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
The Apple approved way is documented in @bentoms post here: Get Currently Logged In User
loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
Using the Apple method, your script would look like:
#!/bin/sh
loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
sudo -u $loggedInUser 'brew install wget'
Posted on 02-08-2016 02:31 PM
Hi @stevewood thanks for the quick response i've been trying a mixture of those combinations and always come back to the following errors:
Posted on 02-08-2016 02:55 PM
@Sachin_Parmar well the error you are getting makes it look like brew is not installed on the machine you are trying to run it on. Have you verified that brew is installed first?
Posted on 02-08-2016 03:02 PM
@stevewood it certainly is if I run the policy via self service I get a error every time, if I open terminal and run the command manually it will work every time, I think it doesn't like the sudo part of the script that's the only thing I can think off as it's trying to run something similar to this and coming back with an error like so:
Posted on 02-09-2016 02:57 PM
@Sachin_Parmar I finally had a few minutes today to test this. I made the following minor change and it appeared to work for me:
#!/bin/sh
loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
echo $loggedInUser
sudo -u $loggedInUser brew install wget
I think that your install script was actually choking on the single quotes around the brew command. Once I removed the quote I was able to get brew to run and try to install wget for me. You can see the log:
Running script brewTest...
Script exit code: 0
Script result: swood
No entry for terminal type "unknown";
using dumb terminal settings.
Warning: wget-1.17.1 already installed
Hope that helps.
Posted on 02-10-2016 02:06 PM
Hi @stevewood,
Thank you so much for your help I had to make a modification to the script above but got it working eventually!
I just had to tell the script the location of brew effectively, so used yours and modified it with:
#!/bin/sh
loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`
echo $loggedInUser
sudo -u $loggedInUser /usr/local/bin/brew install wget
Working like a charm!
Posted on 02-10-2022 07:08 AM
I am trying the above for adobe or citrix brew from JAMF and i continue to fails with the below
Script result: Argument expected for the -c option
usage: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python [option] ... [-c cmd | -m mod | file | -] [arg] ... Try `python -h' for more information. /Library/Application Support/JAMF/tmp/Adobe Reader Brew: line 9: from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""] [username in [u"loginwindow", None, u""]]; sys.stdout.write(username + " ");: command not found sudo: unknown user: /usr/local/bin/brew sudo: error initializing audit plugin sudoers_audit
If i try using a the other method
sudo -u $3
i get
Script result: ==>
Downloading https://ardownload2.adobe.com/pub/adobe/reader/mac/AcrobatDC/2101120039/AcroRdrDC_2101120039_MUI.dmg
Already downloaded: /Users/stapa5/Library/Caches/Homebrew/downloads/668f9a222b98dfef4d114186221a945191259793e807676018dc2a1122eeb808--AcroRdrDC_2101120039_MUI.dmg ==>
Installing Cask adobe-acrobat-reader ==> Running installer for adobe-acrobat-reader; your password may be necessary. Package installers may write to any location; options such as
`--appdir` are ignored. sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper sudo: a password is required Error:
Failure while executing; `/usr/bin/sudo -E -- /usr/bin/env LOGNAME=stapa5 USER=stapa5 USERNAME=stapa5 /usr/sbin/installer -pkg /usr/local/Caskroom/adobe-acrobat-reader/21.011.20039/AcroRdrDC_2101120039_MUI.pkg -target /` exited with 1. Here's the output:
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper sudo: a password is required ==> Purging files for version 21.011.20039 of Cask adobe-acrobat-reader