This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Wake on LAN
Please improve where possible.

Function : Wakes up a PC from the Logic Machine
Goal : Save power by starting PC's only when needed

Requirements :
1. PC must support Wake on LAN.  You can verify this in the Bios or by sending a WoL-Packet (Magic Packet) from a Windows or Linux tool.
2. WoL must be enabled

Actions :
1. Send Magic Packet
2. Wait a number of seconds
3. Check if portnumber on PC is available
4. If not, log error condition

To do :
1. return actionable error code

Installation :

1. Add the the code underneath to the function library
2. Create a script with the following line :
Wol('aa:bb...:ff', '10.1.x.y', '60', '80')

Aa:bb...:ff  MAC-address
10.1.x.y IP address (to verify the fail/success state)
'60' number of seconds before checking if the required port number is running
'80' port number to verify


function wol(mac, ip, timeout, port)

  -- helper function definitions
  function wol_send(mac_dest)

     local broadcast_ip = ""
     local port = 9
     local udp = assert(socket.udp())
     udp:setoption('broadcast', true)

     local mac_array = string.split(mac_dest, ":")
     local mac = ""
     for i,v in ipairs(mac_array) do mac = mac..string.char(tonumber(v, 16));  end
     local mac1 = ""
     for i = 1,16 do
        mac1 = mac1..mac
     local mac2 = string.rep(string.char(0xff),6)..string.rep(mac, 16)
     assert(udp:sendto(mac2, broadcast_ip, port))

  -- tries to open TCP connecton to specified IP:port
  function tcp_knock(ip, port)
     local tcp = assert(socket.tcp())
     local success = tcp:connect(ip, port)
     Log("tcp knock to " .. ip .. " is " .. (success and "success" or "fail"))
     return success

  assert(type(mac) == 'string', "mac address (string) should be specified");
  assert(type(ip)  == 'string', "ip address (string) should be specified");

  timeout = tonumber(timeout) or 0
  if (timeout > 0) then
     print("Sleeping " .. timeout .. " seconds")
     local knock_success = tcp_knock(ip, port)
     if ( not knock_success ) then
        local subject = "
        local alertmessage = "Wake Up fail: " .. ip .. "/" .. mac

Forum Jump: