0% found this document useful (0 votes)
21 views1 page

Mikrotik WM-CGNAT Scheduler Script

This script removes existing WM-CGNAT configuration and creates a new one. It defines scheduling parameters, removes old NAT and pool objects, creates address lists for excluded networks, and generates a range of NAT and pool objects with incremental ports and IP addresses to provide carrier-grade NAT functionality for a specified number of subnets and users. The script outputs the complete configuration to apply these changes.

Uploaded by

Rafael Sena
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views1 page

Mikrotik WM-CGNAT Scheduler Script

This script removes existing WM-CGNAT configuration and creates a new one. It defines scheduling parameters, removes old NAT and pool objects, creates address lists for excluded networks, and generates a range of NAT and pool objects with incremental ports and IP addresses to provide carrier-grade NAT functionality for a specified number of subnets and users. The script outputs the complete configuration to apply these changes.

Uploaded by

Rafael Sena
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/system scheduler remove [find name="wm-cgnat"];/system scheduler add interval=5s

start-time=startup name=wm-cgnat on-event=":local g 0;:local ip;:local d;:if


([:tostr [/ip pool find name~\"^wm-cgnat/\"]] != \"\") do={:set ip [:pick [/ip pool
get [find name~\"^wm-cgnat/\"] ranges] 0];:if ([:tostr [:find (ip) \"/\"]]\_!
= \"\") do={:set d [/ip pool get [find name~\"^wm-cgnat/\"] name];:if ([:tostr
[:find (d) \"/\"]] != \"\") do={:set d [:tonum [:pick (d) ([:find (d) \"/\"]+1)
[:len (d)]]];:if ((d) > 1 && (d) < 65) do={:if ([:tostr (d)] = \"\" || [:tonum (d)]
> 64) do={:set d 64;\t};:if ([:tostr (g)] = \"\") do={:set g 0;\t};/ip firewall
address-list remove [find list=no-wm-cgnat];/ip firewall nat remove [find
comment~\"^wm-cgnat\"];/ip pool remove [find name~\"^wm-cgnat\"];:foreach i
in=[:toarray \"[Link]/8,[Link]/12,[Link]/16\"] do={/ip firewall address-
list add list=no-wm-cgnat address=(i);/};:local\_p1 0;:local p2 1023;:local ac
netmap;:local ipa;:for i from=0 to=([:len (ip)]-1) do={:local c [:pick (ip)
(i)];:if ((c) = \".\") do={:set ipa ((ipa).\",\");} else={:if ((c) = \"/\" || (c) =
\"-\") do={:set ipa ((ipa).\",\");};:set ipa ((ipa).(c));:if ((c) = \"-\") do={:set
ac src-nat;:set ipa ((ipa).\",\");};};};:set ipa [:toarray (ipa)];:local r;:local
t;:local u;:local a;:local pl \"\";:local n none;:for i from=0 to=((d)-1) do={:set
p1 (p2+1);:set\_p2 ((64511 /(d))+(p2));:if ((i) = ((d)-1)) do={:set p2
65535;};:local p (\"\\\"wm-cgnat [\".(p1).\"-\".(p2).\"]\\\"\");:local cg;:for ii
from=0 to=([:len (ipa)]-1) do={:local c [:pick (ipa) (ii)];:if ((ii) = 0 || (ii) =
5 && [:pick (ipa) (4)] = \"-\") do={:set cg ((cg).\"100\");} else={:if ((ii) != 4)
do={:set cg ((cg).\".\");};:if ((ii) = 1 || (ii) = 6) do={:set cg ((cg).((i)+64));}
else={:set cg ((cg).(c));};};};:foreach ii in=[:toarray \"tcp,udp,all\"] do={:local
h (\"\\\"wm-cgnat (jump --> \".(ii).\")\\\"\");:local j;:if ((i) = 0) do={:set j
(\"/ip firewall nat add chain=srcnat action=jump jump-target=\".(h).\" dst-address-
list=!no-wm-cgnat comment=\".(h));};:local f (\"/ip firewall nat add chain=\".
(h).\" src-address=\".(cg).\" dst-address-list=!no-wm-cgnat action=\".(ac).\" to-
address=\\\"\".(ip).\"\\\" comment=\".(p));:if ((ii) != \"all\") do={:if ((i) = 0)
do={:set j ((j).\" protocol=\".(ii).\";\\n\\r\");};:set f ((f).\" to-ports=\".
((p1).\"-\".(p2)).\" protocol=\".(ii));:if ((ii) = \"tcp\") do={:set t ((t).(j).
(f).\";\\n\\r\");} else={:set u ((u).(j).(f).\";\\n\\r\");};} else={:if ((i) = 0)
do={:set j ((j).\";\\n\\r\");};:set a ((a).(j).(f).\";\\n\\r\");};};:if ((g) = 0)
do={:set r ((r).\"/ip pool add name=\".(p).\" range=\".(cg).\";\\n\\r\");:if ((i) !
= 0 &&\_(i) != (d)) do={:set r ((r).\"/ip pool set [find name=\".(n).\"] next-
pool=\".(p).\";\\n\\r\");};} else={:set pl ((pl).(cg).\",\");};:set n (p);};:if
((g) = 1) do={:set r ((r).\"/ip pool add name=wm-cgnat range=\\\"\".(pl).\"\\\";\\
n\\r\");};:set r ((r).(t).(u).(a));:put (r);[:parse (r)];};};};};";

Ademir Vida, Trainer Mikrotik e criador do Script:


Palestra no Youtube: [Link]

Common questions

Powered by AI

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 .

You might also like