Search code examples
autohotkeywake-on-lan

Wake on LAN (WoL) - start computers over the network


You sometimes want to start a computer without pressing it's power button. And it can be done.. over the network using a technology called Wake on LAN (WoL).

But how can it be done in AutoHotkey?


Solution

  • To wake a computer over your LAN you need to create and send a so called magic packet. All you need to know is the mac address of your target computer. Also, make sure it is properly set up for WoL.

    Waking the computer can be done pretty easily using the socket library in AHK:

    #include Socket.ahk ;http://pastebin.com/CtM9p4QG
    
    WakeOnLAN("AABBCCDDEEFF") ;Example  - use the mac address of your target here
    ExitApp
    
    WakeOnLAN(mac) {
        magicPacket_HexString := GenerateMagicPacketHex(mac)
        size := CreateBinary(magicPacket_HexString, magicPacket)
        UdpOut := new SocketUDP()
        UdpOut.connect("addr_broadcast", 9)
        UdpOut.enableBroadcast()
        UdpOut.send(&magicPacket, size)
    }
    GenerateMagicPacketHex(mac) {
        magicPacket_HexString := "FFFFFFFFFFFF"
        Loop, 16
            magicPacket_HexString .= mac
        Return magicPacket_HexString
    }
    CreateBinary(hexString, ByRef var) { ;Credits to RHCP!
        sizeBytes := StrLen(hexString)//2
        VarSetCapacity(var, sizeBytes)
        Loop, % sizeBytes
            NumPut("0x" SubStr(hexString, A_Index * 2 - 1, 2), var, A_Index - 1, "UChar")
        Return sizeBytes
    }