A working script to wake up and put to sleep a remote computer on LAN

I was looking for a single script that could wake up a remote computer over LAN and also put it to sleep.

I’ve come up with a single script that pings a remote Mac to check if it’s sleeping or awake and let’s you wake it or put it to sleep accordingly.

The code is not very polished and everything is hard-coded - but it does work and I couldn’t find a similar script online - so it might come in handy for someone looking for something like this.

Just replace user name, Bonjour address and MAC address.


set nonet to "false"
try
	set thePing to do shell script "ping -o -c 1 BONJOUR_ADDRESS.LOCAL" -- ping remote computer
on error
	set nonet to "true" -- if the ping fails, set nonet to true
end try

if nonet ≠ "true" then
	
	set question to display dialog "SERVER is wide awake." buttons {"Cancel", "Sleep"} default button 1 with title "SERVER status"
	set answer to button returned of question
	if the answer is "Sleep" then
		set remote_machine to "eppc://USERNAME@BONJOUR_ADDRESS.LOCAL"
		using terms from application "Finder"
			with timeout of 120 seconds -- long timeout, as it might ask for password
				try
					tell application "Finder" of machine remote_machine to get processes -- sleep is now managed by system events - this launches system events on remote computer
					tell application "System Events" of machine remote_machine to sleep
				on error the_err
					beep
					activate
					display dialog the_err buttons {"OK"} default button 1 with icon 0 giving up after 5
				end try
			end timeout
		end using terms from
	end if
	
else
	
	set question to display dialog "Shh! SERVER is fast asleep." buttons {"Cancel", "Wake"} default button 1 with title "SERVER status"
	set answer to button returned of question
	if the answer is "Wake" then
		
		(* 
Based on Wake on WAN - by Mark Muir (2008-04-01) 

Sends a magic UDP packet to a remote machine to wake it up from 
sleep. 

http://forums.dealmac.com/read.php?4,2751523
*)
		
		-- replace mac address on next line
		set command to "/usr/bin/php -r " & quoted form of ("$mac = " & quoted form of "00:00:00:00:00:00" & "; $ip = " & quoted form of "255.255.255.255" & "; " & " 
$mac_bytes = explode(\":\", $mac); 
$mac_addr = \"\"; 
for ($i=0; $i<6; $i++) 
$mac_addr .= chr(hexdec($mac_bytes[$i])); 
$packet = \"\"; 
for ($i=0; $i<6; $i++)	/*6x 0xFF*/ 
$packet .= chr(255); 
for ($i=0; $i<16; $i++)	/*16x MAC address*/ 
$packet .= $mac_addr; 

$port = 9; 
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE); 
socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port); 
socket_close($sock); 
")
		do shell script command
		
	end if
	
end if

-Dinis

Since this is really a completed script and not a question, it needs to be posted in the code exchange section instead of the “questions” section.

I didn’t get to try this but it looks like it will work. As such I rewrote it to make it a little easier to adjust the variables that need adjusting… by putting them at the top of the script. Also made a few other minor adjustments.

-- use this to wake or sleep a computer on your network.
-- when run you will be told if the computer is awake or asleep, and asked if you want to wake/sleep it
-- it may work through your router from an external network if you router allows the broadcast ping through
-- if you use the script externally you might have to replace the bonjour name with the actual ip address of the computer

-- based on Wake on WAN - by Mark Muir (2008-04-01) 
-- sends a magic UDP packet to a remote machine to wake it up from sleep. 
-- http://forums.dealmac.com/read.php?4,2751523

-- variables to adjust
set userName to "username" -- user name on computer you want to connect to
set bonjourName to "sharingName.local" -- sharing name of the computer you want to connect to... remember to add ".local" to it
set macAddress to "00:00:00:00:00:00" -- mac address of the network card on the computer you want to connect to
set broadcastPing to "255.255.255.255" -- may need to use 10.0.1.255 instead if using a "10.0" based subnet mask
set timeoutSeconds to 120 -- long timeout, as it might ask for password when trying to sleep the computer

-- is the computer currently awake?
set computerIsAwake to true
try
	set thePing to do shell script "ping -o -c 1 " & quoted form of bonjourName -- ping remote computer
on error
	set computerIsAwake to false
end try

if computerIsAwake then -- sleep the computer
	set question to display dialog "Shh! SERVER is fast asleep." buttons {"Cancel", "Wake"} default button 1 with title "SERVER status"
	set answer to button returned of question
	if the answer is not "Wake" then return
	
	set command to "/usr/bin/php -r " & quoted form of ("$mac = " & quoted form of macAddress & "; $ip = " & quoted form of broadcastPing & "; " & " \n$mac_bytes = explode(\":\", $mac); \n$mac_addr = \"\"; \nfor ($i=0; $i<6; $i++) \n$mac_addr .= chr(hexdec($mac_bytes[$i])); \n$packet = \"\"; \nfor ($i=0; $i<6; $i++)\t/*6x 0xFF*/ \n$packet .= chr(255); \nfor ($i=0; $i<16; $i++)\t/*16x MAC address*/ \n$packet .= $mac_addr; \n\n$port = 9; \n$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); \nsocket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE); \nsocket_sendto($sock, $packet, strlen($packet), 0, $ip, $port); \nsocket_close($sock); \n")
	do shell script command
else -- wake the computer
	set question to display dialog "SERVER is wide awake." buttons {"Cancel", "Sleep"} default button 1 with title "SERVER status"
	set answer to button returned of question
	if answer is not "Sleep" then return
	
	set remote_machine to "eppc://" & userName & "@" & bonjourName
	try
		with timeout of timeoutSeconds seconds
			using terms from application "Finder"
				tell application "Finder" of machine remote_machine to get every process -- this launches system events on remote computer
			end using terms from
			using terms from application "System Events"
				tell application "System Events" of machine remote_machine to sleep -- sleep is now managed by system events
			end using terms from
		end timeout
	on error the_err
		beep
		activate
		display dialog the_err buttons {"OK"} default button 1 with icon 0 giving up after 5
	end try
end if

Thank, great adjustments!

(oops, missed the Code Exchange forum - guess an admin will have to move the topic, couldn’t find a way to move it)