Mikrotik WM-CGNAT Scheduler Script
Mikrotik WM-CGNAT Scheduler Script
The script defines firewall rules for different protocols (tcp, udp, all) during its setup by iterating over each protocol type and setting rules specific to each, ensuring that NAT actions are appropriately mapped to these protocols. It achieves this via nested loops where protocol-specific actions like `src-nat` and ranges are defined and validated .
Using hard-coded IP ranges like `10.0.0.0/8` can simplify initial configurations but poses drawbacks such as flexibility reduction and potential conflicts in dynamically changing environments. It limits adaptability which is crucial for scaling, as any IP address renumbering would require script re-evaluation and adjustments manually, increasing maintenance overhead .
The 'jump' label in the script's firewall NAT configuration is used to specify a new chain to continue processing after the current action. This is seen in the command `/ip firewall nat add chain=srcnat action=jump jump-target=...`, facilitating more granular control over traffic processing such as protocol-specific handling (tcp, udp).
The script uses `:toarray` to split IP range strings into manageable parts for easier processing, enabling operations like parsing through specific parts with loops and conditional checks. This facilitates detailed configuration setups such as addressing and mapping in the NAT rules by treating each part of the IP address separately .
Configuring next-pool links in IP pool definitions ensures seamless transitions between pools, handling cases where one pool's addresses are exhausted and requires overflow capability into another defined pool. This script uses logical conditions to set `next-pool` on secondary iterations ensuring continuous availability of IP resources .
The script's loop evaluates contents within 'd' and 'g' variables to manage dynamic allocation and adjustment of pool ranges, ensuring that IP pools are correctly defined and linked. The conditions help in determining if adjustments like setting `d` to 64 should happen or if the initial configurations need tweaking based on current evaluations .
The script prevents overlap by removing any entries in the `address-list` with `list=no-wm-cgnat` and clearing NAT rules with comments matching `^wm-cgnat`. It then re-adds the correct configurations for the necessary address lists and NAT settings ensuring no mismatches or overlaps .
Checking for empty strings and invalid numerics helps avoid unintended configurations or failures during script execution. In this script, examining 'd' and 'g' ensures that range settings are valid and logical adjustments are made, preventing incorrect pool settings or firewall rules that could lead to network vulnerabilities or misconfigurations .
The script employs dynamic adjustments through calculated p1 and p2 values, adjusting pool size in subdivisions between ports. This method is achieved with expressions like `(64511 /(d))+(p2)` that calculate new ranges dynamically, adapting configuration for precise control over pool distribution and adjustments .
The script extracts and checks the IP pool name using the command `:set ip [:pick [/ip pool get [find name~"^wm-cgnat/"] ranges] 0]`, ensuring that it matches the regex pattern `^wm-cgnat/`. If the pool name is not correctly set, the script adjusts the range settings accordingly .