save export to variable

Hallo,

I want to write an export script to reuse the exported settings with other hardware. Because of hardware specific things like MAC-Address I want to filter such things like the MAC.
First step I need is the text of the export command is saved to a variable. But how?

The following simple code doesn’t work.

:local Text
:set Text ([/interface wireless sniffer export])

The variable Text is empty.

How to save this export text in the variable Text?

Thanks in advance.

Not directly related to the question, but if you’re going to create a full configuration export as a final goal, that’s likely going to be larger than 4096 bytes, which is the size limit for RouterOS variables.

Yes 4096 could be too little.

Is it possible when I export to a file to delete some strings (for example mac addresses) in the file?

You can’t store the direct export as a variable, but…
you can obtain the same output using print as-value.

The below code will output wireless interface settings to file(s). Each file can be independently run using ‘/import’. Also, if a file would exceed 4096 bytes, it increments to the next file (wireless-interface1, wireless-interface2, etc…)

# Do not include '.rsc', it is appended automatically
:local filename "wireless-interface"

# Internal processing below...
# ----------------------------------
:local line
:local contents
:local fileindex 1
:local findindex
:local property
:local value
/interface wireless {
   :set contents "/interface wireless\n"
# loop through wireless interfaces, getting their advanced (plus basic) settings
   :foreach r in=[print advanced as-value] do={
      :set line ""
      :foreach i in=[:toarray $r] do={
         :set findindex [:find [:tostr $i] "="]
         :set property [:pick [:tostr $i] 0 $findindex]
         :set value [:pick [:tostr $i] ($findindex + 1) [:len [:tostr $i]]]

#      do not include '.id' property, it will be different on other routers
#      do not include interface-'type' property, it is read-only
         :if ([:tostr $property] = ".id" || \
               [:tostr $property] = "interface-type") do={
            :set property ""
            :set value ""
         }

#      put quotes around comment property
#      put quotes around value if it contains spaces
         :if ([:tostr $property] = "comment" || \
               [:len [:find [:tostr $value] " "]] > 0) do={
            :set value ("\"" . $value . "\"")
         }

#      append property=value to line
         :if ([:len $property] > 0 && [:len $value] > 0) do={

#         use [find] with mac-address (easier import to other routers)
            :if ([:tostr $property] = "mac-address") do={
               :set line ("[find mac-address=\"" . $value . "\"]" . " " . [:tostr $line])
            } else={
               :set line ([:tostr $line] . $property . "=" . $value . " ")
            }

         }

#   end foreach i
      }
      :set line ("set " . [:tostr $line] . "\n")

#   if contents + line is over filesize limit, write out to file
      :if (([:len [:tostr $contents]] + [:len [:tostr $line]]) > 4096) do={
         /system identity export file=($filename . $fileindex)
#      wait for file to appear (this is needed)
         :while ([:len [/file find name=($filename . $fileindex . ".rsc")]] = 0) do={}
         /file set ($filename . $fileindex . ".rsc") contents=[:tostr $contents]
         :set contents "/interface wireless\n"
         :set fileindex ($fileindex + 1)
      }

#   append line to contents
      :set contents ([:tostr $contents] . [:tostr $line])
   }

# write remaining contents
   :if ([:len $contents] > 0) do={
      /system identity export file=($filename . $fileindex)
#   wait for file to appear (this is needed)
      :while ([:len [/file find name=($filename . $fileindex . ".rsc")]] = 0) do={}
      /file set ($filename . $fileindex . ".rsc") contents=[:tostr $contents]
   }

# /interface wireless
}

I know this may look complex, but it shouldn’t be too hard once you dig into it. Also, this script could be adapted for any settings within RouterOS that you want to output to a file, making a custom script from RouterOS’s settings.

Hope this helps. If you have any problems, or suggestions on how to improve it, let me know.

Hallo dssmiktik,

thank you for your script. Its an interesting way to export settings and I think its a good way to filter parts which I don’t need to export.

I think that will help me a lot! Thank you!! :slight_smile:

there is limitation on how large strings can be assigned to variable, that limit is 4KB 4096 bytes.

janisk,

What is the allowed length of a comment field (in bytes)? Maybe I should check for variable length would exceed 4096, then dynamically allocate the next available variable (var1, var2, var3, etc…, just like I have with files?

Unfortunately, the RouterOS scripting language and limited (no shell/bash) means a lot of work has to be done for simple tasks, like export a custom config, or import a plain text file of IP addresses that exceed 4096 bytes, which is a very small amount.

What would you suggest I change on the above script to make it fail-safe?


Thanks

comment field can be longer than string you can save in variable.

Someone please test this code:

:global vartest
:foreach i in="11111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222223333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111113333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111113333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111113333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111113333333331111111111111111222222222222222222333333333333333333333333331111111111111133333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111133333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111133333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111133333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111133333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111133333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111133333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111133333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111113333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111113333333331111111111111111222222222222222222333333333333333333333333331111222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111133333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111333333333111111111111111122222222222222222233333333333333333333333333111122222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111113333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111133333333311111111111111112222222222222222223333333333333333333333333311112222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111111222222222222222222333333333333333333333333331111111111111113333333331111111111111111222222222222222222333333333333333333333333331111222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111112222222222222222223333333333333333333333333311111111111111133333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333111111111111111222222222222233333333333333333333333333111111111111111122222222222222222233333333333333333333333333ABCDEFG" do={
   :put [:len [:tostr $i]]
   :set vartest [:pick [:tostr $i] 0 10320]
   :put [:len [:tostr $vartest]]
   :put [:pick [:tostr $vartest] ([:len [:tostr $vartest]] - 7) [:len [:tostr $vartest]]]
}

Does it return a global variable with length greater than 4096? It did for me. Why does this happen?

Hallo,

yes it seems to work. My ouput of the terminal with version 3.30 is ..

10320
10320
ABCDEFG

It also seem to work with a local variable! Nice! :slight_smile:

correction. that 4KB limitation is there if you want to save and read from file, if you want to set or read variables and it is not connected with files, only router memory is your limiting factor. (as it seems now - long, from C)

Janisk,

  1. Is this true for all versions of RouterOS (a variable can be larger then 4096 bytes if not read from file), or is this only in a new/specific version(s)?

I was always under the assumption that all variables (being read from file & set in terminal & set in script) had a limitation of 4096 bytes?

  1. Also, is there a way of testing the memory before setting / appending a variable? For example, if I have a very large filter address-list, and I want to set it’s contents to a variable, how would I check that I have enough memory to store this contents?
  1. always variables could hold more data than 4KB. Only limitation is how much you can write/read to file.

  2. you can assign very very large amounts of memory. More free memory you have higher you can go. That is, if you increase the variable you have to be aware of memory allocation mechanisms. If you just using large value, you do not have to worry about that at all.

This information about limitation of contents should be write at http://wiki.mikrotik.com/wiki/Manual:System/Log and on changelog at download page.

put [file get file.txt contents];
and
put [file find where name=“file.txt” contents~“query”=true;
is not working if the file.txt is larger then 4KB.

Please write add this to WIKI !

Someone can help to understand how to catch snmp-walk outout?
when I try to set a variable it is Always type of null.

Files up to 64KB can read into an array.

http://forum.mikrotik.com/t/address-lists-downloader-dshield-spamhaus-drop-edrop-etc/133640/26