网络唤醒的原理:通过有线网卡执行开机操作,需要主板和网卡全都支持网络唤醒功能,
注意,只有插网线的网卡才支持网络唤醒,wifi是无法远程开机的,
被唤醒的计算机需要在同一局域网中,通过UDP广播的方式发送魔法包;
蓝屏或死机属于非正常关机状态,不支持远程开机;
在设备管理器中找到 网卡设备,右键选择「属性」,然后在「电源管理」里面开启「允许此设备唤醒计算机」,然后在「高级」一栏里找到「关机网络唤醒」、「魔术封包唤醒」里设置成「开启」
网络唤醒(Wake On Lan)是一种计算机局域网唤醒技术,使局域网内处于关机或休眠状态的计算机,将状态转换成引导(Boot Loader)或运行状态.。用通俗的语言解释,就是你的电脑通电着但没开机,可以用其他电脑通过网络来唤醒(开机)你的电脑,以此实现【远程开关机】的操作。
目前的电脑硬件【主板】都自带有集成网卡,大多数集成网卡都能实现网络唤醒功能,除了【集成网卡】这个硬件条件,还需要到电脑的【BIOS】以及Windows系统中进行一些设置,即可实现远程开、关电脑的需求。
前提条件
主板支持(现在的主板基本都支持,所以基本不用担心),很旧的主板好像是2000年左右的,还是需要插网卡(PCI2.1标准)需要3针WOL线连接主板;
关机时候为主板通电(600mA以上电流),把电源切断是不行的,网卡带电才可以;
非正常关机可能会唤醒失败,所以里如果断电导致关机的话可能需要手工开启机器;
被唤醒的电脑必须连接网络,或者路由器,并且有一台手机或者电脑可以发送唤醒包;
WakeonlanService.java
package com.imddy.wakeonlan.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
@Service
public class WakeonlanService {
private static final Logger log = LoggerFactory.getLogger(WakeonlanService.class);
/**
* Sends a "magic packet" to the specified MAC address.
*
* @param macAddress The MAC address of the target machine in the format xx:xx:xx:xx:xx:xx.
* @param ipBroadcastAddress The broadcast IP address of the target machine's network.
* @throws IOException If there is an error sending the packet.
*/
public static void sendMagicPacket(String macAddress, String ipBroadcastAddress) throws IOException {
byte[] macBytes = parseMacAddress(macAddress);
byte[] data = createMagicPacket(macBytes);
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName(ipBroadcastAddress), 9);
try (DatagramSocket socket = new DatagramSocket()) {
socket.send(packet);
}
}
private static byte[] parseMacAddress(String macAddress) {
String[] octets = macAddress.split(":");
byte[] bytes = new byte[6];
for (int i = 0; i < octets.length; i++) {
bytes[i] = (byte) Integer.parseInt(octets[i], 16);
}
return bytes;
}
private static byte[] createMagicPacket(byte[] macBytes) {
byte[] data = new byte[102];
// Fill the first 6 bytes with 0xFF
for (int i = 0; i < 6; i++) {
data[i] = (byte) 0xff;
}
// Repeat the MAC address 16 times
for (int i = 6, j = 0; i < data.length; i++, j++) {
if (j == macBytes.length) j = 0;
data[i] = macBytes[j];
}
return data;
}
public static void main(String[] args) {
try {
sendMagicPacket("00:11:22:33:44:55", "192.168.1.255");
System.out.println("Magic packet sent.");
log.info("Magic packet sent.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
Application.java
package com.imddy.wakeonlan;
import com.imddy.wakeonlan.cache.AppCacheMap;
import com.imddy.wakeonlan.service.WakeonlanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Iterator;
import java.util.Set;
@SpringBootApplication
public class Application implements ApplicationRunner {
private static final Logger log = LoggerFactory.getLogger(Application.class);
@Autowired
private WakeonlanService wakeonlanService;
public static void main(String[] args) {
log.info("Application start");
SpringApplication.run(Application.class, args);
log.info("Application end");
}
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("wakeonlan启动run完成...");
/** 默认参数 */
String ip = "";
String mac = "";
/** 有帮助与否显示帮助 */
Set<String> argsSet = args.getOptionNames();
if (argsSet.isEmpty() || argsSet.contains("help")) {
printHelp();
System.exit(1);
}
/** 参数赋值 */
if (!argsSet.isEmpty()) {
for (Iterator iterator = argsSet.iterator(); iterator.hasNext(); ) {
String arg = (String) iterator.next();
if (arg.equalsIgnoreCase("ip")) {
if (args.getOptionValues("ip").size() > 0) {
ip = args.getOptionValues("ip").get(0);
} else {
System.out.println("ip参数异常!");
printHelp();
System.exit(1);
}
}
if (arg.equalsIgnoreCase("mac")) {
if (args.getOptionValues("mac").size() > 0) {
mac = args.getOptionValues("mac").get(0);
} else {
System.out.println("mac参数异常!");
printHelp();
System.exit(1);
}
}
}
}
log.info("ip:" + ip);
log.info("mac:" + mac);
mac = mac.replaceAll("-", ":").toString();
log.info("ip:" + ip);
log.info("mac:" + mac);
AppCacheMap.put("ip", ip);
AppCacheMap.put("mac", mac);
wakeonlanService.sendMagicPacket(mac, ip);
}
public void printHelp() {
System.out.println("--help 显示帮助");
System.out.println("--ip=192.168.106.22 录入IP");
System.out.println("--mac=74:56:3C:88:FA:CE 录入MAC");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.