Bash/Shell Scripting question

bmack99
Contributor III

I am not a scripter, I'll preface this post with that. My apologies if the answer is obvious.

History - we utilize TeamViewer in our environment. We are attempting to register the HOST with our account utilizing an API token. The below when run on a local system in the terminal runs and sets the account registration with no issues.(note the variables token$ and group$ are hard coded, I just left them out here).

/Applications/TeamViewerHost.app/Contents/Helpers/TeamViewer_Assignment -api-token token$ -group "group$" -grant-easy-access -reassign

If I put this command in a script in a separate policy and run the script after the Teamviewer HOST installation policy it does not set the account registration. The script runs, and finishes with exit code 0, but it's not set.

If I tie this command to the TeamViewer HOST package install policy in the Files and Process section, it shows in policy log that it runs, but again never sets the account assignment.

Thank you for any help with this

10 REPLIES 10

iJake
Valued Contributor

Please share your script in a code block so we can see it.

bmack99
Contributor III

Here is the script, the -api-token has been altered for security sake:

#!/bin/sh
/Applications/TeamViewerHost.app/Contents/Helpers/TeamViewer_Assignment -api-token 6654686-zD2iIBzHEWHSjz70Knsb -group "MAC" -grant-easy-access -reassign
exit 0

iJake
Valued Contributor

My guess is it that it's some sort of user context issue then. When you run it locally do you need sudo at all?

bmack99
Contributor III

Yes sudo is needed locally or it also does not make the assignment.

mm2270
Legendary Contributor III

I'm also guessing the command needs to be run as the current user, not as root. I haven't used this command myself, but I'm guessing if you did something like this it may work. I would try it and see.

#!/bin/bash

LoggedInUser=$(stat -f%Su /dev/console)
LoggedInUID=$(id -u "$LoggedInUser")

/bin/launchctl asuser $LoggedInUID sudo -iu "$LoggedInUser" /Applications/TeamViewerHost.app/Contents/Helpers/TeamViewer_Assignment -api-token 6654686-zD2iIBzHEWHSjz70Knsb -group "MAC" -grant-easy-access -reassign

bmack99
Contributor III

Thanks @mm2270 - I gave this a shot, but the script errors out with a 206 exit code:

Executing Policy Teamviewer HOST Account Assignment
Running script TeamViewer HOST Account Assignment...
Script exit code: 206
Script result:
Error running script: return code was 206.

mm2270
Legendary Contributor III

Oh well, it was an educated guess, a shot in the dark really. Sorry it didn't work. I'm not certain what makes that command line option tick so I don't have any insight into what would make it work in a Jamf policy.

If any other ideas pop into my head I'll post back with them.

ChrisCox
New Contributor III

Can you describe the behavior you see when you successfully run this command manually from Terminal? Also, what does the Jamf policy log show after this command runs via Jamf Pro? It may be worth, as a diagnostic measure, to get the return value from the command, checking if it is non-zero (success), and displaying the return value and exiting the script in error if so.

#!/bin/sh
/Applications/TeamViewerHost.app/Contents/Helpers/TeamViewer_Assignment -api-token 6654686-zD2iIBzHEWHSjz70Knsb -group "MAC" -grant-easy-access -reassign

# Get the return value
return_code="$?"

# Check if the return value is not zero
if [ "$return_code" != "0" ]; then

    # Report the error
    echo "$return_code"
    exit 1
fi

exit 0

bmack99
Contributor III

As an update, I ended up getting this script to work by changing the script to run as bash not sh, and by throwing in the sudo at the beginning(which I didn't think was needed due to how JAMF natively runs scripts/policies/packages etc). See below for functioning script:

#!/bin/bash

sudo /Applications/TeamViewerHost.app/Contents/Helpers/TeamViewer_Assignment -api-token 6654686-zD2iIBzHEWHSjz70Knsb -group "MAC" -grant-easy-access -reassign

exit 0

svalencia
New Contributor

I still had this issue with error 206 for a while in Jamf, although it would work manually in terminal. Finally found the reason and a fix, thanks to Theeter. The reason why the assignment tool doesn't work is because all the TeamViewer processes have not started up yet. You could put a sleep 15 in your script and that should do the trick. I tested that and it worked. But what they did was insert a while loop looking for those processes and only run the assignment tool once they were all running.

#!/bin/bash
APITOKEN=1234567-XXXXXXXXXXXXXXXXXXXX
GROUPID=g123456789
while true; do
    process=$(ps aux | grep TeamViewerHost | grep -v grep | wc -l)
        echo "Process: $process"
    if [ $process -gt 2 ]; then
        echo "Assigning..."
        sleep 15
                /Applications/TeamViewerHost.app/Contents/Helpers/TeamViewer_Assignment -api-token $APITOKEN -group-id $GROUPID -alias "$(hostname -s)" -grant-easy-access -reassign
        exit $?
    else
        echo "Waiting for TeamViewer to start..."
        sleep 15
    fi
done