OS X: 用指令碼定時登入Mac電腦

來源:互聯網
上載者:User

遠程登入:

大家知道有時為了管理/測試方便,比如測試AD使用者登入配置是否正確,需要登入到眾多電腦上,使用下面的代碼可以通過ARD等方式,使遠程電腦自動登入到指定的使用者username。

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 


定時登入需求:

如果需求是無人職守地定時讓系統自動登入,以便進行特定的管理操作,那麼上面的代碼無法執行。而為什麼ARD可以成功呢?因為,在你從ARD發送Unix命令的時候,選擇了使用者root,也就是說使用root的使用者環境來執行該命令。

因為普通的Unix命令無法訪問GUI程式的安全環境/名字空間,除非在執行該GUI程式的相同使用者環境裡。如果需要使用指令碼的方式在無使用者登入的時候,訪問LoginWindow,就需要使用特殊的命令。


重寫指令碼:

launchctl bsexec <PID> <command and args>命令就是通過該進程的PID,使用其它的使用者環境執行後面的命令。

於是可以將上面的命令改寫如下:

#!/bin/sh USERNAME="username"PASSWORD="passsword" logger "AutoLogin Start…."sleep 3PID=`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

這樣就可以在無人登入的時候訪問LoginWindow,並對它輸入字串。


定時機制:

定時運行機制,還是使用Launch daemon來完成的好。

問題的關鍵就是使用Launch服務提供的定時執行的參數,StartCalendarInterval,定義好自己使用的時間。

下面是一個例子:

<?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>

實際環境:

實際環境中,可能遇到很多問題,使得上面的指令碼無法正常運行。下面時比較常見的兩個:

  • 螢幕保護裝置程式: 如果在執行上面的指令碼時,螢幕保護裝置程式在運行,那麼它會吃掉部分字元。
  • 登入視窗:登入視窗需要是顯示使用者名稱和密碼,不是使用者列表。(Display login window as Name and password);其次,當前游標應該在Username處,否則也是不工作。

所以,處理好這些"意外"是保證指令碼順利成功的條件。最簡單的方法就是設定好後重新啟動電腦。


安全問題:

因為該指令碼中使用明碼,所以可能導致安全隱患。


其它方法:

另外一個可能的方法是,在一台機器上設定好自動登入後,得到/etc/kcpassword檔案,裡面的密碼是加密的,然後設定com.apple.loginwidnow中的autoLoginUser,這樣可以讓系統在重新啟動後自動登入,把所有這些打成pkg包,使用一個方式定時發布到用戶端。


在此感謝Mike Lynn的協助


Tony Liu

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.