Skip to main content
Solved

Scripters Help! User Interaction Stored as Variable


Forum|alt.badge.img+20
  • Valued Contributor
  • 732 replies

Hey all, I wrote a script to add users to a certain dscl group and I was wondering how can I get an interaction pop up that says "Please Type in your Name" and the name is then saved as lets say $name?

Best answer by Matt11

#!/bin/bash

#####################################
# Get Current Logged In User        #
#####################################

user=`ls -l /dev/console | cut -d " " -f 4`

#####################################
# Move User to Local Admin Group    #
#####################################

dscl . append /Groups/admin GroupMembership $user

Looks like this works well. Wonder if this can automated with an Extension Attribute :D

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

40 replies

mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • March 29, 2012

Your best bet would be to invoke AppleScript to do that, with 'osascript': Need to head out right now, but if no-one else responds, I'll try to put something together for you as an example later.


Forum|alt.badge.img+7
  • Contributor
  • 28 replies
  • March 29, 2012

You can go the applescript route for this if you make a cocoa applet depending on how its going to be triggered

external image link

The following AppleScript / AppleScript Objective C code should work

activate
set askForUsername to display dialog "username" default answer "foo"
set username to text returned of askForUsername as string
set result to "<result>" & username & "</result>"
tell current application to NSLog(result)
quit

You could then do something like this:

external image link

declare -x name="$(/Applications/Username.app/Contents/MacOS/CocoaApplet 2>&1 | awk -F'[<>]' '{print $3}')"
bash-3.2# echo $name
foo

Might want to also check out this thread

https://jamfnation.jamfsoftware.com/discussion.html?id=4117


Forum|alt.badge.img+7
  • Contributor
  • 56 replies
  • March 29, 2012

I agree Apple script might the best bet. You can't just directly access the display dialog function, or it will return an error "No user interaction allowed. (-1713)"

To work around the issue, use Tell to system events:

#!/bin/bash
EndUserName=`/usr/bin/osascript <<EOT
tell application "System Events"
    activate
    set EndUserName to text returned of (display dialog "Enter your Name" default answer "")
end tell
EOT`
echo $EndUserName

I tested this with Casper Remote and it works when a user is logged in, but will hang if no one is logged in. I haven't tested through self service or from a scheduled event like at login.

Consider a check, or even a wait, until someone is logged in. With the script relying on end user input, it would be prudent to check the input before doing anything important with it.

If the input/name can be obtained via an automated means, it would seem to eliminate the need for extra checking and in many cases be preferred, and possibly more secure.


Forum|alt.badge.img+31
  • Honored Contributor
  • 2721 replies
  • March 29, 2012

Hey Jacob,

I have some scripts that do similar functionality for some projects I have worked and am currently working on. Your example is very similar to what I am doing. However, when testing on a 10.7.x box if I use System Events I always got some error message, but if I used the Finder it worked. I have zero clue if this is due to Lion's sandbox environment or not.

Have you tested your method in 10.7?

Many Thanks,
Tom


talkingmoose
Forum|alt.badge.img+36
  • Community Manager
  • 1901 replies
  • March 29, 2012

You can accomplish this if you use the Finder or even System Events to display the dialog:

#!/bin/sh

NAME=$(osascript <<-EOF

tell application "Finder"
    set theText to display dialog "What is your name?" default answer ""
end tell

text returned of theText

EOF)

echo $NAME

mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • March 29, 2012

I prefer using System Events instead of Finder because if the Finder is not the foreground application when it executes, it just bounces in the Dock. Using System Events causes the dialog to display over existing windows/applications (although the dialog can still be pushed to the background by the user afterwards)


Forum|alt.badge.img+31
  • Honored Contributor
  • 2721 replies
  • March 29, 2012

Hey guys,

Well I will go back and revisit System Events again, but when I was running these through testing it always kicked back an error. However, it only did it on my 10.7 test box. Though to be fair, my 10.7 test box gets abused slightly. :-)


Forum|alt.badge.img+7
  • Contributor
  • 56 replies
  • March 29, 2012

I did not think about 10.7 (We are still running 10.6.8 everywhere)

I choose "System Events" for the same reason: "Finder" likes to pop windows forward and/or bounce in the dock.

Though if you remove the "Activate" it might behave better.


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • March 30, 2012

What I am really trying to do is move users from our AD Admins group into the Local Admins group. I think I know where to start. I know I need something like this:

currentuser = 'stat -f%Su /dev/console'

and

dscl . append /Groups/Admin GroupMembership $currentuser

Basically my goal would be if the user is in the "ADADMINS" group they get both Network Admin and Local Admin rights. I'm thinking the best way to do this is via Self Service and just have the tech login the user, verify Admin rights and then click this script to envoke local admin.


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • Answer
  • March 30, 2012
#!/bin/bash

#####################################
# Get Current Logged In User        #
#####################################

user=`ls -l /dev/console | cut -d " " -f 4`

#####################################
# Move User to Local Admin Group    #
#####################################

dscl . append /Groups/admin GroupMembership $user

Looks like this works well. Wonder if this can automated with an Extension Attribute :D


Forum|alt.badge.img+31
  • Honored Contributor
  • 2721 replies
  • March 30, 2012

Matt,

If you use this as a log in hook policy or self service policy, the Casper Framework automatically sets the currently logged in user to $3. However, this only works when a policy is executed at log in/out or via self service. Every time a user logs in, you could check their membership against a group and then if you get a false or positive result an if/then statement could work to add them to that group.

Thanks,
Tom


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • March 30, 2012

Thanks Tom. Sounds like the same logic I was thinking. For now I am using this script in self service and just tested it a few times. Its great thus far. I added another script to run before with the jamf helper to give a message to the users as well.

#!/bin/bash

#####################################
# Warning Message                   #
#####################################

/usr/sbin/jamf displayMessage -message "This script must run logged in as the user. Please verify that user is in appropriate AD Group MACADMINS before proceeding. Running this script without the user being in the proper group will cause permanent data loss."
#!/bin/bash

#####################################
# Get Current Logged In User        #
#####################################

user=`ls -l /dev/console | cut -d " " -f 4`

#####################################
# Move User to Local Admin Group    #
#####################################

dscl . append /Groups/admin GroupMembership $user

Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • March 30, 2012

How about adding an if statement to check dscl for membership already? The only issue I see so far is that dscl will continue to add user records to the group even if it already exists. I'm going to give it a shot but if anyone can give me some if statement tips.

Using the script above my logic would be.

if $user is already in /Groups/admin then run jamf displaymessage

else

if $user is not in /Groups/admin then run jamf displaymessage and run dscl . append /Groups/admin GroupMembership $user

dscl . -read /Groups/admin GroupMembership | cut -d " " -f 2- | tr " " "
"

I know that brings up the users.

Thanks for all the help. Getting better at scripting everytime!


talkingmoose
Forum|alt.badge.img+36
  • Community Manager
  • 1901 replies
  • March 30, 2012

I like this basic "if" statement syntax:

if [ command ] ; then
    do something
fi

The "command" part must return something that's "true" or a result that you can parse.

The dseditgroup command allows you to check for group membership. Replace "username" with the name you're checking and "groupname" with the group you're checking:

dseditgroup -o checkmember -m username groupname

It will return one of three statements:

  • yes jcool is a member of admin
  • no jcool is NOT a member of admin
  • Unable to find the user record

You could use something like the following to check for "yes" in the result:

if [ dseditgroup -o checkmember -m jcool admin | grep yes ] ; then
    do something
fi

Many ways to skin a cat.


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • March 30, 2012

Let me give this a shot and see what I can dig up.


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • March 30, 2012

Ran into something interesting with the deseditgroup option.

If you are a Network Admin but not a "local" admin it still shows up as being a member of the admin group even though you are not in the /Groups/admin.

EDIT 1:

dseditgroup -o read -n /Local/Default admin

Does list correctly. When I run that command it shows all the local admins. Now how can I get that output in the if statement. :(


Forum|alt.badge.img+31
  • Honored Contributor
  • 2721 replies
  • March 30, 2012

Hey Matt,

Another neat trick you can use is to check the exit status of the last command you ran. If the command was successful it should exit with a 0, if not with a 1 or some other exit status number. See my example:

bash-3.2# dseditgroup -o checkmember -m tlarkin admin
yes tlarkin is a member of admin
bash-3.2# echo "$?"
0
bash-3.2# dseditgroup -o checkmember -m dude admin
Unable to find the user record
bash-3.2# echo "$?"
64

When I run the command `echo $?` it echoes the exit status of the previously ran command. You can see when it found my admin account it exited with 0, and when it did not find the account named "dude" it exited with 64.

You can use this method to build logic on figuring out if a member is in fact a member of a certain group with out have to deal with piping out any other commands. So for example:

#!/bin/sh
#use as login script with Casper

dseditgroup -0 checkmember -m $3 admin

if [[ `echo "$?"` -eq 0 ]]
  then echo "$3 is a member"
  else echo "$3 is not a member"
fi
exit 0

So you can logic your code in that manner. My example code just echoes out results and doesn't do anything else. You'd have to rewrite it to fit your needs, but the concept is decently solid. I have used it in the past for scripts to test against positive or negative output.

thanks,
Tom


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • March 30, 2012

The issue I am running into is the difference between being a Network Admin and a Local Admin. When using Active Directory Groups to handle Admin right it works until you unplug from the network; then you are back to a standard user. When I do "dseditgroup -0 checkmember -m $3 admin" it always says Yes because I am a Network Admin even though I am not in the "dscl . read /Groups/admin GroupMembership"

I tried doing this:

#!/bin/sh

user=`ls -l /dev/console | cut -d " " -f 4`
group=`dseditgroup -o read -n /Local/Default admin | grep -a $user`

if [ $user == $group ]; then
      result=Yes
else
      result=No
fi
echo "<result>$result</result>"

And when I expect Yes I get Yes!!!! When I get No though I get this error:

"/bin/bash: line 6: [: matthewle: unary operator expected
<result>No</result>"

I am getting the No I expect but whats this error mean??? At first I thought it said "Urinary operator". Thought maybe my computer had to take a leak :D


Forum|alt.badge.img+24
  • Valued Contributor
  • 1892 replies
  • March 30, 2012

That ```
[:
``` is telling you that the test is failing (that bit between the square brackets in your if statement)

What's failing is that one of your variables isn't getting set so it's tossing the error. I'd throw in an echo for each variable right before your if statement to see if they're getting set properly.


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • March 30, 2012

When the user is in /Groups/admin it works perfect.

When the user is not in /Groups/admin it reports No correctly just gives me that strange error. When I did echo for the $user it echo's an expected result, when you echo $group and you are not in the group it returns nothing at all and I think that's what causing the error.


Forum|alt.badge.img+13

try using a fuzzy regex match instead with =~ instead of ==.


Forum|alt.badge.img+13

(oops… double post)


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • March 30, 2012
#!/bin/sh

user=`ls -l /dev/console | cut -d " " -f 4`
group=`dseditgroup -o read -n /Local/Default admin | grep -a $user`

if [ $user =~ $group ]
then
   echo "Yes"
else
   echo "No"
fi

Returns the same error but also returns No as expected. I am pretty certain now the error is because its returning nothing (as it should because the user isn't in the group.)

EDIT 1:

#!/bin/sh

user=`ls -l /dev/console | cut -d " " -f 4`
group=`dseditgroup -o read -n /Local/Default admin | grep -a $user`

echo "$user"
echo "$group"

if [[ "$user" = "$group" ]] ; then
   echo "Yes"
else
   echo "No"
fi

I found an issue with whitespace it seems. The results are:

matthewle
<lots of whitespace>matthewle
No

Wondering if the whitespace is killing this.


Forum|alt.badge.img+13

probably. you're making an exact comparison with the == operator. " username" is not the same as "username," so your comparison fails.

try the regex match operator.


Forum|alt.badge.img+13

or use ruby, python, perl, etc. that all offer a "strip" method for strings that would save you a step.


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