Community discussions

MikroTik App
 
kolinsb
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Sat Jun 30, 2007 7:22 pm

routing with dynamic gateway

Sat Mar 27, 2010 7:52 pm

The situation is like this. I have two PPPoE connections. With one provider is a dynamic gateway, and one static. I have 2 routing mark. with one is marked "free zone" to a provider, and other internet provider to another. at the first where the static gateway, default route is created with the appropriate routing mark, and it works. I have a problem with this with a dynamic gateway. I found a script that dynamically finds the gateway to the PPPoE client connection, and that creates defoult route with the appropriate routing mark and gateway, but that script not working on my v4.6 :( Here's the script:

# name of interface that should be monitored 
# (edit to match your interface name)
:set mInterface my_pppoe_connection;

# routing mark name used for default route that should be updated
# (edit to match your routing mark name)
:set mRoutingMark my_other_routing_mark;

/ip address ; 
:set mAddress [find interface=$mInterface] ;
:set mGateway [get $mAddress network]  ;

/ip route ;
:set mGatewayFound 0
:foreach i in  [find routing-mark=$mRoutingMark] do={ \
  :set mDstAddress [get $i dst-address]; \
  :set qResult [:find $mDstAddress "0.0.0.0/0"]; \
  :if ($qResult = 0) do={ \
    set $i gateway=$mGateway ; \
    :put "gateway rule adjusted"; \
    :set mGatewayFound 1; \
   }; \
}; 
:if ($mGatewayFound = 0) do={ ; \
  add dst-address=0.0.0.0/0 gateway=$mGateway routing-mark=$mRoutingMark; \
  :put "gateway rule added"; \
}; 
If I understand it, this script needs to create a default route for the given data from the PPPoE connection that I have configured and routing mark that I configured. it to me just does not work. script does not create a default route to this information. what's wrong with this script? can someone help me? thanks!
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: routing with dynamic gateway

Sat Mar 27, 2010 10:40 pm

I would routing-mark only the static route traffic and leave the dynamic route as default. The dynamic route is set by your ISP's dhcp server, correct? The 0.0.0.0/0 route with the routing-mark will be used by packets with the routing mark. All else will go out the dynamic route. Would that work for you?

ADD: The script needs all the variables declared as global or local. And if you want to run it scheduled, replace the ":put" with ":log info".
 
kolinsb
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Sat Jun 30, 2007 7:22 pm

Re: routing with dynamic gateway

Sun Mar 28, 2010 4:13 pm

there is my mikrotik setup. but it is not working :(

Image
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: routing with dynamic gateway

Sun Mar 28, 2010 4:31 pm

I need to see "/ip firewall mangle" and "/ip firewall nat". Insure to include the routing mark entries.

ADD: I also do not see an ip address for the gateway on your static route. It should be set to the ip your ISP gave you for that network.
 
kolinsb
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Sat Jun 30, 2007 7:22 pm

Re: routing with dynamic gateway

Sun Mar 28, 2010 6:18 pm

there is "ip firewall mangle print":
 /ip firewall mangle> print
Flags: X - disabled, I - invalid, D - dynamic 
 0   chain=prerouting action=mark-routing new-routing-mark=ostalo 
     passthrough=no src-address=192.168.50.0/24 dst-address-list=!free_zona 

 1   chain=prerouting action=mark-routing new-routing-mark=free_zona 
     passthrough=no src-address=192.168.50.0/24 dst-address-list=free_zona 
there is "ip firewall print"
ip firewall nat print
Flags: X - disabled, I - invalid, D - dynamic
0 ;;; NAT opsega 192.168.50.0/24
chain=srcnat action=masquerade src-address=192.168.50.0/24

1 ;;; pptp maskarada
chain=srcnat action=masquerade src-address=192.168.200.0/24

2 X chain=dstnat action=dst-nat to-addresses=192.168.0.1 to-ports=81
protocol=tcp in-interface=kolins_adsl dst-port=80

3 ;;; FTP server
chain=dstnat action=dst-nat to-addresses=192.168.0.3 to-ports=50
protocol=tcp in-interface=kolins_adsl dst-port=21

4 ;;; MW2_UDP_1500
chain=dstnat action=dst-nat to-addresses=192.168.50.14 to-ports=1500
protocol=udp in-interface=kolins_adsl dst-port=1500

5 ;;; MW2_UDP_3005
chain=dstnat action=dst-nat to-addresses=192.168.50.14 to-ports=3005
protocol=udp in-interface=kolins_adsl dst-port=3005

6 ;;; Mw2_UDP_3101
chain=dstnat action=dst-nat to-addresses=192.168.50.14 to-ports=3101
protocol=udp in-interface=kolins_adsl dst-port=3101

7 ;;; MW2_UDP_28960
chain=dstnat action=dst-nat to-addresses=192.168.50.14 to-ports=28960
protocol=udp in-interface=kolins_adsl dst-port=28960
and there is routing table:

Image
 
kolinsb
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 63
Joined: Sat Jun 30, 2007 7:22 pm

Re: routing with dynamic gateway

Sun Mar 28, 2010 7:27 pm

thanks for any help... i couldn't waiting anymore, and i made my own script :) there is:
:local currentGateway [ /ip address get [/ip address find interface=interface_name ] network]

:local existingGateway [/ip route get [/ip route find comment="Comment on route which gateway we want to set"] gateway]


:if ($currentGateway != $existingGateway) do={

  :log info ("Setting default gateway for our pppoe interface " )
  :local a [ /ip route find comment="Comment on route which gateway we want to set"]

:if ([ :typeof $a ] = nil ) do={
    :log info "adding route..."
    /ip route add dst-address=0.0.0.0/0 gateway=$currentGateway routing-mark=the_name_of_the_mark comment="Comment on route which gateway we want to setK"
   } else={
    :log info "changing route..."
    /ip route set $a gateway=$currentGateway
   }
} else={
  :log info "No route changes needed."
}
this script need to put in the scheduler and set the interval time. this working for me now. i didn't tested it a lot of time, but for now is working.
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: routing with dynamic gateway

Sun Mar 28, 2010 8:29 pm

I understand. But in the future, you need to use the routing mark in the "/ip firewall nat" to select the out-interface. I used ether1 as the static interface in this example. The srcnat with the routing mark must be before the other in the list.

/ip firewall nat
add chain=srcnat action=masquerade out-interface=ether1 routing-mark=free_zona
add chain=srcnat action=masquerade out-interface=ether2
 
adroman
just joined
Posts: 16
Joined: Tue May 29, 2018 3:54 pm

Re: routing with dynamic gateway

Fri Oct 26, 2018 2:24 am

Hello, very good post!
Plz explain how to use this second working script for ether1 interface..
Your first script doesn't work for my RouterOS as well.