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

Post your working scripts, libraries and tools for AHK v1.1 and older
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

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

09 Nov 2014, 13:10

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...
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

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

09 Nov 2014, 13:55

Pretty neat! How are you making use of it on your network (just curious)?
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

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

09 Nov 2014, 14:49

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.
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

09 Nov 2014, 15:49

I've written a function to do this before! Only problem is I don't seem to have any computers that support WoL :/
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

09 Nov 2014, 16:04

I know, no options for it anywhere :/
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

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

09 Nov 2014, 18:13

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.
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

09 Nov 2014, 19:58

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
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

28 May 2016, 23:44

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?
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

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

31 May 2016, 09:40

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.
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

31 May 2016, 09:50

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?
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

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

02 Jun 2016, 04:09

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.
User avatar
niczoom
Posts: 78
Joined: 09 Mar 2016, 22:17

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

20 May 2019, 00:27

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?
[AHK] 1.1.23.05 x32 Unicode
[WIN] 10 Pro x64 Version 5111 (Build 10586.218)
Bruttosozialprodukt
Posts: 463
Joined: 24 Jan 2014, 22:28

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

31 May 2019, 05:41

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.
Ruevil2
Posts: 173
Joined: 14 Jul 2014, 10:39

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

04 Jun 2019, 09:17

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.
User avatar
niczoom
Posts: 78
Joined: 09 Mar 2016, 22:17

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

11 Jun 2019, 00:37

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.
[AHK] 1.1.23.05 x32 Unicode
[WIN] 10 Pro x64 Version 5111 (Build 10586.218)
mstrauss2021
Posts: 30
Joined: 13 Feb 2021, 10:34

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

20 Nov 2022, 12:18

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

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 103 guests