Rao
Rao

Reputation: 2962

WakeUp On Lan with Python

I am writing as application to Switch On Systems on Network using WakeOnLan feature.

I googled and able to get the code from here. My code looks like below. I have forwarded port 9 in my router also.

I have Enabled Wake On Lan Options for the network Card from Power Management. I followed instructions from here

I have installed Wake on Lan Monitor/Sniffer from here to check if i am able to receive magic Packet to wakeup. And the system is receiving magic packets. When i shutdown and run WOL python script from another system (Laptop) on same network, My system doesn't power on.

Can anyone suggest me solution.

My systems are Desktop with Win 8.1 which need to be wake On Lan. Laptop with Win 8 which need to run application and send magic packet to desktop.

My LAN IPs range from 172.16.0.1 and so on, so used 172.16.255.255 as broadcast address.

import sys, struct, socket

# Configuration variables
broadcast = ['172.16.255.255']
wol_port = 9

known_computers = {
    'mercury'    : '00:1C:55:35:12:BF',
    'venus'      : '00:1d:39:55:5c:df',
    'earth'      : '00:10:60:15:97:fb',
    'mars'       : '00:10:DC:34:B2:87',
}

def WakeOnLan(ethernet_address):

    # Construct 6 byte hardware address
    add_oct = ethernet_address.split(':')
    if len(add_oct) != 6:
        print "\n*** Illegal MAC address\n"
        print "MAC should be written as 00:11:22:33:44:55\n"
        return
    hwa = struct.pack('BBBBBB', int(add_oct[0],16),
        int(add_oct[1],16),
        int(add_oct[2],16),
        int(add_oct[3],16),
        int(add_oct[4],16),
        int(add_oct[5],16))

    # Build magic packet

    msg = '\xff' * 6 + hwa * 16

    # Send packet to broadcast address using UDP port 9

   soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    soc.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
    for i in broadcast:
        soc.sendto(msg,(i,wol_port))
    soc.close()

def wol(*macs):
    if len(macs) == 0:
        print "\n*** No computer given to power up\n"
        print "Use: 'wol computername' or 'wol 00:11:22:33:44:55'"
    else:
        for i in macs:
            if i[0] != '/':
                if ":" in i:
                    # Wake up using MAC address
                    WakeOnLan(i)
                else:
                    # Wake up known computers
                    if i in known_computers:
                        WakeOnLan(known_computers[i])
                    else:
                        print "\n*** Unknown computer " + i + "\n"
                        quit()

        if len(macs) == 2:
            print "\nDone! The computer should be up and running in a short while."
        else:
            print "\nDone! The computers should be up and running in a short while."
        print

wol('My System MAC address')

Upvotes: 0

Views: 25361

Answers (3)

vincent strobbe
vincent strobbe

Reputation: 23

I use wireshark to trace my WOL packet

I tested my code and it works.

for the wol in windows 8 + you have to uncheck the fast booting

(like i said before)

if you want to acces the remote pc for shutting down or check logged in status

or to logon

you need to add something into the regestry off the remote pc

see this picture to see where to add in regestry

(also) like i said before :)

if you don't do that, you cannot have remote acces (acces denied (5))

you can check it with cmd send a shutdown -s -m \ipadressOfTheRemotePC

i just created some massive software to wake up the other pc

check logged in or not and if its not it will do it for you if 1 of both are logged in.

The code i used for my wol packet is :

see this picture of my code

Upvotes: 0

Damien Daco
Damien Daco

Reputation: 1

I know this thread is old, but did you manage to make it work? First of all, I see you were using Win8.1 According to wikipedia:

"The ability to wake from a hybrid shutdown state (S4) or a fully powered off state (S5) is unsupported in Windows 8 and above,[20][21] and Windows Server 2012 and above.[22] This is because of a change in the OS behavior which causes network adapters to be explicitly not armed for WOL when shutdown to these states occurs. WOL from a sleep state (S3) or non-hybrid hibernation state (S4) is supported."

So I'd suggest to try with a different machine/OS, and make sure the WoL option is enabled in the BIOS.

Edit: I've just made a small python app and it works wether I use the '!' operator or not. Therefore, I'm not sure about the following lines:

Also, I see you were using the default byte order in your pack:

struct.pack('BBBBBB',

Aren't you supposed to use the '>' or '!' (big-endian / network) operator?

e.g.

struct.pack('!BBBBBB',

I'm afraid if you don't use the big-endian operator, it will default to your system way of encoding, and on Intel machines it will be the little-endian encoding scheme. So you're sending your bytes in the wrong order.

https://docs.python.org/3.0/library/struct.html https://en.wikipedia.org/wiki/Endianness

Upvotes: 0

You need to check whether the system reacts to getting the WOL-packets, not only that it's able to receive it (i.e. actually wakes up). If not you need to turn that on either trough special software or in the BIOS-settings of your computer.

I also have the experience that on most laptops you need to have the power plugged in, and also have the ethernet cable having been plugged in the computer before you turn it off to work properly.

To test if it works on your computer, download an existing software for sending WOL Magic Packets and make sure that works before you make an assumption that it's something wrong in your code.

In Linux (debian based example) all you need to do is:

sudo apt-get install etherwake

and then do

wakeonlan MAC-ADDRESS

Upvotes: 0

Related Questions