Page 1 of 1

Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 09 Nov 2014, 13:10
by Bruttosozialprodukt
This function allows you to awake any computer in your LAN that is set up for WoL, using just the MAC address of the target computer's network card.

Code: Select all

#include Socket.ahk ;http://pastebin.com/CtM9p4QG

WakeOnLAN("AABBCCDDEEFF") ;Example
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
}
I might extend the function to allow WoL passwords and waking computers over the Internet, if enough people would be interested in it...

Re: Wake on LAN (WoL) function - (Wake computers in your LAN

Posted: 09 Nov 2014, 13:55
by BGM
Pretty neat! How are you making use of it on your network (just curious)?

Re: Wake on LAN (WoL) function - (Wake computers in your LAN

Posted: 09 Nov 2014, 14:49
by Bruttosozialprodukt
I have a gaming rig in the basement and because of its uncomfortable position I prefer working on my notebook. But sometimes I just need more power or simply a second PC to run some benchmarks or other testings. So I set up TeamViewer and WoL on the gaming rig and now I can easily remote control my that PC any time I want without running it 24/7. :)
I also wrote this for a friend who wanted an open source script to turn on about 50 PCs at his working place every day in the morning.

Re: Wake on LAN (WoL) function - (Wake computers in your LAN

Posted: 09 Nov 2014, 15:49
by geek
I've written a function to do this before! Only problem is I don't seem to have any computers that support WoL :/

Re: Wake on LAN (WoL) function - (Wake computers in your LAN

Posted: 09 Nov 2014, 15:59
by BGM
GeekDude, you have to enable it in the BIOS (you probably knew that)

Re: Wake on LAN (WoL) function - (Wake computers in your LAN

Posted: 09 Nov 2014, 16:04
by geek
I know, no options for it anywhere :/

Re: Wake on LAN (WoL) function - (Wake computers in your LAN

Posted: 09 Nov 2014, 18:13
by Bruttosozialprodukt
I think that even my pentium 4 motherboard 10+ years ago supported WoL. Sometimes this option is just hidden. In my current BIOS for example I had to disable an option called ErP to get WoL... The best way to find out is to get a pdf manual and search for Wake on Lan in it.
I googled for a WoL AHK script before writing my own one and I really couldn't find anything, only one script in the old German AHK forums archive that seemed pretty dirty and outdated to me.

Re: Wake on LAN (WoL) function - (Wake computers in your LAN

Posted: 09 Nov 2014, 19:58
by geek
Sorry, none of the computer I have connected via ethernet have a WoL option. One of them technically does, but I can't get it to work for the life of me, even using actual WoL tools

Re: Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 28 Feb 2016, 05:49
by tmplinshi
thanks, it works.

Re: Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 28 May 2016, 23:44
by geek
I've been diving deep into Bentschi's socket library lately, actually rebuilding it from the ground up. While doing this, I've put together a few examples that demonstrate using its various features. One of these is a WakeOnLan script, similar to the one you've posted here. However, I've found that when broadcast mode is not explicitly enabled (and even when it is explicitly disabled), it still broadcasts just fine and the computer still wakes up. This effect seems to be reproducible in the original socket library, using your WakeOnLan script here by removing the line UdpOut.enableBroadcast() (or changing it to disable). I've read repeatedly around the net that this should not work, even though it appears to work just fine on my system (Win7 Pro x64 running AHKU32). As far as I can tell, the enableBroadcast method has never had much of an effect, as it would need to be called before WSAConnect in the connect method of Bentschi's library to actually influence the connection.

According to the MSDN article for WSAConnect, it should be failing when used for a broadcast address such as 255.255.255.255:
For connectionless sockets, name can indicate any valid address, including a broadcast address. However, to connect to a broadcast address, a socket must have setsockopt SO_BROADCAST enabled. Otherwise, WSAConnect will fail with the error code WSAEACCES.
However, as evidenced by my suggested modifications to this WoL code, this does not appear to be the case. I've tried to find anything describing this behavior, but unfortunately there doesn't seem to be much buzz on the web about code that shouldn't work working; almost all of it is code that should work not working. Can anyone help me figure out what is going on here?

Re: Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 31 May 2016, 09:40
by Bruttosozialprodukt
Technically a connect does not happen when using UDP, as it is by definition a connection-less protocol. So according to your quote SO_BROADCAST it is simply not meant to be used with UDP.

Re: Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 31 May 2016, 09:50
by geek
If that is the case, then I feel somewhat misled. I'm literally calling the function named connect to "connect" to that address, reading the documentation on the effects of "connect"ing. Also, the reason for SO_BROADCAST (as described here and here) is to prevent accidental broadcasting, presumably for security and congestion reasons (broadcast packets can apparently bog down a network). If SO_BROADCAST doesn't apply to the ubiquitous UDP, then what's the point?

Re: Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 02 Jun 2016, 04:09
by Bruttosozialprodukt
I really don't know a lot about winsock, but when using udp, the connect function does not actually do a connect. All it does is setting the ip that the send function is then going to use by default. Instead of using connect and send you could just use the sendto function. And yes, I agree, the connect function is very misleading.

I can only guess why SO_BROADCAST for tcp only, but maybe it's because establishing a tcp connection to every simple computer in a network has much more potential of making something bad happen then a simple udp broadcast which just sends a single tiny packet to all computers.

In the end I would say it doesn't really matter. Just don't use a broadcast address if you don't want to broadcast and if you really want your class to be able to block broadcasting, just do a If InStr(ip,"255") check before sending.

Re: Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 20 May 2019, 00:27
by niczoom
Hi,

I tried the WakOnLan utility above (to wake my TV) and found it didnt work for me. I used the correct MAC address to match the device but with no luck.

Im using it over my wireless network.

Ive installed a WOL app on my phone which does wake my tv so I know it works. Also nirsoft's 'WakeMeOnLan' works via command line.

Any ideas?

Re: Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 31 May 2019, 05:41
by Bruttosozialprodukt
Maybe your computer is broadcasting into the wrong network or your firewall/AV is blocking the broadcast. Or maybe it's a permission issue of some sort. Hard to say.

Re: Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 04 Jun 2019, 09:17
by Ruevil2
Wake on Lan definitely has permissions issues. On my network I had to create a proxy for the WoL so that it would run from server side for admin privileges. It needed admin rights and I did not want to give all my users who might potentially use it admin rights.

Client creates a txt with the target MAC address. Server side watches a folder for these files, reads and verifies the MAC and runs the wake command then deletes the txt file.

Re: Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 11 Jun 2019, 00:37
by niczoom
It's working now, I made the mistake of using : colons in the MAC address, worked great without them!

Also tried GeekDudes version above, works great as well.

Re: Wake on LAN (WoL) function - (Wake computers in your LAN)

Posted: 20 Nov 2022, 12:18
by mstrauss2021
Currently I use a stand alone program WakeMeOnLan.exe to wake my Win 7 pro 64-bit laptop and it works fine.

I was searching for an AHK solution and found your code. However, after I input my mac addr (no dashes, colons or spaces) I see 1 flash on the lan port of my laptop but it doesn't wake up.
I can only assume its because the laptop is on 1 network and my work computer on the same network and a 2nd network:

win 7 laptop:
On Home LAN - wired
192.168.56.1
mask: 255.255.255.0

Work computer: Win 10 (I wake up my laptop from this computer)
Home LAN - wired
192.168.1.117
mask: 255.255.255.0

Work LAN:
10.41.194.180
mask: 255.255.255.255 (maybe because part of VPN)

Any ideas how I can make this work?

Thanks

Mike