Remote ARD Kick via SSH

So I’ve written a little script (only my second one) to automate a remote ARD kickstart on Macs on the same LAN via SSH.
It works thusly :

  1. Pings the entered hostname to check for machine availability. If the machine is available, the admin is able to proceed. If the machine is unavailable, the admin has the option to try again or try another name.

  2. Collects SSH credentials (should be root as well).

  3. Begins SSH session with given credentials. Agrees to RSA notice (sometimes not needed).

  4. Sends kickstart commands with root credentials provided earlier.

  5. Closes SSH session when done.

Anyway, I hope it’s useful for some of you. Works for me. Any input or modification for special needs is encouraged!


# Enter Machine Hostname
repeat
	set HostName to ""
	set Dialog_1 to display dialog "Enter Mac Hostname:" default answer ""
	set HostName to text returned of Dialog_1
	
	# Ping Hostname to determine availability
	try
		set ping to (do shell script "ping -c 1 " & HostName)
		set Dialog_2 to display dialog "Machine is Available" buttons "Continue" default button "Continue"
	on error
		set Dialog_3 to display dialog "Machine is Unavailable" buttons {"Next.", "Cancel"} default button "Next."
	end try
	if the button returned of the result is "Continue" then
		exit repeat
	end if
end repeat

# Gather SSH Credentials
if the button returned of Dialog_2 is "Continue" then
	set AdminName to ""
	set Dialog_4 to display dialog "Admin name:" default answer ""
	set AdminName to text returned of Dialog_4
	set PassKey to ""
	set Dialog_5 to display dialog "Admin Password:" default answer "" with hidden answer
	set PassKey to text returned of Dialog_5
end if

# Open SSH Session
tell application "Terminal"
	activate
	set ssh to (do script "ssh " & AdminName & "@" & HostName)
	delay 3
	(do script "yes" in window 1)
	delay 3
	(do script PassKey in window 1)
	delay 3
	
	#Enable RDP
	(do script "sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -access -on -users " & AdminName & "-privs -all -restart -agent -menu" in window 1)
	delay 3
	(do script PassKey in window 1)
	delay 5
	(do script "exit" in window 1)
end tell