家庭局域网中电脑唤醒


import socket
import struct

class WOL_SEND():
    def __init__(self,wake_ip,wake_mac):
        self.wake_ip  = wake_ip
        self.wake_mac = wake_mac

    def create_wol_package(self):
        wake_mac_list = self.wake_mac.split("-")
        print(wake_mac_list)
        mac = struct.pack("BBBBBB", *[int(wake_mac_list[i],16) for i in range(6)]) # 将mac地址转二进制
        broadcast_mac = b"\xff"*6 # 生成6字节的FF

        wol = broadcast_mac+mac*16 #将6字节FF与目标mac的16次进行合并得到wol幻数据包
        return wol

    def send_wol_package(self):
        sockets = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        sockets.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1) #设置广播
        sockets.sendto(self.create_wol_package(), (self.wake_ip, 8))

def main():

    # wake_ip =input("请输入唤醒设备的局域网IP地址:")
    wake_ip = "192.168.1.112"
    # wake_mac = input("请输入即将发送wol远程唤醒数据包,请输入唤醒设备mac地址:")
    wake_mac = "D4-5D-64-08-16-81"
    # B4.2E.99.C3.DD.EC

    ws = WOL_SEND(wake_ip, wake_mac) #创建对象,传入MAC地址与IP地址

    ws.send_wol_package()
    print(ws.create_wol_package())
    print("[+]已发送远程指令")

if __name__ == "__main__":
    main()