您的位置:首页 > 其它

wakeonlan-远程开机

2017-06-15 10:00 405 查看
private  void sendIP_MAC(String ipStr,String macStr ){
  try {
     int port = 9;
           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);
           }
     String ip = getIpBroadcast(ipStr);
          //System.out.println(Arrays.toString(bytes));
           InetAddress address = InetAddress.getByName(ip);
           DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, port);
           DatagramSocket socket = new DatagramSocket(port);
           socket.setBroadcast(true);
           socket.send(packet);
           socket.close();
           //sendMagicPacketTo(address,macStr,port);
     
           System.out.println("远程主机"+ipStr+"已经开启");
          
       }
       catch (Exception e) {
           System.out.println("Failed to send Wake-on-LAN packet: + e");
           System.exit(1);
       }
  }

public static byte[] getMacBytes(String macStr){
byte[] bytes=new byte[6];
String[] hex = macStr.split("(\\:|\\-)");
if(hex.length != 6){
throw new IllegalArgumentException("非法的MAC地址");
}
try {
for(int i=0;i<6;i++){
bytes[i]=(byte)Integer.parseInt(hex[i],16);
}
} catch (Exception e) {
throw new IllegalArgumentException("非法的MAC地址");
}
return bytes;
}

public String getIpBroadcast(String ip) {
   String ipBroad = UtilsNetwork.binaryIpToInteger(UtilsNetwork.getBroadcastAddressBinary(ip,"255.255.255.0"));
   return ipBroad;
}

public final class UtilsNetwork  {

  private static final long serialVersionUID = 1L;

  /**

   * Constructor of the object. <br>

   * This constructor should remain private

   */

  private UtilsNetwork() {

    throw new IllegalAccessError("Can not be instantiate");

  }

  

  /**

   * Checks if IP as a good standard look.

   * 

   * @param anIp

   *          an ip as a string

   * @return true if IP is good, false if not

   */

  public static boolean checkIp(String anIp) {

    if ((anIp == null) || (anIp.trim().length() == 0)) {

     

      return false;

    }

    // Needs 4 parts

    String[] ipArray = anIp.split("\\.");

    if ((ipArray == null) || (ipArray.length != 4)) {

    

      return false;

    }

    // All part are [0, 255] integer

    for (int lcI = 0; lcI < ipArray.length; lcI++) {

      int val = -1;

      try {

        val = Integer.parseInt(ipArray[lcI]);

      } catch (NumberFormatException lcExc) {

      

        return false;

      }

      if ((val < 0) || (val > 255)) {

       

        return false;

      }

    }

    return true;

  }

  

  /**

   * Transform a binary IP into its integer representation.

   * 

   * @param aBinaryIp

   *          a binary IP

   * @return the integer representation of the IP

   */

  public static String binaryIpToInteger(String[] aBinaryIp) {

    StringBuilder resu = new StringBuilder();

    for (int lcI = 0; lcI < aBinaryIp.length; lcI++) {

      resu.append(Integer.parseInt(aBinaryIp[lcI], 2)).append('.');

    }

    resu.delete(resu.length() - 1, resu.length());

    

    return resu.toString();

  }

  

  /**

   * Compute binary broadcast mask for an IP.

   * 

   * @param anIp

   *          an IP

   * @param aMask

   *          a mask

   * @return the broadcast address for this IP/mask

   */

  public static String[] getBroadcastAddressBinary(String anIp, String aMask) {

    String[] ipMask = UtilsNetwork.ipToBinary(aMask);

    String[] subNetMaskBin = UtilsNetwork.getSubnetAddressBinary(anIp, aMask);

    String[] invertMask = new String[ipMask.length];

    String[] broadCastIp = new String[ipMask.length];

    for (int lcI = 0; lcI < ipMask.length; lcI++) {

      String ip0 = ipMask[lcI];

      StringBuilder resu = new StringBuilder();

      for (int lcI2 = 0; lcI2 < ip0.length(); lcI2++) {

        if (ip0.charAt(lcI2) == '1') {

          resu.append('0');

        } else {

          resu.append('1');

        }

      }

      invertMask[lcI] = resu.toString();

    }

    

    for (int lcI = 0; lcI < invertMask.length; lcI++) {

      String ip0 = subNetMaskBin[lcI];

      String ip1 = invertMask[lcI];

      StringBuilder resu = new StringBuilder();

      for (int lcI2 = 0; lcI2 < ip0.length(); lcI2++) {

        // Do OR

        if ((ip0.charAt(lcI2) == '1') || (ip1.charAt(lcI2) == '1')) {

          resu.append('1');

        } else {

          resu.append('0');

        }

      }

      broadCastIp[lcI] = resu.toString();

    }

   

    return broadCastIp;

  }

  

  /**

   * Compute binary subnet mask for an IP.

   * 

   * @param anIp

   *          an IP

   * @param aMask

   *          a mask

   * @return the subnet address for this IP/mask

   */

  public static String[] getSubnetAddressBinary(String anIp, String aMask) {

    String[] ipBin = UtilsNetwork.ipToBinary(anIp);

   
4000
String[] masqBin = UtilsNetwork.ipToBinary(aMask);

    String[] subMasq = new String[ipBin.length];

    for (int lcI = 0; lcI < masqBin.length; lcI++) {

      String ip0 = ipBin[lcI];

      String ip1 = masqBin[lcI];

      StringBuilder resu = new StringBuilder();

      for (int lcI2 = 0; lcI2 < ip0.length(); lcI2++) {

        // Do AND

        if ((ip0.charAt(lcI2) == '1') && (ip1.charAt(lcI2) == '1')) {

          resu.append('1');

        } else {

          resu.append('0');

        }

      }

      subMasq[lcI] = resu.toString();

    }

   

    return subMasq;

  }

  

  /**

   * Transform an IP into its binary representation.

   * 

   * @param anIp

   *          an IP

   * @return the binary representation of the IP

   */

  public static String[] ipToBinary(String anIp) {

    String[] ipArray = anIp.split("\\.");

    String[] ipBin = new String[ipArray.length];

    for (int lcJ = 0; lcJ < ipBin.length; lcJ++) {

      ipBin[lcJ] = Integer.toBinaryString(Integer.parseInt(ipArray[lcJ]));

      if (ipBin[lcJ].length() < 8) {

        int nb0 = 8 - ipBin[lcJ].length();

        StringBuilder buff = new StringBuilder();

        while (nb0 != 0) {

          buff.append('0');

          nb0--;

        }

        ipBin[lcJ] = buff.toString() + ipBin[lcJ];

      }

    }

    

    return ipBin;

  }

  

}

255.255.255.0这个根据所在的局域网自己设定!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  远程开机