haussli / rancid

RANCiD
Other
114 stars 42 forks source link

Mikrotik exports cut off by scripts in the config #53

Open miken32 opened 1 year ago

miken32 commented 1 year ago

Exports from my device are getting cut off after certain system scripts. It seems to be something to do with the contents of the script; if I install the following script on my router, it and everything in the configuration afterwards is removed:

:local domains [:toarray ""]
:local dnsttl "90m"

:local magiccomment "automatic-from-dhcp (magic comment)"
:local activehosts [:toarray ""]

:foreach lease in [/ip dhcp-server lease find] do={
  :local hostname [/ip dhcp-server lease get value-name=host-name $lease]
  :local hostaddr [/ip dhcp-server lease get value-name=address $lease]

  :if ([:len $hostname] > 0) do={
    :foreach domain in $domains do={
      :local regdomain "$hostname.$domain"
      :set activehosts ($activehosts, $regdomain)

      :if ([:len [/ip dns static find where name=$regdomain]] = 0) do={
        # add new entry
        /ip dns static add name=$regdomain address=$hostaddr comment=$magiccomment ttl=$dnsttl
      } else={
        :if ([:len [/ip dns static find where name=$regdomain address=$hostaddr comment=$magiccomment]] = 0) do={
          # update entry only if IP, name, and comment don't match
          /ip dns static set address=$hostaddr [/ip dns static find name=$regdomain comment=$magiccomment]
        }
      }
    }
  }
}

# remove old entries
:foreach dnsentry in [/ip dns static find where comment=$magiccomment] do={
  :local hostname [/ip dns static get value-name=name $dnsentry]
  :if ([:type [:find $activehosts $hostname]] = "nil") do={
    /ip dns static remove $dnsentry
  }
}

The debug output is a little overwhelming considering my configuration is 600 lines long and each line seems to generate a half dozen lines of debug. How can I narrow down what the problem might be?

miken32 commented 1 year ago

This issue is happening on both RouterOS versions 6.49 and 7.7 under Rancid 3.13. My mtlogin is modified, but only to change the login to use the cte200w username suffix instead of ct200w.

miken32 commented 1 year ago

And this is what the script looks like in the config:

add dont-require-permissions=no name=dns-update owner=admin policy=read,write \
    source=":local domains [:toarray \"\"]\
    \n:local dnsttl \"90m\"\
    \n\
    \n:local magiccomment \"automatic-from-dhcp (magic comment)\"\
    \n:local activehosts [:toarray \"\"]\
    \n\
    \n:foreach lease in [/ip dhcp-server lease find] do={\
    \n  :local hostname [/ip dhcp-server lease get value-name=host-name \$leas\
    e]\
    \n  :local hostaddr [/ip dhcp-server lease get value-name=address \$lease]\
    \n\
    \n  :if ([:len \$hostname] > 0) do={\
    \n    :foreach domain in \$domains do={\
    \n      :local regdomain \"\$hostname.\$domain\"\
    \n      :set activehosts (\$activehosts, \$regdomain)\
    \n\
    \n      :if ([:len [/ip dns static find where name=\$regdomain]] = 0) do={\
    \n        # add new entry\
    \n        /ip dns static add name=\$regdomain address=\$hostaddr comment=\
    \$magiccomment ttl=\$dnsttl\
    \n      } else={\
    \n        :if ([:len [/ip dns static find where name=\$regdomain address=\
    \$hostaddr comment=\$magiccomment]] = 0) do={\
    \n          # update entry only if IP, name, and comment don't match\
    \n          /ip dns static set address=\$hostaddr [/ip dns static find nam\
    e=\$regdomain comment=\$magiccomment]\
    \n        }\
    \n      }\
    \n    }\
    \n  }\
    \n}\
    \n\
    \n# remove old entries\
    \n:foreach dnsentry in [/ip dns static find where comment=\$magiccomment] \
    do={\
    \n  :local hostname [/ip dns static get value-name=name \$dnsentry]\
    \n  :if ([:type [:find \$activehosts \$hostname]] = \"nil\") do={\
    \n    /ip dns static remove \$dnsentry\
    \n  }\
    \n}\
    \n"