[Python] WakeUp On Lan with Python

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    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)
    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')

    Continue reading...

Compartilhe esta Página