Sunday, April 25, 2010

Wake on LAN command line script

Automate the Wake On LAN (WoL) by scripting a small but smart script.

First, make sure your hosts can wake on LAN, you may have to check the bios but also make sure to enable  wake function from windows, test that it works with the WOL GUI if you'd like and move to the next step when it works.

Second, download the wolcommand from here, I like to place it in C:\WINDOWS\system32 folder so it can be accessed from anywhere in the local system but you can place it in the same folder as the script which makes this command a dependency for the script.

Third step is the scripting portion, as described at the bottom of enable WoL in windows save all the MACs if there are multiple hosts you want to wake up, in my case the list is as follows:
pc00: 00-07-E9-xx-xx-xx
pc01: 00-07-aa-aa-aa-aa
pc02: 00-07-bb-bb-bb-bb

Place as many MACs as needed in this text file, we named it ip-mac.txt but you can name it anything else and have any other delimiter but make sure to edit the script below accordingly if you do have changes.

Now the fun part:
FOR /F "tokens=2 delims=:" %%G IN (ip-mac.txt) DO wolcmd %%G 192.168.1.255 255.255.255.255
pause

Named it wol.bat (or anything you like) and run or schedule with at command or  The first line is a FOR loop that takes the second field with colon delimiter (:) then execute "wolcmd " with each of the MAC address lines, it may help to learn more about FOR loops to understand this for your case.

Second line is a pause which will wait for user input any key, I just want to make sure the commands were successful otherwise the command window closes before you blink.

Anyway, this seems to work with most PCs, I am also considering placing a sleep few seconds between each MAC wake execution to give the packets time to leave before sending the next.

Let me know what you think!

No comments: