公众号:架构师与哈苏
关注公众号进入it交流群! 公众号:架构师与哈苏 不定时都会推送一些实用的干货。。。

param类

package com.meeno.framework.wol.params;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Setter
@Getter
@NoArgsConstructor
public class WolParam {

    /**
     * ip
     */
    private String ip;

    /**
     * mac
     */
    private String mac;

    public WolParam(String ip, String mac) {
        this.ip = ip;
        this.mac = mac;
    }
}

WolUtils

package com.meeno.framework.wol;

import com.meeno.framework.wol.params.WolParam;
import org.springframework.util.CollectionUtils;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class WolUtils {


    /**
     * 批量唤醒
     * @param params
     */
    public static void batchWake(WolParam... params){
        if(params != null && params.length != 0){
            List<WolParam> list = Arrays.stream(params).collect(Collectors.toList());
            batchWake(list);
        }
    }

    /**
     * 批量唤醒
     * @param list
     */
    public static void batchWake(List<WolParam> list){
        if(!CollectionUtils.isEmpty(list)){
            for (WolParam wolParam : list) {
                wake(wolParam.getIp(), wolParam.getMac(), 4343);
            }
        }
    }

    /**
     * 唤醒
     * @param ip
     * @param macAddr
     * @param port
     */
    // 魔术包的组成
    // 魔术包的组成非常固定,由6对“FF”组成前缀,其余为重复16次的MAC地址组成。例如:试验机的MAC为:“28-D2-44-35-68-A7”,那么魔术包为:
    // “0xFFFFFFFFFFFF28D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A728D2443568A7”
    public static void wake(String ip, String macAddr, int port){
        try {
            //String ip = "192.168.0.72";
            //mac地址
            //String macAddr = "70-85-C2-8E-36-0B";
            //端口
            //int port = 4343;
            byte[] macByte = new byte[6];
            String[] ips = macAddr.split("\\:|\\-");
            for (int i = 0; i < 6; i++) {
                macByte[i] = (byte) Integer.parseInt(ips[i], 16);
            }
            // 用来存储网络唤醒数据包
            byte[] bys = new byte[6 + 16 * macByte.length];
            for (int i = 0; i < 6; i++) {
                bys[i] = (byte) 0xff;
            }
            for (int i = 6; i < bys.length; i += macByte.length) {
                System.arraycopy(macByte, 0, bys, i, macByte.length);
            }
            // 将字符形式的IP地址转换成标准的IP地址
            InetAddress address = InetAddress.getByName(ip);
            // 生成标准的数据报
            DatagramPacket pack = new DatagramPacket(bys, bys.length, address, port);
            // 创建标准套接字,用来发送数据报
            DatagramSocket socket = new DatagramSocket();
            // 发送魔法包
            socket.send(pack);
            socket.close();
            System.out.println("发送结束");
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

   public static void main(String[] args) {
        batchWake(new WolParam("192.168.0.72","70-85-C2-8E-36-0B"));
    }

}

posted on 2020-12-28 15:25  公众号/架构师与哈苏  阅读(343)  评论(0编辑  收藏  举报