简体   繁体   English

Ping机器会唤醒它吗?

[英]Does Pinging a machine wakes it up?

I have a code to check whether a machine is alive or not. 我有一个代码来检查机器是否还活着。

InetAddress.getByName(IPADDRESS).isReachable(TIMEOUT);

Will this request wakes up the machine?, Or just gives a status that it is not shutdown or not accessible. 该请求将唤醒计算机吗?,或者只是给出一个状态,表明它没有关闭或无法访问。

No, simple ping command wouldn't wake up your machine regardless of its configuration. 不,无论其配置如何,简单的ping命令都不会唤醒您的计算机。 You would want to use a "wake-on-lan" command, which has to contain the machine's MAC address above all, IP address itself is insufficient. 您可能想使用“ wake-on-lan”命令,该命令首先必须包含机器的MAC地址,但IP地址本身不足。 Also, the machine has to support the feature (more precisely, the network adapter is uses has to), and it has to be enabled, so make sure that is done. 另外,机器必须支持该功能(更确切地说,必须使用网络适配器),并且必须启用它,因此请确保已完成。

I'm including the example I've referred to in the past on how to implement this in java: 我包括了我过去提到的有关如何在Java中实现此示例的示例:

import java.io.*;
import java.net.*;

public class WakeOnLan {

public static final int PORT = 9;    

public static void main(String[] args) {

    if (args.length != 2) {
        System.out.println("Usage: java WakeOnLan <broadcast-ip> <mac-address>");
        System.out.println("Example: java WakeOnLan 192.168.0.255 00:0D:61:08:22:4A");
        System.out.println("Example: java WakeOnLan 192.168.0.255 00-0D-61-08-22-4A");
        System.exit(1);
    }

    String ipStr = args[0];
    String macStr = args[1];

    try {
        byte[] macBytes = getMacBytes(macStr);
        byte[] bytes = new byte[6 + 16 * macBytes.length];
        for (int i = 0; i < 6; i++) {
            bytes[i] = (byte) 0xff;
        }
        for (int i = 6; i < bytes.length; i += macBytes.length) {
            System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
        }

        InetAddress address = InetAddress.getByName(ipStr);
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
        DatagramSocket socket = new DatagramSocket();
        socket.send(packet);
        socket.close();

        System.out.println("Wake-on-LAN packet sent.");
    }
    catch (Exception e) {
        System.out.println("Failed to send Wake-on-LAN packet: + e");
        System.exit(1);
    }

}

private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
    byte[] bytes = new byte[6];
    String[] hex = macStr.split("(\\:|\\-)");
    if (hex.length != 6) {
        throw new IllegalArgumentException("Invalid MAC address.");
    }
    try {
        for (int i = 0; i < 6; i++) {
            bytes[i] = (byte) Integer.parseInt(hex[i], 16);
        }
    }
    catch (NumberFormatException e) {
        throw new IllegalArgumentException("Invalid hex digit in MAC address.");
    }
    return bytes;
}


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM