Using .NET to implement WOL wake-up remote booting

Table of Contents

1. Background

2. About WOL

2.1 Working principle of WOL

2.2 Turn on the network card wake-up function

3. Quick verification

3.1 Wake on Lan application

3.2 Ubuntu’s etherwake command

4. Code implementation

4.1 Create a .NET console application

4.2 Writing code

4.3 Running the application

5. Finally


1. Background

Home automation is an important part of modern smart homes. By connecting various devices, we can achieve many convenient functions, such as remote power on. Traditionally, we can use a remote boot card to achieve this function, but this method has certain limitations, such as occupying the PCIe or USB interface of the motherboard. Of course, the boot card also has its own advantages. In this article, we will introduce how to use software solutions to achieve remote booting, and give the .NET implementation method.

I will continue to update relevant articles in the future, and will continue to introduce the deployment of applications in Qunhui NAS, remote calls, .NET IOT hardware access, home linkage, etc. based on the background of this theme.

2. About WOL

Before we learn more about how to use .NET to implement WOL wake-up remote boot function, let us first understand the related concepts and principles of WOL (Wake-on-LAN, Wake-on-LAN).

WOL is a network protocol that allows users to send a specific data packet through the LAN to wake up a computer that is in a hibernation or shutdown state. This function has high practical value in scenarios such as remote management, energy saving, and home automation.

2.1 How WOL works

The working principle of WOL is to monitor a specific data packet on the computer’s network card, called a Magic Packet. When the network card receives this packet, the computer wakes up. A magic packet is a UDP broadcast packet containing a specific format, which mainly includes the following parts:

1. The first is 6 bytes of 0xFF, which is a binary value of all ones. 2. This is followed by the MAC address of the target computer repeated 16 times.

2.2 Enable the network card wake-up function

To implement the WOL function, we need to ensure that the target computer’s hardware and operating system support WOL, and enable relevant settings in the BIOS and operating system. Additionally, the device sending the magic packet and the target computer need to be on the same LAN, as magic packets are typically not forwarded to other networks by routers.

Network card configuration

Most modern network cards support the WOL function and are enabled by default. However, in order to ensure that the WOL function can be used normally, we can check and configure the relevant settings of the network card. You need to find “Wake-up Mode” or an item with a similar name in the “Properties” list of the network card in use, and set its value to “Magic Packet” or “Magic Packet & Pattern Match”. I won’t go into details here. There are many related startup tutorials on the Internet, just search for them.

BIOS configuration

In order to ensure that the WOL function can work properly, we also need to enable relevant settings in the BIOS. This is not turned on by default. The BIOS settings of different motherboard manufacturers may be slightly different, but the basic steps are as follows:

1. Press a specific key (usually F2, F10, DEL or ESC) when booting to enter the BIOS setup interface. 2. Find “Power Management” or an option with a similar name in the BIOS setup interface. 3. Find related settings such as “Wake on LAN” or “Wake on Network” in the power management options and enable them. 4. Save settings and exit BIOS.

It should be noted that the BIOS setup interface and options of different motherboard manufacturers may be slightly different. Please configure according to the actual situation. If necessary, you can consult the motherboard manual for detailed information.

Picture

BIOS

3. Quick Verification

In order to confirm whether the WOL function has been correctly turned on, you can shut down the target computer and use a mobile phone or other device to test and verify.

3.1 LAN Wake on Lan Application

After performing the above operation to turn on the WOL function, we can quickly verify it through the Wake on Lan application.

Wake on Lan is a great native tool app that is open source and ad-free, and also supports Android watches. Open source address: https://github.com/Florianisme/WakeOnLan?wt.mc_id=DT-MVP-5005195

Picture

Wake on Lan

3.2 Ubuntu’s etherwake command

Install the etherwake package:

sudo apt install wakeonlan

To send a wake-up packet to the target host, you need to know the MAC address of the target host:

wakeonlan <mac address>

4. Code Implementation

Next, we will use the .NET framework to implement the WOL wake-up remote boot function. The following code will show how to create a simple .NET console application to send WOL magic packets.

4.1 Create .NET console application

First, we need to create a new .NET console application. At the command line, enter the following command:

dotnet new console -n WOLApp
cd WOLApp 

This will create a .NET console application called WOLApp and change the current working directory into the project directory.

4.2 Writing Code

Next, we need to write the actual WOL code. In the Program.cs file, replace the default code and paste the following code: ?

using System;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;

namespace WOLApp
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage: WOLApp <MAC Address>");
                return;
            }

            string macAddress = args[0];

            if (!IsValidMacAddress(macAddress))
            {
                Console.WriteLine("Invalid MAC address format");
                return;
            }

            try
            {
                byte[] magicPacket = CreateMagicPacket(macAddress);
                SendMagicPacket(magicPacket);
                Console.WriteLine("Magic packet sent successfully");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error sending magic packet: " + ex.Message);
            }
        }

        static bool IsValidMacAddress(string macAddress)
        {
            Regex regex = new Regex("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$");
            return regex.IsMatch(macAddress);
        }

        static byte[] CreateMagicPacket(string macAddress)
        {
            byte[] macBytes = ParseMacAddress(macAddress);
            byte[] magicPacket = new byte[6 + (6 * 16)];

            for (int i = 0; i < 6; i + + )
            {
                magicPacket[i] = 0xFF;
            }

            for (int i = 6; i < magicPacket.Length; i + = 6)
            {
                Array.Copy(macBytes, 0, magicPacket, i, 6);
            }

            return magicPacket;
        }

        static byte[] ParseMacAddress(string macAddress)
        {
            string[] hexValues = macAddress.Split(new[] { ':', '-' });
            byte[] macBytes = new byte[6];

            for (int i = 0; i < hexValues.Length; i + + )
            {
                macBytes[i] = Convert.ToByte(hexValues[i], 16);
            }

            return macBytes;
        }

        static void SendMagicPacket(byte[] magicPacket)
        {
            using (UdpClient udpClient = new UdpClient())
            {
                udpClient.Connect(IPAddress.Broadcast, 9);
                udpClient.Send(magicPacket, magicPacket.Length);
            }
        }
    }
} 

This code first checks if the entered MAC address is valid, then creates a magic packet and sends it via UDP broadcast into the LAN.

Please note that the UDP port used here is 9, which is the standard port for WOL. You can also change to other ports if needed.

4.3 Run Application

Now we can run the application and test WOL functionality. At the command line, enter the following commands to compile and run the application:

dotnet run <MAC Address> 

Where is the MAC address of the target computer. For example:

dotnet run 00-11-22-33-44-55

If everything is fine, you should see the “Magic packet sent successfully” prompt. At this point, if the target computer’s hardware, BIOS, and operating system are properly configured for WOL functionality, it should wake up.

5. Last

This article introduces how to use .NET to implement the WOL wake-up remote boot function. We learned the basic principles and configuration methods of WOL, and wrote a simple .NET console application to send magic packets. Of course, in actual applications, factors such as network conditions and firewall configuration may also need to be considered. I hope this article can be helpful to you. We will continue to update relevant chapters in the future, and we will conduct actual deployment, application and hardware linkage of this function.

Import address