Using Python to realize wake on LAN remote boot function


Wake on LAN, or WOL for short, is a power management function; if there is network activity, it allows the device to wake up the operating system from standby or sleep mode. Many motherboard manufacturers support the wake-up standard proposed by IBM. The standard allows network administrators to turn on the PC remotely for file upgrading, resource tracking and device inventory.

First, let’s see how to use Python to realize the wakeon LAN remote power on function. The details are as follows:

Create a magic wake-up package

Format the MAC address, generate the magic wake-up package, and then send the package. First, the computer needs to turn on the wake on LAN function

Create the main? Wake? On? file

import socket
import binascii
import struct
import re
Format the MAC address, generate a magic wake-up packet, and send it.
Mac format: MAC = a1b2c3d4e5f6
Wake up packet format: send_data = binascii. Unhexlify ('ff '* 6 + str (MAC) * 16)
MAC = "18:31:BF:B0:36:8F"

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

#Format MAC address 989096c1fecb in this form
def format_mac0(mac):
 if len(mac) == 12:
 elif len(mac) == 17:
  if mac.count(':') == 5 or mac.count('-') == 5:
   sep = mac[2]
   mac = mac.replace(sep, '')
   raise ValueError('Incorrect MAC format')
  raise ValueError('Incorrect MAC format')
 return mac
def format_mac(mac):
 mac_re = re.compile(r'''
      )''', re.VERBOSE | re.IGNORECASE)
 # print(re.match(mac_re, mac))
 if re.match(mac_re, mac):
  if mac.count(':') == 5 or mac.count('-') == 5 or mac.count('.'):
   sep = mac[2]
   mac_fm = mac.replace(sep, '')
   return mac_fm
  raise ValueError('Incorrect MAC format')

#Method 1: create wake-up packet with MAC address of 989096c1fecb format
def create_magic_packet0(mac):
 data = b'FF' * 6 + (mac * 16).encode()

 send_data = b''
 for i in range(0, len(data), 2):
  Send ﹐ data = send ﹐ data + struct.pack (B'b ', int (data [I: I + 2], 16)) ﹐ int (data [I: I + 2], 16) converts hex to integer
 return send_data
#Method 2: create a wake-up packet with MAC address of 989096c1fecb format, and use the binascii. Unhexlify() method
def create_magic_packet(mac):
 data = 'FF' * 6 + str(mac) * 16
 # print(data)
 # print(type(data))
 send_data = binascii.unhexlify(data)
 # print(type(send_data))
 return send_data
def send_magic_packet(send_data):
 # broadcast_address = ''
 broadcast_address = ''
 port = 9
 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
 s.sendto(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)

Call the contract function

Create main boot

import main_wake_on_lan
import sys

def boot_computer():
  pyname, parameter = sys.argv
  if parameter == '-h':
   Print ('parameter usage method: python3 main boot MAC address \ npython3 main boot 98:90:96: C1: Fe: CB ')
   Print ('sending magic wake-up package to ', parameter,' now! )
   # 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)

   Return 'successfully sent wake-up packet to' + parameter + '! '
 except ValueError:
  Print ('the passed in parameter was not received \ n for help: python3 main boot? Computer. Py - H ')

if __name__ == '__main__':

Usage method

Python3 main boot MAC address

Add: Python wakes up the intranet computer through wakeonlan

First needpip3 install wakeonlan

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

Then turn on BIOS on the motherboard to support wake-up.

Select “allow the computer to turn off this device to save power” and “allow this device to wake up the computer” in the system network card properties

Then here is the Python code, very simple.

from wakeonlan import send_magic_packet


The above is a brief introduction of using Python to realize wake on LAN remote boot function. I hope it can help you!

Recommended Today

Construction of Vue development environment and project creation under mac

start First of all, let’s take a look at the overall development environment of Vue. Homebrew: package manager under MAC system, similar to apt get under Linux, windows control panel – install and remove program Node.jsJavaScript runtime, which is similar to simultaneous interpretation in international conferences, can’t run various programming languages directly between different systems […]