Bladtman242 8 Junior Poster

Hi Guys, I am new to java and I need some help:)

First of all: I admit, I did not write this, I re-wrote a bit of it for my needs, and therefore may not be able to fully understand parts of it.

The thing is, the script accepts only ip adresses as the first argument, And I need it to accept a domain name, as I have a dynamic ip:)

Here it is.
Any help, whether a link for further reading or actually doing the dirty work for me, is much appreciated.

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

public class wakeonlan {
    
    public static final int PORT = 9;    
    
    public static void main(String[] args) {
         
        String ipStr = "THE IP GOES HERE";
        String macStr = "THE MAC GOES HERE";
        
        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;
    }
    
   
}