Wake Up On Lan - the mistery

If you would be thinking that a simple access to a shutdown server would bring it up, you will have found out that is not at all the case.

I had the same kind of problem in my perception of what "wake on lan" could be and started sniffing around. On the 'net I found this very interesting article which will explain the concept - which is not to difficult - and provides along the way the solution by means of a piece of Java code. (the code is at the end of this article)

In a way of safeguarding this referenced article's information - and not having to look it up again - here's a kind of resumé.

A wake up on lan is performed by sending a very specific packet to a machine's NIC (= network interface card) which should have the WOL capability. Because it is the NIC which is addressed and not the computer's operating system itselve the addressing is done at the level of the MAC address.

The packets content, which is often named the 'magic packet', is composed of 6 bytes filled with 1bits (= ff:ff:ff:ff:ff:ff) followed by 6 bytes that contain the MAC address of the NIC, followed by a repetition of the MAC address bytes 'til the packet is full.

Attention should be taken that the packet gets transmitted upto the machines NIC. It is very useful to consider the network topology on which you are broadcasting (UDP packets) the Magic Packet. If for example you want to wake up your server from some lokation on the internet, you should enable port forwarding rules on your Internet Router).

A commandline tool exists (windows) which is called mc-cmd and can be downloaded from this location.

Further information can be found, obviously, on Wikipedia.

For those speaking French WOL should not be translated into LAINE .... it will not work ;-)

I reformatted the java code of the article to suite my own layout ...


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

public class WakeUpOnLan 
{  
   // Define the port to be used to send the UDP packet
   public static final int PORT = 9;
   
   public static 
   void 
   main
   (  String[] args
   ) 
   {
      if (args.length != 2) 
      {  
         System.out.println("Usage: java WakeOnLan  ");
         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;
    }
}

Tug's Blog: Using HTTPS with Web Services

Tug's Blog: Using HTTPS with Web Services