Using Python to realize wake on LAN remote boot function

Time:2020-3-20

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? Lan.py 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:
  pass
 elif len(mac) == 17:
  if mac.count(':') == 5 or mac.count('-') == 5:
   sep = mac[2]
   mac = mac.replace(sep, '')
  else:
   raise ValueError('Incorrect MAC format')
 else:
  raise ValueError('Incorrect MAC format')
 return mac
def format_mac(mac):
 mac_re = re.compile(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})$
      )''', 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
 else:
  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()
 print(data)

 print(type(data))
 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
 print(type(send_data))
 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 = '192.168.255.255'
 broadcast_address = '255.255.255.255'
 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)
 send_magic_packet(send_data)

Call the contract function

Create main boot computer.py

import main_wake_on_lan
import sys


def boot_computer():
 try:
  pyname, parameter = sys.argv
  if parameter == '-h':
   Print ('parameter usage method: python3 main boot computer.py MAC address \ npython3 main boot computer.py 98:90:96: C1: Fe: CB ')
  else:
   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)

   main_wake_on_lan.send_magic_packet(send_data)
   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__':
 boot_computer()

Usage method

Python3 main boot computer.py 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
send_magic_packet('4d.2d.6b.12.9b.1f')

summary

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