Hi all!
Is it possible to assign different ospf cost to different types of tunnels dynamic without creating bindings? For example, at l2tp cost 10, openvpn cost 20, pptp cost 30
Hi.
For this you will need to make static interfaces for your VPN client or server and then create as interface in OSPF interface list in order to set up a cost.
Otherwise you only will have default cost.
Regards
I’m not a script wizard by any stretch, but since the ppp menu allows you to specify an “on-up” script which is triggered at connection time, it should be possible to write a script which adds the OSPF static interface and sets the cost, and an “on-down” script which removes the static interface from OSPF whenever the connection drops.
This is actually quite simple to do, it works with PPPoE, PPTP, SSTP, L2TP and OVPN.
The key here is to use certain variables that are available to you when using the scripting facility of “PPP” (found in PPP → Profiles), you can get more information here https://wiki.mikrotik.com/wiki/Manual:PPP_AAA#User_Profiles but basically you’ve got the following variables:
- 1.- user
2.- local-address
3.- remote-address
4.- caller-id
5.- called-id
6.- interface
We can create then a profile and attach it to the secret you’ve created, say the profile is going to be called “clients-ospf-cost100”, then the following will work (provided you’ve created a secret and associated this profile with said secret):
/ppp profile
add name=clients-ospf-cost100 local-address=10.10.10.1 on-down="/routing ospf interface remove [find interface=\$interface]" on-up=\
"/routing ospf interface add interface=\$interface cost=100" remote-address=10.10.10.2
This will basically execute 2 scripts, one when the tunnel is created that creates the interface and sets its cost to 100 (in reality you can change any parameter of the interface like its type):
/routing ospf interface add interface=$interface cost=100
And another to remove the interface when the tunnel is terminated:
/routing ospf interface remove [find interface=$interface]
Bear in mind that you gotta make sure the IP addresses are already set in an area, on this case you can do this:
/routing ospf network add network=10.10.10.0/24 area=backbone
Else, even though the interface will be created, OSPF won’t be active for it, unlike Cisco’s devices you can’t just add an interface to an area and make it work without specifying it with a network statement (the interface creation doesn’t even have the option).
Also if, for any reason, the router just reboots, you’ll find a lot of interfaces invalid “unknown” in the interface section of /routing ospf, all you gotta do is clean these issuing:
/routing ospf interface remove [find interface ~ "/*F.*"]
This happens because the router couldn’t execute the “on-down” script to clean everything up, this command just uses a RegExp to find all the “unknown” interfaces (always starts with *F) and removes these. A better way, would be to create a schedule to run the above command on every startup, so it cleans everything in case it rebooted without human intervention.
If using Radius there’s a Reply value called “Mikrotik-Group” I have not tested this myself, but I think it may work too (maybe someone could clarify this), to indicate what profile a PPP account should use, if not, you’ll have to stick to setting the scripts into the “default” profile and probably working with scripting more so you can set OSPF interface parameters based on the account’s name.
Hope this helps.
Hi all.
I have troubles with
/routing ospf interface remove [find interface=$interface]
If i execute it by hands (with interface=<interface_name>) all works correctly. But from script it removes all my configured ospf interfaces.
I tested this strange behavior with script
:log info "Interface to remove: $interface"
:log info "Interface ID: $[/routing ospf interface find interface=$interface]"
and it gives me this log entries
12:48:39 script,info Interface to remove: *f00135
12:48:39 script,info Interface ID: *14;Interface ID: *15;Interface ID: *16;Interface ID: *1019E238
For what reasons command “find interface=$interface” returns all interfaces? What’s wrong?
My board is RB1100AHx2 and version is 6.40.3.
I’m definitely out of my comfort zone here. But is not that * kind of ALL to regular expressions in find?
Maybe that. But I do not found anything about special usage of * in scripts. Besides internal ID’s starts with * symbol.
My workaround is using on-down script the same as on reboot. It removes all old interfaces but last. Last is removed on next disconnect.
And in recent tests, I noticed that it first removes old interface with name “*F00144”, and next renames current interface “” to “*F00145”. But variable $interface contains “*F00145”, not “”. I think this is bug related to a multi-core processor in RB1100AHx2.
Working solution:
On up:
/routing ospf interface add interface=$interface cost=100 comment=$interface
On down:
:local i
:set i $interface
/routing ospf interface remove [find comment=$i]