Community discussions

MikroTik App
 
Xylex314
just joined
Topic Author
Posts: 1
Joined: Thu Sep 21, 2023 12:56 pm

Static Route According to dhcp assigned Gateway

Thu Sep 21, 2023 1:24 pm

Hello together,
i´m totally new to Mikrotik, nevertheless i find the devices very powerfull but still affordable.

I try to use the router as vpn endpoint behind a home router. So the Device IP is local and assigned via dhcp. All traffic behind the Mikrotik should be forced trough the tunnel..
I figured out that the best way to do this is to change the default gateway to my VPN Interface, but leave the route to the rout to the VPN i try to connect to assigned to the Internetendpoint/Router in the net before the MikrotikNet:

/ip route
add disabled=no distance=1 dst-address=178.x.x.x/32 gateway=\
192.168.178.1 pref-src="" routing-table=main scope=30 \
suppress-hw-offload=no target-scope=10
add disabled=no distance=1 dst-address=0.0.0.0/0 gateway=ether1 pref-src="" \
routing-table=main suppress-hw-offload=no


My question is. How can i set the route 1 (to the VPN i try to connect to) according to the ip configuration i get via dhcp from the router in the net before?

i found a script but it does not work, i can´t figure out why:

:local currentGateway [ /ip address get [/ip address find interface=ether1 ] network]
:log info "2"
:local existingGateway [/ip route get [/ip route find comment="ipsectogw"] gateway]
:log info "3"
:if ($currentGateway != $existingGateway) do={
:log info "Setting default gateway for our ether1 interface "
:local a [ /ip route find comment="ipsectogw"]
:if ([ :typeof $a ] = nil ) do={
:log info "adding route..."
/ip route add dst-address=178.xx.xx.xx/32 gateway=$currentGateway routing-mark=ipsectogwmark comment="ipsectogw"
} else={
:log info "changing route..."
/ip route set $a gateway=$currentGateway
}
} else={
:log info "No route changes needed."
}
:log info "last line"


The Script should rewrite the commented route when insertet in the scheduler, but it does not work...
Log entries are added to see when it fails, but this also only works when i enter the script line by line in the scheduler.
It breaks in the "if... part.

Every help is appreciated

Thanks in advance

Who is online

Users browsing this forum: noyo and 7 guests