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.