aboutsummaryrefslogtreecommitdiffstats
path: root/ipv6-update
blob: b434e05de3499181a54662ffcafa070f92776719 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# RouterOS script: ipv6-update
# Copyright (c) 2013-2018 Christian Hesse <mail@eworm.de>
#
# update firewall and dns settings on IPv6 prefix change

:global "ipv6-interface";
:global "ipv6-pool";

:local addrlist [ / ipv6 firewall address-list find where comment=("ipv6-pool-" . $"ipv6-pool") ];
:local oldprefix [ / ipv6 firewall address-list get $addrlist address ];

# give the interfaces a moment to receive their addresses
:delay 2s;

if ($oldprefix != $"pd-prefix") do={
  :log info ("Updating ipv6 address list with new IPv6 prefix " . $"pd-prefix");
  / ipv6 firewall address-list set address=$"pd-prefix" $addrlist;

  :local oldprefix56 [ :pick $oldprefix 0 [ :find $oldprefix "00::/56" ] ];
  :local oldprefix64 [ :pick $oldprefix 0 [ :find $oldprefix "::/56" ] ];

  :local newprefix [ / ipv6 address get [ / ipv6 address find where from-pool=$"ipv6-pool" interface=$"ipv6-interface" ] address ];
  :local newprefix64 [ :pick $newprefix 0 [ :find $newprefix "::/64" ] ];

  :foreach record in=[ / ip dns static find where address~$oldprefix56 ] do={
    :local address [ / ip dns static get $record address ];
    :local name [ / ip dns static get $record name ];

    :if ( [ :len $name ] = 0) do={
      :set name [ / ip dns static get $record regex ];
    }

    :local suffix [ :pick $address [ :len $oldprefix64 ] [ :len $address ] ];

    :log info ("Updating dns record for " . $name . ": " . $address . " -> " . $newprefix64 . $suffix);
    / ip dns static set address=($newprefix64 . $suffix) $record;
  }
}