为Mikrotik自动添加DHCP客户端,解决因MAC地址变化时接口失效

Automating MikroTik DHCP Client and WAN List Configuration

In this tutorial, we will walk you through a MikroTik script that automates the process of:

  • Adding Ethernet interfaces to the WAN interface list.
  • Enabling DHCP client on each interface, but avoiding duplicates.

This script is designed to ensure all interfaces are added to the WAN list and DHCP clients are activated without any redundancy.

Prerequisites

  • A MikroTik router with RouterOS version 6.x or higher.
  • Basic knowledge of MikroTik's command-line interface (CLI).
  • Access to the MikroTik router via WinBox, SSH, or WebFig.

The Problem

When you clone a MikroTik router or add new interfaces, you might need to:

  1. Automatically add all interfaces to the WAN interface list.
  2. Enable DHCP client on all Ethernet interfaces that do not already have it configured.

This script will solve both issues by checking the interfaces and ensuring the necessary configuration is applied.

Solution

The MikroTik Script

Here's the complete script that adds interfaces to the WAN list and enables DHCP client without duplication:

# Automatically detect all Ethernet interfaces, add them to the 'WAN' list if not already there, and enable DHCP client only if not already enabled
:foreach i in=[/interface ethernet find] do={

    :local intName [/interface get $i name]

    # Check if the interface is already in the 'WAN' list
    :local alreadyInList false
    :foreach j in=[/interface list member find where list="WAN"] do={
        :if ([/interface list member get $j interface] = $intName) do={
            :set alreadyInList true
        }
    }

    # If not in the WAN list, add it
    :if ($alreadyInList = false) do={
        /interface list member add list=WAN interface=$intName
        :log info ("Added interface " . $intName . " to interface list WAN")
    }

    # Check if DHCP client is already enabled on the interface
    :local dhcpExists false
    :foreach dhcp in=[/ip dhcp-client find] do={
        :if ([/ip dhcp-client get $dhcp interface] = $intName) do={
            :set dhcpExists true
        }
    }

    # If DHCP client is not already enabled, add it
    :if ($dhcpExists = false) do={
        /ip dhcp-client add interface=$intName use-peer-dns=yes add-default-route=yes disabled=no
        :log info ("Enabled DHCP client on interface " . $intName)
    }
}

How the Script Works

  1. Check for existing interfaces in the 'WAN' list: The script will first check if the interface is already added to the WAN list. If it is not, it will add the interface to the list.
  2. Check for existing DHCP client configuration: It then checks if a DHCP client is already enabled on the interface. If not, it will configure the DHCP client to obtain an IP address and set the necessary parameters such as peer DNS and default route.
  3. Avoid duplication: By checking if the DHCP client is already enabled, the script avoids unnecessary duplication of the configuration.

How to Use the Script

  1. Open your MikroTik router's CLI via WinBox, SSH, or WebFig.
  2. Copy the script above and paste it into the terminal.
  3. Run the script.
  4. Optionally, you can create a scheduled task to run the script on startup so that it automatically configures your interfaces each time the router reboots.

Conclusion

This MikroTik script provides a simple and effective way to manage DHCP clients and interface lists. By automating the process, you can save time and avoid configuration errors. The script ensures that all Ethernet interfaces are properly configured with minimal effort and without duplicates.


Feel free to modify the script to fit your specific network setup, such as filtering specific interfaces or adding additional configurations.

Happy networking!

posted on 2025-04-10 22:06  项希盛  阅读(30)  评论(0)    收藏  举报