Community discussions

MikroTik App
 
lopatich
just joined
Topic Author
Posts: 1
Joined: Mon Mar 20, 2017 11:43 am

OSPF cost on dynamic interfaces

Mon Mar 20, 2017 11:51 am

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
 
gustavomam
Trainer
Trainer
Posts: 289
Joined: Tue Jul 23, 2013 6:29 pm
Location: Spain
Contact:

Re: OSPF cost on dynamic interfaces

Tue Mar 21, 2017 5:16 pm

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
-
Eng. Gustavo Angulo
Academy Xperts Spain
Mikrotik Certified Trainer
https://www.youtube.com/watch?v=Cbt2HVYwjYU
 
User avatar
ZeroByte
Forum Guru
Forum Guru
Posts: 4051
Joined: Wed May 11, 2011 6:08 pm

Re: OSPF cost on dynamic interfaces

Tue Mar 21, 2017 5:59 pm

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.
When given a spoon,
you should not cling to your fork.
The soup will get cold.
 
User avatar
shaoranrch
Member Candidate
Member Candidate
Posts: 184
Joined: Thu Feb 13, 2014 8:03 pm

Re: OSPF cost on dynamic interfaces

Thu Mar 23, 2017 8:24 pm

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
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:P ... r_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.
Rafael Carvallo
Telecommunications Engineer

Need consultation?
Need a hotspot with facebook integration?
Send a PM!

Hablamos español, atendemos el mercado de latinoamérica visita nuestra página web:
http://www.tuproximosalto.com
 
ivank
just joined
Posts: 2
Joined: Fri Nov 03, 2017 3:11 am

Re: OSPF cost on dynamic interfaces

Fri Nov 03, 2017 3:38 am

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.
 
JimmyNyholm
Member Candidate
Member Candidate
Posts: 249
Joined: Mon Apr 25, 2016 2:16 am
Location: Sweden

Re: OSPF cost on dynamic interfaces

Sun Nov 05, 2017 2:44 pm

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?
 
ivank
just joined
Posts: 2
Joined: Fri Nov 03, 2017 3:11 am

Re: OSPF cost on dynamic interfaces

Tue Nov 07, 2017 12:27 am

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 "<pptp-username>" to "*F00145". But variable $interface contains "*F00145", not "<pptp-username>". I think this is bug related to a multi-core processor in RB1100AHx2.
 
awardix
just joined
Posts: 1
Joined: Tue Jun 26, 2018 2:14 pm

Re: OSPF cost on dynamic interfaces

Sun Feb 21, 2021 6:46 pm

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]
Last edited by awardix on Sun Feb 21, 2021 9:37 pm, edited 2 times in total.

Who is online

Users browsing this forum: No registered users and 10 guests