简体   繁体   中英

Do login command with AppleScript

Prettty straightforward–Is there a way to do the login command and then enter the login password with do shell script?

For example, I want to login into the account user with the password name . How can I get the script to do login user and then input the password name (without opening a new Terminal window)? Maybe some bash script executable with

do shell script "./mybashscript.sh"

That does the login and inputs the password. I want it to do this invisibly .

In case you're referring to automating logging in as a different OSX user , as opposed to running a shell command as another user: I wouldn't know how to do that .

If you're referring to running shell commands or a shell script as another user , that's doable, although it requires administrative credentials - NOT the target user's password, but the password of a user with administrative privileges.

The following solutions simply execute whoami in the context of the target user, which echoes that user's username.

  • The SECURE way : prompt for administrative credentials :
set targetUser to "user1"
do shell script "sudo -H -s -u " & targetUser & " whoami" with administrator privileges
  • The INSECURE way : hard-code the administrative credentials :
set targetUser to "user1"
set adminUser to "admin"
set adminPasswd to "adminPassword" # !! NOT RECOMMENDED
do shell script "sudo -H -s -u " & targetUser & " whoami" ¬
  user name adminUser password adminPasswd with administrator privileges

Omit the user name adminUser clause if you want to default to the current user, assuming it is an administrator.

As for the options passed to sudo :

  • -s causes the command ( whoami , in this case) to be executed by a (non-login) shell (an instance of the current user's shell).
  • -H ensures that the $HOME environment variable is set to the target user's, which ensures that the target user's shell initialization files are read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM