Python wol/wakeonlan/Network Wake-up packet sending tool

Source: Internet
Author: User

According to the WOL protocol, when the computer is turned off and the NIC is powered, the WOL Magic Packet received from the network will be automatically powered on. This approach can be easily used in environments where remote administration is required. This program realizes the network wake-up Magic packet sending function, can realize the remote boot.

Assume that you need to wake up the PC Nic MAC address is: 01:02:03:04:05:06 the WOL magic Packet structure is as follows:

FF FF FF FF FF | 01 02 03 04 05 06 ... Repeat 16 times ... in Geneva | 00 00 00 00 00 00

6 bytes 0xFF and trailing 6 byte 0x00 without change copy, total packet length: 108 bytes

The PC can be awakened by sending the above packet to the broadcast address of the local subnet (in code: 192.168.1.255) on UDP port 9

Note: This code requires the Python interpreter to run, Windows/linux/mac OS generic

How to use:

1. Save the following code to wol.py

#!/usr/bin/env python#coding=utf-8import socket, sysimport structdef to_hex_int (s):     return int (S.upper (),  16)  dest =  (' 192.168.1.255 ',  9)  if len (SYS.ARGV)  < 2: print ("Usage: %s <mac address to  wakeup> " % sys.argv[0])  sys.exit ()  mac = sys.argv[1] spliter  =  "" If mac.count (":")  == 5: spliter =  ":" If mac.count ("-")  ==  5: spliter =  "-"  if spliter ==  "":  print ("mac address  Should be like xx:xx:xx:xx:xx:xx / xx-xx-xx-xx-xx-xx ")  sys.exit ()  parts  = mac.split (Spliter) a1 = to_hex_int (Parts[0]) a2 = to_hex_int (parts[1]) a3 =  to_hex_int (parts[2]) a4 = to_hex_int (parts[3]) a5 = to_hex_int (parts[4]) a6 =  to_hex_int (parts[5]) ADDR&NBSP;=&NBSP;[A1,&NBSP;A2,&NBSP;A3,&NBSP;A4,&NBSP;A5,&NBSP;A6]&NBSP;PACKET&NBSP;=&NBSP;CHR (255) &NBSP;+&NBSP;CHR ( 255) &NBSP;+&NBSP;CHR (255) &NBSP;+&NBSP;CHR (255) &NBSP;+&NBSP;CHR (255) &NBSP;+&NBSP;CHR (255)  for n  In range (0,16): &NBSP;FOR&NBSP;A&NBSP;IN&NBSP;ADDR:&NBSP;&NBSP;PACKET&NBSP;=&NBSP;PACKET&NBSP;+&NBSP;CHR (A ) &NBSP;PACKET&NBSP;=&NBSP;PACKET&NBSP;+&NBSP;CHR (0)  + chr (0)  + chr (0)  + chr (0) &NBSP;+&NBSP;CHR (0)  + chr (0)  s = socket.socket (socket.af_inet, socket. SOCK_DGRAM) s.setsockopt (socket. Sol_socket,socket. so_broadcast,1) s.sendto (packet,dest)  print ("wol packet %d bytes sent !"  % len (packet))

2. Use the method for command-line execution (Linux/mac can not need to write commands to start with Python):

Python./wol.py 01:02:03:04:05:06

This article from "IPlay IT" blog, reproduced please contact the author!

Python wol/wakeonlan/Network Wake-up packet sending tool

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.