Hi,
I have been practicing with VRF in my GNS3 LAB. Everything works fine so far, but when it comes to setting routes for the VRF tables, you have to set your gateway IPs manually:
/ip route
add disabled=no dst-address=0.0.0.0/0 gateway=172.16.10.1@main routing-table=vrf2-eth2 suppress-hw-offload=no
add disabled=no dst-address=0.0.0.0/0 gateway=172.16.10.1@main routing-table=vrf3-eth3 suppress-hw-offload=no
But what if the gateways are dynamic? I know that you need to use some script in the Dhcp-client tab, but I don’t know anything about it. Could you please help me with this?
Thanks
Can anyone help me came up with a useful script? Thanks
:local newGateway $"gateway" ;# The new gateway from the DHCP client
:local vrfList {"vrf2-eth2"; "vrf3-eth3"} ;# List of VRF tables, replace with your actual VRF names
# Check if a valid gateway exists
:if ($newGateway != "") do={
# For each VRF in the list
:foreach vrf in=$vrfList do={
# Find and remove any existing default routes for this VRF table to avoid duplicates
:foreach routeId in=[/ip route find where dst-address=0.0.0.0/0 routing-table=$vrf] do={
/ip route remove $routeId
}
# Add the new default route with the dynamically obtained gateway
/ip route
add dst-address=0.0.0.0/0 gateway=($newGateway . "@main") routing-table=$vrf \
check-gateway=ping comment="Auto-added by DHCP script" disabled=no
}
}
Unfortunately, it doesn’t work as I want, since it doesn’t set the IP gateway for the two vrf routes