Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Script the Login window through Apple Remote Desktop Network
After trying to script the login window multiple ways, I have come up with the perfect solution; this works 100% of the time when tested under OS X 10.4 and higher. We use this to log in computer(s) through Apple Remote Desktop by sending this script to computers sitting at the login window. This is great in a computer lab situation, where you may need to log in all the computers at once.

This method requires the username and password fields, but as long as you have a list of users with "Other..." displayed in the login window, it will work fine. Here's the script:
#!bin/sh
cat >/tmp/Login_Automatically.sh <<-"EOF"
if [[ "$(who | grep console)" == "" ]]; then # Runs ONLY if no one is logged in
osascript <<EOT
set username_ to "username"
set password_ to "password"
tell application "System Events"
key code 125 -- Down Arrow
key code 125 -- Down Arrow
delay 1
key code 125 -- Down Arrow
key code 125 -- Down Arrow
key code 125 -- Down Arrow
key code 125 -- Down Arrow
key code 125 -- Down Arrow
key code 125 -- Down Arrow
delay 0.5
key code 36 -- Return
delay 1
tell process "SecurityAgent" to set value of text field 1 of group 1 of window 1 to username_
tell process "SecurityAgent" to set value of text field 2 of group 1 of window 1 to password_
click button "Log In" of window 1 of application process "SecurityAgent"
end tell
EOT
fi
EOF
chmod 755 /tmp/Login_Automatically.sh
./tmp/Login_Automatically.sh
rm -f /tmp/Login_Automatically.sh
Some notes:
  • Initially the script will help "wake up" computers before attempting to go further by simulating a few Down Arrow keystrokes.
  • Any computers that are not on the username and password login window screen will be "encouraged" by clicking the Down Arrow multiple times and selecting that option. (It does not hurt anything to run this if the screen is already at the username/password fields.)
  • The script will fill in each field, and does not depend on the correct field being highlighted -- unlike other scripts we have seen and used in the past.
[robg adds: I haven't tested this one.]
    •    
  • Currently 2.17 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (18 votes cast)
 
[24,431 views]  

Script the Login window through Apple Remote Desktop | 29 comments | Create New Account
Click here to return to the 'Script the Login window through Apple Remote Desktop' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Script the Login window through Apple Remote Desktop
Authored by: palahala on Feb 16, '10 07:49:57AM

Just as a reference for a more basic script (which is language independent, I guess) script, see also "Logging In a User at the Login Window" at Apple's RemoteDesktop 3.0 Help.

(And if anyone knows how to log in as any user by using a super user's password, I'd love to hear that.)



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: patrickfergus on Feb 16, '10 08:20:30AM
I think the above hint requires enabling access for assistive devices. At least that's the error I got when I tried to use it. This may be an alternative without enabling access for assistive devices, but with the following caveats:

- Our login windows are username and password blanks, while the original hint includes code to reach the "Other" entry in a list-style login window

- The original hint includes code to deal with the screen saver/energy saver, mine assumes a computer at the ready

- If you have a non-Mac keyboard attached to the computer the script will likely not work. The keystrokes seem to be eaten rather than passed through

- I've used this only on 10.5, but I've used it a lot in 10.5


username=YYYYYYYY
password=XXXXXXXX

#Sanity checks
if [ $username = YYYYYYYY ]
then
	echo You need to set the username
	exit 1
fi

if [ $password = XXXXXXXX ]
then
	echo You need to set the password
	exit 2
fi

#Close the Bluetooth setup assistant, if there is one
osascript <<EOT
tell application "System Events"
        keystroke "w" using {command down}
end tell
EOT

#Wait a little bit for everything to work
sleep 5

#Type in the username, password, and press return
osascript <<EOT
tell application "System Events" to keystroke "$username"
tell application "System Events" to delay 2
tell application "System Events" to keystroke tab
tell application "System Events" to delay 2
tell application "System Events" to keystroke "$password"
tell application "System Events" to delay 2
tell application "System Events" to keystroke return
EOT


[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: Unsoluble on Feb 16, '10 10:13:25AM

Um, instead of sending downarrow a bunch of times, why not just send "o" "t" "h" "e" "r"?



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: applebit on Feb 16, '10 03:20:30PM

Actually the purpose was to wake the computer up. Some older computers are sluggish for response time. But excellent alteration!

I gotta tell ya, I'm excited to have finally found this solution! Life saving!

---
Jon McCullough
Systems Support Specialist



[ Reply to This | # ]
Why?
Authored by: slb on Feb 16, '10 01:25:28PM

Can someone post why this is needed or useful?
I don't grasp it as posted.

Thanks!



[ Reply to This | # ]
Why?
Authored by: patrickfergus on Feb 16, '10 01:47:16PM

- A script running DiskWarrior on a large number of computers via NetBoot

- Scripting a GUI backup tool (or any other tool that's primarily GUI-based)

- A training class that needs all the machines logged in and ready to go when the students walk in

Sometimes you just have to script the process as far as you possibly can and do the last step in the GUI.



[ Reply to This | # ]
Why?
Authored by: Unsoluble on Feb 16, '10 02:24:46PM
When you've got a lab or school or whatever full of Macs sitting at the login screen, there's no built-in method to get them to all log in at the same time, using Remote Desktop. So you need to send a script to accomplish this. FYI, here's the script I've been using, which works pretty well:

osascript <<EndOfMyScript
tell application "System Events"
keystroke "oth"
delay 5.0
keystroke return
delay 5.0
keystroke "admin"
keystroke return
delay 1.0
keystroke "password"
keystroke return
delay 1.0
keystroke return
end tell
EndOfMyScript


[ Reply to This | # ]
Why?
Authored by: slb on Feb 16, '10 05:37:58PM

Thanks much!



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: engelby on Feb 16, '10 02:09:32PM

You do need assistive devices enabled in order to do this. If you have ARD and want to do it, just push out the /var/db/.AccessibilityAPIEnabled file from your machine to turn it on.

This is what I used to do a few years back to log in to lots of machines at once through ARD. Same thing, just a lot less. If you want to click on Other, you can add keystrokes to type that instead.
you can add a "delay 1" in between lines if needed

osascript -e 'tell application "System Events" to keystroke "usernamegoeshere"'; \
osascript -e 'tell application "System Events" to keystroke tab'; \
osascript -e 'tell application "System Events" to keystroke "passwordgoeshere"'; \
osascript -e 'tell application "System Events" to keystroke return'



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: patrickfergus on Feb 16, '10 07:58:36PM

I think System Events' "keystroke" command doesn't require enabling access for assistive devices, however the original hint's script does (e.g. "click button "Log In"...").



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: ihadmeavision on Feb 16, '10 06:03:10PM

Great hint and link, thank you! Does anyone know what would happen if a student decided not to login but clicked in the password field and left. Since it's actually typing in the field and then tabs to the next field, could someone see your password? Curtain mode is broken in ARD (at least mine is) so could any of these potentially expose your password, if the fields get reversed? Would there be a way to make sure the first field selected is the username field? Thanks for any suggestions.



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: applebit on Feb 16, '10 07:09:15PM

That is where my original script owns any other solution that is out there. You can have incorrect fields selected and the script will select the appropriate field.

Also since it fills in the field rather then "types" in the field, it is a futher reason to use the top script I provided.

There is nothing wrong with other scripts - they do the job, but the one I worked long and hard to find a solution for is a 100% certainty that it will not expose passwords and will login properly.

I can't tell you enough how overjoyed I am to finally have this solution!

---
Jon McCullough
Systems Support Specialist



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: GuidoDelConfuso on Feb 16, '10 11:04:28PM
There is nothing wrong with other scripts - they do the job, but the one I worked long and hard to find a solution for is a 100% certainty that it will not expose passwords and will login properly.

Really? You can't think of a single attack vector against sending a shell script containing a plaintext password across the network to be executed (from a world-readable file with a known name) on a machine where it trusts implicitly that the operating system utilities haven't been compromised before typing its password into the second dialog box of any application that happens to be named "SecurityAgent"? Well, how about this one, for starters: mkfifo /tmp/Login_Automatically.sh ; cat /tmp/Login_Automatically.sh

Don't get me wrong. I'm sure your script performs admirably the job it's intended to do, and it may well be secure enough for your purposes. But let's not get cocky and claim with "100% certainty that it will not expose passwords". If anything, you're begging for your password to be stolen as soon as some inquisitive user with a bit more computer knowledge than scruples takes an interest in your remote login scheme. In other words, if you've already deployed this system in your average college computer lab--or, God forbid, a high school lab--then your password is already common knowledge among the student body.

[ Reply to This | # ]

Script the Login window through Apple Remote Desktop
Authored by: ecwh73 on Sep 06, '11 10:31:46AM

John,

What technique did you use to determine the field names in SecurityAgent? 10.7 has completely broken this functionality and I'm rather hurting here to fix it. We manage around a 1000 macs, mostly during production hours. The original Apple ARD script still works, but it's rather clumsy pushing strings into System Events without field references.

Given the format is just "text field 1 of group 1 of window 1", was it just a lucky guess?

I have developed a rather trivial way to push out password hash updates if required, but I would rather not have to need to due to a field population error.



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: applebit on Mar 20, '12 11:21:32AM
I have since discovered that 10.7 has broke this - an altered version worked for me this morning (even showing this to some of the other tech's in our office) then it broke - now if I run the same code by sending out via ARD it works... I submitted a bug report with Apple because we need this resolved!



sudo touch /var/db/.AccessibilityAPIEnabled
osascript <<EOT<br> tell application "System Events"
tell process "SecurityAgent"
set value of text field 1 of group 1 of window "Login" to "localpass"
set value of text field 2 of group 1 of window "Login" to "localadmin"
end tell
end tell
EOT


and the other option that WAS working but now isn't (and only a few short hours later!):


osascript <<EOT<br> tell application "System Events"
tell process "SecurityAgent"
repeat 2 times
delay 0.1
key code 48
delay 0.1
key code 51
end repeat
keystroke "username"
key code 48
keystroke "password"
repeat 3 times
delay 0.1
key code 36
end repeat
end tell
end tell
EOT


---
Jon McCullough
Systems Support Specialist


[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: applebit on Mar 20, '12 11:31:07AM
Just sent to Apple Bug Report:

ok, new info - if I send the command thru ARD it works and *THEN* it will work in the script - it appears as though "System Events" is not running until then. So either this was done by design or it is a bug. Either way it would be **GREATLY** helpful to know as this is a }}MAJOR{{ functionality of our environment and we are considering moving all 2000 intel machines to 10.7 in a few months.
---
Jon McCullough
Systems Support Specialist


[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: patrickfergus on Feb 16, '10 07:55:14PM

Restart the target computers--then you'll have the same field selected on each computer. Not elegant, but doesn't require enabling the accessibility for assistive devices. Luckily I'm not competing with curious kids who might go touch machines that spontaneously restart, so YMMV and enabling accessibility might be the way to go for you.



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: palahala on Feb 17, '10 08:57:37AM

Is there any reason to create a temporary script each time?



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: ihadmeavision on Feb 17, '10 11:59:58AM

Hi, I turned on the assistive devices option and it works as advertised with some errors. We have a part time person handling the Unix side of things, but is away for a while. Does anyone know why it returns the following errors? Why is Adobe even mentioned? Thanks.

2010-02-17 14:54:25.075 osascript[12612:903] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable imag /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.
button Log In of window 1 of application process SecurityAgent

Edited on Feb 17, '10 12:01:41PM by ihadmeavision



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: drudus on Mar 28, '10 07:51:18AM

FWIW, the Adobe error is described here.

http://kb2.adobe.com/cps/516/cpsid_51615.html

Hope it helps.



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: ihadmeavision on Mar 29, '10 05:42:58PM

This does help. Thanks a lot.



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: markoneil on Feb 24, '10 11:55:39AM

I am in no way a programmer or a scripter but had an idea i was working on and was able to use some of this script to update one i was working on.

Basically i want to be able to select a bunch of mac machines using apple remote desktop and then graphically log into all of them, using different accounts.

There are plenty of scripts available that will allow you to log in multiple computers using the same account through ard, but this does not do what i need it to do.

In many of the environments i work in there are consecutive network accounts. Lab01 through Lab60 for example. I needed to be able to script each selected computer logging in to a different consecutive account and then select a particular workgroup.

I was able to put bits and pieces of script together to perform the log in portion, and then a second script to select the workgroup, but i really like how this particular script handles the username and password fields, making sure that a password is not accidentally inserted into the username field and therefor making it visible for students or other users to see. Anyway here are the two scripts put together in what i am sure is horrible code. I would love to be able to create one script out of the two but need to make sure that the first portion waits until the workgroup selection window is active before proceeding with the second portion of the script. I could use a simple delay but depending on the environment, some computers may present this window quicker than others and i dont want to delay the quicker computers by having a delay time that will accomodate the slower computers.

Anyway here they are.

display dialog "This script will graphically login to a group of selected computers using unique consecutive usernames and their passwords. (lab01, lab02, lab03, etc...)" with title "Multi User GUI Login"
set num to 1 as text
set dialog_1 to display dialog "Please enter username prefix: (lab, fsa, test, etc...)" default answer "lab"
set the user_name to the text returned of dialog_1
set dialog_2 to display dialog "Please enter the password for these users: " default answer "12345" with hidden answer
set the pass_word to the text returned of dialog_2
tell application "Remote Desktop"
set theComputers to the selection
repeat with x in theComputers
set thescript1 to "echo -n \"a\" > /private/var/db/.AccessibilityAPIEnabled"
set thetask1 to make new send unix command task with properties {name:"Enable accessibility", script:thescript1, showing output:false, user:"root"}
execute thetask1 on x
delay 1
if length of num < 2 then
set num to "0" & num as text
end if
set thescript2 to "osascript -e 'tell application \"System Events\"' -e 'tell process \"SecurityAgent\" to set value of text field 1 of group 1 of window 1 to \"" & user_name & num & "\"' -e 'tell process \"SecurityAgent\" to set value of text field 2 of group 1 of window 1 to \"" & pass_word & "\"' -e 'click button \"Log in\" of window 1 of application process \"SecurityAgent\"' -e 'end tell'"
set thetask2 to make new send unix command task with properties {name:"Multigui Login", script:thescript2, showing output:false, user:"root"}
execute thetask2 on x
set num to num + 1 as text
end repeat
end tell


here is the second script

display dialog "This script will select and login to the workgroup specified on all selected computers in the following steps. " with title "Workgroup Selector"
set dialog_1 to display dialog "Please enter first few letters of desired workgroup: " default answer "student"
set the work_group to the text returned of dialog_1
tell application "Remote Desktop"
set theComputers to the selection
repeat with x in theComputers
set thescript2 to "osascript -e 'tell application \"System Events\"' -e 'keystroke \"" & work_group & "\"' -e 'delay 0.5' -e 'keystroke return' -e 'end tell'"
set thetask2 to make new send unix command task with properties {name:"Select workgroup", script:thescript2, showing output:false, user:"root"}
execute thetask2 on x
end repeat
end tell

Any input or help would be appreciated, In no way do i take credit for this script, just the idea.




[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: Swooper on May 03, '10 12:11:30PM

I must be missing something. None of these scripts have any effect on my local computers.

During summer upgrades, we frequently need to log into a large number of computers simultaneously. Similar scripts worked in years past but now, nothing.

Do I need to turn on this "access enabled" thing previously mentioned? I could use some help with that.



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: DudeWheresMyCar on Sep 10, '12 09:07:57PM

markoneil,
I need to do exactly what you're describing here, but I'm not sure how to get it running.

Could you give me some quick basics on where/how to save that script and then how to run it from the "Remote Desktop" program?

FYI, we're running 10.4 through 10.6.8 from a 10.7 server.

New wrinkle: I've got more than one lab computer prefix (e.g. emac##, imac##, etc.); guess I'll have to rename them, but that's fine with me.



[ Reply to This | # ]
10.7: Script the Login window through Apple Remote Desktop (updated for lion)
Authored by: ecwh73 on Sep 06, '11 01:11:12PM

if [[ "$(who | grep console)" == "" ]]; then # Runs ONLY if no one is logged in
touch /var/db/.AccessibilityAPIEnabled

osascript <<EOT

set username_ to "localadmin"
set password_ to "localpass"

tell application "System Events"
key code 125 -- Down Arrow
key code 125 -- Down Arrow
delay 1
key code 125 -- Down Arrow
key code 125 -- Down Arrow
tell process "SecurityAgent"
set value of text field 1 of window "Login" to password_
set value of text field 2 of window "Login" to username_
delay 0.5
key code 36 -- Return
delay 0.5
key code 36 -- Return
end tell
end tell
EOT
fi



[ Reply to This | # ]
10.7: Script the Login window through Apple Remote Desktop (updated for lion)
Authored by: applebit on Mar 08, '12 01:30:38PM

does this mean you found a solution? Because it appears for me that 10.7 is also broke and I cannot get it to work.

---
Jon McCullough
Systems Support Specialist



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: applebit on Mar 09, '12 08:35:52AM
Ok, here is updated code that has now been tested and works 10.5, 10.6, and 10.7. Enjoy! I suppose I could be ambitious and try it on the upcoming 10.8 but meh, it's not mainstream enough for me to put effort into it yet.


#!bin/sh
cat >/tmp/Login_Automatically.sh <<-"EOF"
loginwindow_username='username'
loginwindow_password='password'

if [[ "$(/usr/bin/who | /usr/bin/grep console)" == "" ]]; then # Run this
code ONLY if NO users are logged in
	osascript -e 'tell application "System Events"' -e 'repeat 2 times' -e
'repeat 10 times' -e 'key code 125 -- Down Arrow' -e 'end repeat' -e
'delay 1' -e 'end repeat' -e 'key code 36 -- Return' -e 'end tell'
	sleep 1
	OperatingSystem=$(sw_vers -productVersion | cut -c 1-4)
	if [[ "$OperatingSystem" != "10.7" ]]; then
		osascript -e 'tell application "System Events"' -e 'tell process
"SecurityAgent" to set value of text field 1 of group 1 of window 1 to
"'$loginwindow_username'"' -e 'tell process "SecurityAgent" to set value
of text field 2 of group 1 of window 1 to "'$loginwindow_password'"' -e
'click button "Log In" of window 1 of application process "SecurityAgent"'
-e 'end tell'
		rr=$?
	else
		osascript -e 'set username_ to "'$loginwindow_username'"' -e 'set
password_ to "'$loginwindow_password'"' -e 'tell application "System
Events"' -e 'tell process "SecurityAgent" to set value of text field 2 of
window 1 to username_' -e 'tell process "SecurityAgent" to set value of
text field 1 of window 1 to password_' -e 'key code 36' -e 'end tell'
		rr=$?
	fi
fi

EOF
chmod 755 /tmp/Login_Automatically.sh
./tmp/Login_Automatically.sh
rm -f /tmp/Login_Automatically.sh

---
Jon McCullough
Systems Support Specialist


[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: danta_1998 on Mar 29, '12 09:10:05AM

I have a simpler solution that works for me on multiple versions of Mac OS X. Try:

osascript <<EndOfMyScript
tell application "System Events"
keystroke "myUserName"
keystroke return
delay 0.5
keystroke "myPassword"
delay 0.5
keystroke return
end tell
EndOfMyScript

Naturally, you'll need to insert your particular username/password information into this script before using it. I've tested this successfully for 10.6.x and 10.7.x. I found this snippet online quite awhile back and can't remember where. If anyone recognizes this as their work, thanks.



[ Reply to This | # ]
Script the Login window through Apple Remote Desktop
Authored by: cashxx on Mar 07, '14 07:56:58AM

Was also having this problem and forgot about it because I haven't used it and some people on Macenterprise forums posted the fixes, the top two lines are added for the problematic OS's like Mt Lion, etc.

osascript -e 'tell application "SecurityAgent" to activate'; \
osascript -e 'tell application "Bluetooth Setup Assistant" to quit'; \
osascript -e 'tell application "System Events" to keystroke "yourUsername"'; \
osascript -e 'tell application "System Events" to keystroke tab'; \
osascript -e 'tell application "System Events" to delay 0.5'; \
osascript -e 'tell application "System Events" to keystroke "yourPassword"'; \
osascript -e 'tell application "System Events" to delay 0.5'; \
osascript -e 'tell application "System Events" to keystroke return'



[ Reply to This | # ]