SoFunction
Updated on 2024-11-20

Wake On Lan Remote Booting with Python

Wake-On-LAN, abbreviated WOL, is a power management feature; it allows a device to wake the operating system from standby or hibernation mode if network activity is present. Many motherboard manufacturers support the Wake-On-LAN standard proposed by IBM. The standard allows network administrators to remotely power on PCs for tasks such as file upgrades, resource tracking, and device inventories.

Let's first look at using Python to implement the WakeOn Lan remote boot function, as described below:

Creating a Magic Wake Package

Format the mac address, generate the magic wake up packet, then send the packet, first the computer needs to turn on wake on lan function

Create the main_wake_on_lan.py file

import socket
import binascii
import struct
import re
'''
formattingmacaddress,Generate magic wake-up packages,Then send。
macspecification: mac = A1B2C3D4E5F6
唤醒包specification: send_data = ('FF'*6 + str(mac)*16)
'''
MAC = "18:31:BF:B0:36:8F"

# MAC = "98-90-96-C1-FE-CB"

# Formatting MAC address 989096C1FECB in this form
def format_mac0(mac):
 if len(mac) == 12:
  pass
 elif len(mac) == 17:
  if (':') == 5 or ('-') == 5:
   sep = mac[2]
   mac = (sep, '')
  else:
   raise ValueError('Incorrect MAC format')
 else:
  raise ValueError('Incorrect MAC format')
 return mac
def format_mac(mac):
 mac_re = (r'''
      (^([0-9A-F]{1,2}[-]){5}([0-9A-F]{1,2})$
      |^([0-9A-F]{1,2}[:]){5}([0-9A-F]{1,2})$
      |^([0-9A-F]{1,2}[.]){5}([0-9A-F]{1,2})$
      )''',  | )
 # print((mac_re, mac))
 if (mac_re, mac):
  if (':') == 5 or ('-') == 5 or ('.'):
   sep = mac[2]
   mac_fm = (sep, '')
   return mac_fm
 else:
  raise ValueError('Incorrect MAC format')

# Method 1: Create a wake-up packet with the mac address in 989096C1FECB format.
def create_magic_packet0(mac):
 data = b'FF' * 6 + (mac * 16).encode()
 print(data)

 print(type(data))
 send_data = b''
 for i in range(0, len(data), 2):
  send_data = send_data + (b'B', int(data[i: i + 2], 16)) # int(data[i: i+2], 16) Convert hexadecimal to integer
 print(type(send_data))
 return send_data
# Method 2: Create a wakeup packet with the mac address in 989096C1FECB format, using the () method
def create_magic_packet(mac):
 data = 'FF' * 6 + str(mac) * 16
 # print(data)
 # print(type(data))
 send_data = (data)
 # print(type(send_data))
 return send_data
def send_magic_packet(send_data):
 # broadcast_address = '192.168.255.255'
 broadcast_address = '255.255.255.255'
 port = 9
 s = (socket.AF_INET, socket.SOCK_DGRAM)
 (socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
 (send_data, (broadcast_address, port))
if __name__ == '__main__':
 # print('mac address:', format_mac(MAC))
 mac = format_mac(MAC)
 send_data = create_magic_packet(mac)
 # print(send_data)
 send_magic_packet(send_data)

Calling a packet-sending function

Create main_boot_computer.py

import main_wake_on_lan
import sys


def boot_computer():
 try:
  pyname, parameter = 
  if parameter == '-h':
   print('Parameter usage: python3 main_boot_computer.py mac address\npython3 main_boot_computer.py 98:90:96:C1:FE:CB')
  else:
   print(' is moving to ', parameter, ' Send the magic wake-up packet!')
   # mac = '98:90:96:C1:FE:CB'

   mac = main_wake_on_lan.format_mac(parameter)
   send_data = main_wake_on_lan.create_magic_packet(mac)

   main_wake_on_lan.send_magic_packet(send_data)
   return 'Success to' + parameter + 'Send wakeup packet!'
 except ValueError:
  print('Incoming argument not received \n Get help: python3 main_boot_computer.py -h')

if __name__ == '__main__':
 boot_computer()

Usage

python3 main_boot_computer.py mac address

Addendum: python wake up intranet computer boot via wakeonlan

first needpip3 install wakeonlan

Then in the computer needs your network card to support the network to wake up the computer.

Then enable Wake on Support in the motherboard BIOS.

In the system NIC properties, check "Allow the computer to turn off this device to save power" and "Allow this device to wake up the computer".

Then the following is the python code, which is very simple.

from wakeonlan import send_magic_packet
send_magic_packet('4d.2d.6b.12.9b.1f')

summarize

The above is a small introduction to the use of Python to realize the Wake On Lan remote boot function, I hope to help you!