Community discussions

MikroTik App
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

save export to variable

Sun Apr 11, 2010 5:33 pm

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.
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

Re: save export to variable

Sun Apr 11, 2010 6:59 pm

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.
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

Re: save export to variable

Sun Apr 11, 2010 8:48 pm

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?
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: save export to variable

Mon Apr 12, 2010 2:21 am

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.
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

Re: save export to variable

Tue Apr 13, 2010 10:26 am

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!! :-)
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: save export to variable

Tue Apr 13, 2010 11:30 am

there is limitation on how large strings can be assigned to variable, that limit is 4KB 4096 bytes.
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: save export to variable

Tue Apr 13, 2010 11:50 am

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
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: save export to variable

Wed Apr 14, 2010 8:31 am

comment field can be longer than string you can save in variable.
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: save export to variable

Wed Apr 14, 2010 10:01 am

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?
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

Re: save export to variable

Wed Apr 14, 2010 11:16 am

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! :-)
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: save export to variable

Wed Apr 14, 2010 3:19 pm

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)
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: save export to variable

Thu Apr 15, 2010 2:54 am

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?

2) 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?
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: save export to variable

Thu Apr 15, 2010 8:27 am

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.
 
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: save export to variable

Mon Mar 07, 2016 8:20 pm

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 !
 
maxfava
Member Candidate
Member Candidate
Posts: 224
Joined: Mon Oct 17, 2005 12:30 am

Re: save export to variable

Sun Jan 26, 2020 6:56 pm

Someone can help to understand how to catch snmp-walk outout?
when I try to set a variable it is Always type of null.
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: save export to variable

Sat Sep 12, 2020 8:41 pm

Files up to 64KB can read into an array.

viewtopic.php?f=9&t=152632&p=759468&hilit=cidr#p759468

Who is online

Users browsing this forum: Google [Bot], loloski and 26 guests