OS X: timed logon to Mac using scripts

Source: Internet
Author: User


Remote Logon:
We all know that sometimes, to facilitate management/testing, for example, to test whether the AD user's logon configuration is correct, you need to log on to many computers. The following code can be used through ARD, etc, enables the remote computer to automatically log on to the specified user username.

[Python]
<Span style = "font-size: 16px;"> 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 </span>


Timed logon requirements:
The above Code cannot be executed if the system is automatically logged on at a scheduled time for specific management operations. Why can ARD be successful? Because, when you send a Unix Command from ARD, you select user root, that is, you can run the command in the root user environment.
Because Common Unix Commands cannot access the secure environment/namespace of the GUI program, unless they are in the same user environment where the GUI program is executed. If you need to use a script to access LoginWindow without user logon, you need to use special commands.

 

Rewrite script:
The launchctl bsexec <PID> <command and args> command uses the PID of the process to execute subsequent commands in other user environments.

You can rewrite the preceding command as follows:


[Python]
#! /Bin/sh
USERNAME = "username"
PASSWORD = "passsword"
 
Logger "AutoLogin Start .... "
Sleep 3
 
PID = 'ps-ax | grep loginwindow. app | grep-v grep | tail-n 1 | awk '{print $1 }''
 
Launchctl bsexec $ PID osascript-e "tell application \" System Events \ "to keystroke \"\""
Launchctl bsexec $ PID osascript-e "tell application \" System Events \ "to keystroke \" $ USERNAME \""
Sleep 2
Launchctl bsexec $ PID osascript-e "tell application \" System Events \ "to keystroke return"
Sleep 2
Launchctl bsexec $ PID osascript-e "tell application \" System Events \ "to keystroke \" $ PASSWORD \""
Sleep 2
Launchctl bsexec $ PID osascript-e "tell application \" System Events \ "to keystroke return"
 
Logger "AutoLogin End .... "
Exit 0

In this way, you can access LoginWindow without logon and enter a string for it.


Timing Mechanism:
It is better to use Launch daemon to implement the regular operation mechanism.

The key to the problem is to use the scheduled execution parameters provided by the Launch service, StartCalendarInterval, to define your own time.

The following is an example:


[Python]
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE plist PUBLIC "-// Apple // dtd plist 1.0 // EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<Plist version = "1.0">
<Dict>
<Key> StartCalendarInterval </key>
<Dict>
<Key> Minute </key>
<Integer> 54 </integer>
<Key> Hour </key>
<Integer> 9 </integer>
</Dict>
<Key> UserName </key>
<String> root </string>
<Key> Label </key>
<String> edu. ts. AutoLogin </string>
<Key> ProgramArguments </key>
<Array>
<String>/Library/Adminscripts/AutoLogin. sh </string>
</Array>
<Key> LaunchOnlyOnce </key>
<False/>
<Key> RunAtLoad </key>
<False/>
<Key> ServiceDescription </key>
<String> Auto Login Test </string>
</Dict>
</Plist>

Actual environment:
In the actual environment, you may encounter many problems, making the above script unable to run normally. The following are two common examples:

Screen Saver: If the Screen Saver is running when the above script is executed, it will eat part of the characters.
Logon window: the logon window must display the user name and password, not the user list. (Display login window as Name and password); secondly, the current cursor should be at Username; otherwise, it will not work. Www.2cto.com
Therefore, handling these "accidents" is a condition to ensure the success of the script. The simplest way is to restart the computer after the settings are completed.

 

Security Questions:
This script uses clear codes, which may cause security risks.

 

Other methods:
Another possible method is to get the/etc/kcpassword file after setting automatic logon on a machine. The password in the file is encrypted, and then set com. apple. the autoLoginUser in loginwidnow enables the system to automatically log on after the restart, compress all the logs into a pkg package, and release them to the client periodically in one way.

Author: afatgoat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.