rancid icon indicating copy to clipboard operation
rancid copied to clipboard

Mikrotik exports cut off by scripts in the config

Open miken32 opened this issue 2 years ago • 2 comments

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 avatar Jan 12 '23 22:01 miken32

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 avatar Jan 12 '23 22:01 miken32

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"

miken32 avatar Jan 12 '23 22:01 miken32

I have similar problem. Mikrotik with big script are backed up partially. The worst thing is that no error is generated. There is simply no configuration dumped that is usually after scripts.

psztoch avatar Sep 17 '24 11:09 psztoch