Showing posts with label network. Show all posts
Showing posts with label network. Show all posts

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;
    }
}

OEL netwerk configuration

Sometimes a system has been initiated to use DHCP and the use of a static ip-address is required, including correct dns resolution and gateway settings.

The following steps accomplish this:

Define the parameters of the NIC
  • Connect as root
  • Go to directoy /etc/sysconfig/network-scripts
  • Identify the file that is used for the NIC that you must define:
    for example ifcfg-eth0 is for the traditional first network card.

  • Verify/Change the settings depending on your needs
  • When changing from dhcp to static addressing, the file should become something like this:
    BOOTPROTO=static

    BROADCAST=192.168.1.255
    HWADDR=xx:xx:xx:xx:xx
    IPADDR=192.168.1.102
    NETMASK=255.255.255.0
    NETWORK=192.168.1.0
    ONBOOT=yes

Define the paramter for the network in itself
  • Connect as root
  • Go to /etc/sysconfig
  • Edit the file network
  • Set the hostname here and the gateway for the network.
  • The file should contain something like:
    HOSTNAME=myservername

    GATEWAY=192.168.1.1


Set the resolv.conf file with the nameservers
  • Connect as root
  • Go to directory /etc
  • Edit the file resolv.conf
  • Add/modify lines in the file with the specification of the nameserver.
  • An example
    search hellings.be

    nameserver vvv.xxx.yyy.zzzz

  • Add as many nameservers as you require
Restart the network
  • Connect as root
  • Restart the network
    service network restart