Add interface to VRF by script / API

Hi All

I’m trying to make a script to add 2 interfaces on my Mikrotik and afterwards add the interfaces to already created VRF.

The script to add the interfaces including IP adresse running just fin. But i can’t fine any solution to the VRF problem.

as many of you probable know, the
“/ip route vrf edit number=1 value-name=interfaces”
command just start an editor on the mikrotik.

Any suggestion how to add an interface to a running VRF by script / API?

There’s the “add” command, available in both scripting and API, so f.e.

/ip route vrf add routing-mark=""

Check out all available arguments by typing

/ip route vrf add ?

Or if you mean you want to add them to an existing vrf item, use “set” instead of edit, so f.e.

/ip route vrf set numbers=1 interfaces="ether1"

If i use the set command, all interfaces already in the VRF are removed, and the new is the only on in the VRF.

The add command can only be use if I want to add a new VRF.
getting the following error:
failure: there already exists vrf with this routing mark

That can’t be right.

What RouterOS version are you using? I can’t duplicate this on 6.29.1 on x86.

RouterOS v6.29.1

Yes that is correct behavior of set.

To add new entries at the interface list you need to:

  • get current list
  • append new interfaces to the list
  • set whole list

Wait, what?

If I do

/ip route vrf
add routing-mark=""
add routing-mark="mark1"
add routing-mark="mark2"
print
set 1 interface=ether1
set 2 interface=ether1

The first set runs as expected - all 3 VRF items remain in the list, and the one with “mark1” gets the interface “ether1”, while “” and “mark2” have no interface.
The second set gives out the error “failure: there already exists vrf for this interface”. Doing

set 2 interface=ether2

again does what I would expect - all 3 VRF items remain in the list, and the one with “mark2” gets the interface “ether2”, while “mark1” still has “ether1”, and “” has no interface.


Unless I’m misinterpreting something, what rtorto suggested was that doing even the first set command alone would instead remove the marks “” and “mark2”, and leave just the item with “mark1” in the list, and make it have “ether1”. That’s not what I’m seeing, and it’s certainly not what “set” does on most menus.

I think you misunderstood what was asked.

/ip route vrf
add routing-mark="mark1"
print
set 0 interface=ether1

At this point interface=ether1

set 0 interface=ether2

At this point “interface=ether2” not “interface=ether1,ether2” as rtorto wanted to be.

Oooh… I see.

Here we are, ten years later, and you still cannot add an interface to an existing VRF without replacing the existing ones…

However, what seems to work instead is to use an interface list instead of specific interface names. That way you can dynamically add and remove members as required.

Necroposting for nothing.

The OP is very poorly written and explained even worse.


Don't write bulls–t.
It's you who can't do it, not others. MikroTik can't fix your incompetence.

Open winbox and add it, EASY, or else LEARN how to do it with CLI/scripts.
[rex@example] /ip route vrf> add routing-mark=test
[rex@example] /ip route vrf> pri
Flags: X - disabled, I - inactive
0 routing-mark=test interfaces=""

[rex@example] /ip route vrf> set [find where routing-mark=test] interfaces=sfp9
[rex@example] /ip route vrf> pri
Flags: X - disabled, I - inactive
0 routing-mark=test interfaces=sfp9

sfp9 as expected

[rex@example] /ip route vrf> set [find where routing-mark=test] interfaces=sfp10
[rex@example] /ip route vrf> pri
Flags: X - disabled, I - inactive
0 routing-mark=test interfaces=sfp10

with SET sfp9 is replaced, not added, with sfp10 as expected

[rex@example] /ip route vrf> set [find where routing-mark=test] interfaces=sfp9,sfp10
[rex@example] /ip route vrf> pri
Flags: X - disabled, I - inactive
0 routing-mark=test interfaces=sfp9,sfp10

with SET sfp10 is replaced, not added, with sfp9 AND sfp10 as expected

[rex@example] /ip route vrf> set [find where routing-mark=test] interfaces=([get [find where routing-mark=test] interfaces],"sfp11")
[rex@example] /ip route vrf> pri
Flags: X - disabled, I - inactive
0 routing-mark=test interfaces=sfp9,sfp10,sfp11

this correctly add sfp11 with the other interfaces

work also if previous list is empty

Everything works as someone familiar with RouterOS would expect.

Obviously it should be checked before adding an interface if the interface is already there...

Obviously the script will be more complex if an interface needs to be removed from the list, rather than simply added...
but on winbox it's one click...

Obviously the obvious is obvious...

@rextended
While you are at it, and of course only if you feel like it, could you also post a v7.x approach?

I have seen here and there references to vrf’s in 7.x having the possibility to use interface lists, but have not seen any practical example on how exactly to implement and manage them.

i haven’t tested in v7
does that bechive the same?

More easy add/remove compared with v6 if you use interface list:

create vrf and list, or better, first the list, then the vrf...

/interface list
add name=listForVRFtest
/ip vrf
add interfaces=listForVRFtest name=vrfTEST

add interface

/interface list member
add list=listForVRFtest interface=ether3

remove interface

/interface list member
remove [find whrere list=listForVRFtest and interface=ether3]
/ip vrf
add interfaces=ether3 name=vrfTEST

must be present at least one interface

/ip vrf
set [find where name=vrfTEST] interfaces=ether5

set the interface to ether5

/ip vrf
set [find where name=vrfTEST] interfaces=([get [find where name=vrfTEST] interfaces],"ether4")

add interface ether4

I see :slight_smile:.
Undoubtedly v7.x with interface list is easier and much more intuitive, thanks.

I add on previous post “example on v7 without list code”

I forgot to specify that you can specify BOTH a list AND an interface in the v7 VRF definition.
for example:

/ip vrf
add name=vrfTEST interfaces=listForVRFtest,ether3

and both previous v7 methods are valid.

Then I won’t investigate further what happens if an interface is both in the list and specified directly…