mikrotik #2115
Wleed59944
started this conversation in
General
mikrotik
#2115
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
fix
اصدار الميكروتك
RouterOS 6.48.6
software id = FWAP-5PHP
model = RB2011UiAS-2HnD
serial number = BE460BF23EC2م
منفذ الخروج ether10
/ip firewall mangle
--- Weighted PCC for Load Balancing ---
Total 16 categories: 6 for ADSL (weight 1 each), 10 for 4G (weight 5 each, 2 lines * 5 = 10)
This distributes connections proportionally to line speeds.
ADSL Lines (6 lines, 1 category each)
add action=mark-connection chain=prerouting new-connection-mark=conn_ISP1 passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/0 comment="PCC_WAN1"
add action=mark-routing chain=prerouting connection-mark=conn_ISP1 new-routing-mark=to_ISP1 passthrough=no comment="PCC_WAN1_Route"
add action=mark-connection chain=prerouting new-connection-mark=conn_ISP2 passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/1 comment="PCC_WAN2"
add action=mark-routing chain=prerouting connection-mark=conn_ISP2 new-routing-mark=to_ISP2 passthrough=no comment="PCC_WAN2_Route"
add action=mark-connection chain=prerouting new-connection-mark=conn_ISP3 passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/2 comment="PCC_WAN3"
add action=mark-routing chain=prerouting connection-mark=conn_ISP3 new-routing-mark=to_ISP3 passthrough=no comment="PCC_WAN3_Route"
add action=mark-connection chain=prerouting new-connection-mark=conn_ISP4 passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/3 comment="PCC_WAN4"
add action=mark-routing chain=prerouting connection-mark=conn_ISP4 new-routing-mark=to_ISP4 passthrough=no comment="PCC_WAN4_Route"
add action=mark-connection chain=prerouting new-connection-mark=conn_ISP5 passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/4 comment="PCC_WAN5"
add action=mark-routing chain=prerouting connection-mark=conn_ISP5 new-routing-mark=to_ISP5 passthrough=no comment="PCC_WAN5_Route"
add action=mark-connection chain=prerouting new-connection-mark=conn_ISP6 passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/5 comment="PCC_WAN6"
add action=mark-routing chain=prerouting connection-mark=conn_ISP6 new-routing-mark=to_ISP6 passthrough=no comment="PCC_WAN6_Route"
4G Lines (2 lines, 5 categories each)
add action=mark-connection chain=prerouting new-connection-mark=conn_WAN7_4G passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/6 comment="PCC_WAN7_1"
add action=mark-connection chain=prerouting new-connection-mark=conn_WAN7_4G passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/7 comment="PCC_WAN7_2"
add action=mark-connection chain=prerouting new-connection-mark=conn_WAN7_4G passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/8 comment="PCC_WAN7_3"
add action=mark-connection chain=prerouting new-connection-mark=conn_WAN7_4G passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/9 comment="PCC_WAN7_4"
add action=mark-connection chain=prerouting new-connection-mark=conn_WAN7_4G passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/10 comment="PCC_WAN7_5"
add action=mark-routing chain=prerouting connection-mark=conn_WAN7_4G new-routing-mark=to_WAN7_4G passthrough=no comment="PCC_WAN7_Route"
add action=mark-connection chain=prerouting new-connection-mark=conn_WAN8_4G passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/11 comment="PCC_WAN8_1"
add action=mark-connection chain=prerouting new-connection-mark=conn_WAN8_4G passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/12 comment="PCC_WAN8_2"
add action=mark-connection chain=prerouting new-connection-mark=conn_WAN8_4G passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/13 comment="PCC_WAN8_3"
add action=mark-connection chain=prerouting new-connection-mark=conn_WAN8_4G passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/14 comment="PCC_WAN8_4"
add action=mark-connection chain=prerouting new-connection-mark=conn_WAN8_4G passthrough=yes
in-interface=ether10 dst-address-type=!local per-connection-classifier=both-addresses-and-ports:16/15 comment="PCC_WAN8_5"
add action=mark-routing chain=prerouting connection-mark=conn_WAN8_4G new-routing-mark=to_WAN8_4G passthrough=no comment="PCC_WAN8_Route"
--- Mark connections coming FROM WAN interfaces (for reply traffic) ---
These rules are important for ensuring reply traffic goes back through the correct WAN interface.
add action=mark-connection chain=input in-interface=ISP1_PPPoE new-connection-mark=conn_ISP1 passthrough=yes comment="Reply_WAN1"
add action=mark-connection chain=input in-interface=ISP2_PPPoE new-connection-mark=conn_ISP2 passthrough=yes comment="Reply_WAN2"
add action=mark-connection chain=input in-interface=ISP3_PPPoE new-connection-mark=conn_ISP3 passthrough=yes comment="Reply_WAN3"
add action=mark-connection chain=input in-interface=ISP4_PPPoE new-connection-mark=conn_ISP4 passthrough=yes comment="Reply_WAN4"
add action=mark-connection chain=input in-interface=ISP5_PPPoE new-connection-mark=conn_ISP5 passthrough=yes comment="Reply_WAN5"
add action=mark-connection chain=input in-interface=ISP6_PPPoE new-connection-mark=conn_ISP6 passthrough=yes comment="Reply_WAN6"
add action=mark-connection chain=input in-interface=WAN7_4G new-connection-mark=conn_WAN7_4G passthrough=yes comment="Reply_WAN7"
add action=mark-connection chain=input in-interface=WAN8_4G new-connection-mark=conn_WAN8_4G passthrough=yes comment="Reply_WAN8"
--- Accept related/established connections ---
add action=accept chain=prerouting connection-state=established,related comment="Accept_Established_Related"
--- Drop invalid connections ---
add action=drop chain=prerouting connection-state=invalid comment="Drop_Invalid"
/ip firewall nat
--- Masquerade for all WAN interfaces ---
add action=masquerade chain=srcnat out-interface=ISP1_PPPoE
add action=masquerade chain=srcnat out-interface=ISP2_PPPoE
add action=masquerade chain=srcnat out-interface=ISP3_PPPoE
add action=masquerade chain=srcnat out-interface=ISP4_PPPoE
add action=masquerade chain=srcnat out-interface=ISP5_PPPoE
add action=masquerade chain=srcnat out-interface=ISP6_PPPoE
add action=masquerade chain=srcnat out-interface=WAN7_4G
add action=masquerade chain=srcnat out-interface=WAN8_4G
--- Optional: DST-NAT rules from user's config (adjust as needed) ---
add action=dst-nat chain=dstnat dst-port=8899 protocol=tcp to-addresses=192.168.88.254 to-ports=8899
add action=dst-nat chain=dstnat dst-port=8855 protocol=tcp to-addresses=192.168.88.254 to-ports=8855
add action=dst-nat chain=dstnat dst-port=8877 protocol=tcp to-addresses=192.168.88.254 to-ports=8877
/ip route
Distance is 1 for all to ensure PCC takes precedence.
add distance=1 gateway=ISP1_PPPoE routing-mark=to_ISP1 check-gateway=ping comment="PCC_Route_WAN1"
add distance=1 gateway=ISP2_PPPoE routing-mark=to_ISP2 check-gateway=ping comment="PCC_Route_WAN2"
add distance=1 gateway=ISP3_PPPoE routing-mark=to_ISP3 check-gateway=ping comment="PCC_Route_WAN3"
add distance=1 gateway=ISP4_PPPoE routing-mark=to_ISP4 check-gateway=ping comment="PCC_Route_WAN4"
add distance=1 gateway=ISP5_PPPoE routing-mark=to_ISP5 check-gateway=ping comment="PCC_Route_WAN5"
add distance=1 gateway=ISP6_PPPoE routing-mark=to_ISP6 check-gateway=ping comment="PCC_Route_WAN6"
add distance=1 gateway=192.168.7.1 routing-mark=to_WAN7_4G check-gateway=ping comment="PCC_Route_WAN7_4G"
add distance=1 gateway=192.168.20.1 routing-mark=to_WAN8_4G check-gateway=ping comment="PCC_Route_WAN8_4G"
--- Default Routes (for failover and dynamic distance adjustment) ---
These routes are used if no PCC rule matches or for dynamic distance changes.
Initial distances prioritize ADSL, then 4G.
The script will dynamically adjust these distances based on load/status.
add distance=10 gateway=ISP1_PPPoE check-gateway=ping comment="Default_WAN1"
add distance=11 gateway=ISP2_PPPoE check-gateway=ping comment="Default_WAN2"
add distance=12 gateway=ISP3_PPPoE check-gateway=ping comment="Default_WAN3"
add distance=13 gateway=ISP4_PPPoE check-gateway=ping comment="Default_WAN4"
add distance=14 gateway=ISP5_PPPoE check-gateway=ping comment="Default_WAN5"
add distance=15 gateway=ISP6_PPPoE check-gateway=ping comment="Default_WAN6"
add distance=20 gateway=192.168.7.1 check-gateway=ping comment="Default_WAN7_4G"
add distance=21 gateway=192.168.20.1 check-gateway=ping comment="Default_WAN8_4G"
/system script add name="smart_lb_script" source="
:local adsl_interfaces {"ISP1_PPPoE"; "ISP2_PPPoE"; "ISP3_PPPoE"; "ISP4_PPPoE"; "ISP5_PPPoE"; "ISP6_PPPoE"}; \n
:local all_wan_interfaces {"ISP1_PPPoE"; "ISP2_PPPoE"; "ISP3_PPPoE"; "ISP4_PPPoE"; "ISP5_PPPoE"; "ISP6_PPPoE"; "WAN7_4G"; "WAN8_4G"}; \n
:local adsl_speeds {"ISP1_PPPoE":3; "ISP2_PPPoE":4; "ISP3_PPPoE":4; "ISP4_PPPoE":4; "ISP5_PPPoE":2; "ISP6_PPPoE":2}; \n
:local adsl_load_threshold_percent 80; \n
:local adsl_distance_normal 10; \n
:local adsl_distance_overload 100; \n
:local adsl_distance_down 250; \n
:local fourg_distance_normal 20; \n
:local fourg_distance_down 250; \n
\n# --- Function to update route distances based on load/status --- \n
:foreach i in=$adsl_interfaces do={ \n
:local route_id [/ip route find comment="Default_" . [:pick $i 0 ([:len $i] - 6)]]; \n
:local current_distance [/ip route get $route_id distance]; \n
:local interface_status [/interface get $i running]; \n
:local rx_rate [/interface get $i rx-byte]; \n
:local tx_rate [/interface get $i tx-byte]; \n
:local current_speed_mbps (([$rx_rate + $tx_rate] * 8) / 1000000); \n
:local declared_speed_mbps ($adsl_speeds->$i); \n
:local load_percent ($current_speed_mbps / $declared_speed_mbps * 100); \n
\n :if ($interface_status = false) do={ \n
:if ($current_distance != $adsl_distance_down) do={ \n
/ip route set $route_id distance=$adsl_distance_down; \n
:log warning "ADSL line " . $i . " is down. Setting distance to $adsl_distance_down."; \n
} \n
} else={ \n
:if ($load_percent > $adsl_load_threshold_percent) do={ \n
:if ($current_distance != $adsl_distance_overload) do={ \n
/ip route set $route_id distance=$adsl_distance_overload; \n
:log info "ADSL line " . $i . " is overloaded ($load_percent%). Setting distance to $adsl_distance_overload."; \n
} \n
} else={ \n
:if ($current_distance != $adsl_distance_normal) do={ \n
/ip route set $route_id distance=$adsl_distance_normal; \n
:log info "ADSL line " . $i . " is normal ($load_percent%). Setting distance to $adsl_distance_normal."; \n
} \n
} \n
} \n
} \n
\n# --- Update 4G route distances based on status --- \n
:local wan7_route_id [/ip route find comment="Default_WAN7_4G"]; \n
:local wan8_route_id [/ip route find comment="Default_WAN8_4G"]; \n
:local wan7_status [/interface get WAN7_4G running]; \n
:local wan8_status [/interface get WAN8_4G running]; \n
\n:if ($wan7_status = false) do={ \n
:if ([/ip route get $wan7_route_id distance] != $fourg_distance_down) do={ \n
/ip route set $wan7_route_id distance=$fourg_distance_down; \n
:log warning "4G line WAN7_4G is down. Setting distance to $fourg_distance_down."; \n
} \n
} else={ \n
:if ([/ip route get $wan7_route_id distance] != $fourg_distance_normal) do={ \n
/ip route set $wan7_route_id distance=$fourg_distance_normal; \n
:log info "4G line WAN7_4G is up. Setting distance to $fourg_distance_normal."; \n
} \n
} \n
\n:if ($wan8_status = false) do={ \n
:if ([/ip route get $wan8_route_id distance] != $fourg_distance_down) do={ \n
/ip route set $wan8_route_id distance=$fourg_distance_down; \n
:log warning "4G line WAN8_4G is down. Setting distance to $fourg_distance_down."; \n
} \n
} else={ \n
:if ([/ip route get $wan8_route_id distance] != $fourg_distance_normal) do={ \n
/ip route set $wan8_route_id distance=$fourg_distance_normal; \n
:log info "4G line WAN8_4G is up. Setting distance to $fourg_distance_normal."; \n
} \n
} \n
\n# --- Self-Healing for Inactive Lines (Run less frequently, e.g., every 5 minutes) --- \n
:local current_time [system clock get time]; \n
:local last_run_self_heal [/system scheduler get smart_lb_self_heal_tracker last-run]; \n
:if (([len $last_run_self_heal] = 0) || (([:parse $current_time - $last_run_self_heal] / 60) >= 5)) do={ \n
:log info "Running self-healing check for inactive lines."; \n
:foreach i in=$all_wan_interfaces do={ \n
:local interface_running [/interface get $i running]; \n
:local rx_rate [/interface get $i rx-byte]; \n
:local tx_rate [/interface get $i tx-byte]; \n
:if ($interface_running = true && ($rx_rate < 1000 && $tx_rate < 1000)) do={ \n
:log warning "Interface " . $i . " is running but inactive. Attempting to restart."; \n
/interface disable $i; \n
/delay 1s; \n
/interface enable $i; \n
:if ([:find $i "PPPoE"] != -1) do={ \n
# For PPPoE, try to reconnect \n
:local pppoe_id [/interface pppoe-client find name=$i]; \n
:if ($pppoe_id != "") do={ \n
/interface pppoe-client disable $pppoe_id; \n
/delay 1s; \n
/interface pppoe-client enable $pppoe_id; \n
} \n
} \n
} \n
} \n
/system scheduler set smart_lb_self_heal_tracker last-run=$current_time; \n
}"
/system scheduler add name="smart_lb_main" on-event="smart_lb_script" interval=30s start-time=startup
/system scheduler add name="smart_lb_self_heal_tracker" on-event="" interval=5m start-time=startup disabled=yes comment="This scheduler is for tracking last run time of self-healing within the main script."
Beta Was this translation helpful? Give feedback.
All reactions