Community discussions

MikroTik App
 
MikeRoTik
just joined
Topic Author
Posts: 17
Joined: Wed Jul 08, 2020 4:47 am

Generate script from another script

Thu Oct 15, 2020 6:03 am

Is it possible to generate a script from a script with variable substitution, something like this:

:global myVar1 "ABC"
:global myVar2 "DEF"

/system script
add dont-require-permissions=no name=myScript owner=admin policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="#\
    \n:global scriptVar1 $myVar1\r\
    \n:global scriptVar2 $myVar2\r\
    \n\r\
    \n}"

Instead of the generated script containing $myVar1, I want it to contain "ABC"
 
User avatar
eworm
Long time Member
Long time Member
Posts: 678
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Generate script from another script

Thu Oct 15, 2020 11:23 pm

Sure it does. In this example you should add quotes I guess...
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts
 
MikeRoTik
just joined
Topic Author
Posts: 17
Joined: Wed Jul 08, 2020 4:47 am

Re: Generate script from another script  [SOLVED]

Fri Oct 16, 2020 5:35 pm

Yes it does when working with variables. String arrays and key, value arrays are another story. Ultimately I was able to solve the problem. Here's how:
# Previously defined/assigned string array
:global FOinterfaces

# Previously defined/assigned key, value array
:global FOpingHosts

#Convert the string array to a string variable, need to insert the quotes
:global FOSinterfaces ""
:foreach i in=$FOinterfaces do={
	:if ([:len $FOSinterfaces] = 0) do={ :set FOSinterfaces ("\"".$i."\"") } else={ :set FOSinterfaces ($FOSinterfaces.";\"".$i."\"") }
}

#Convert the key, value array to a string. Need to quote the value
:global FOSpingHosts ""
:foreach k,v in=$FOpingHosts do={
	:if ([:len $FOSpingHosts] = 0) do={ :set FOSpingHosts ($k."=\"".$v."\"") } else={ :set FOSpingHosts ($FOSpingHosts.";".$k."=\"".$v."\"") }
}

/system script
add dont-require-permissions=no name=success owner=admin policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="
    \n:global FLinterfaces {$FOSinterfaces}\r\
    \n:global FLpingHosts {$FOSpingHosts}\r\
    ...............
Here is the generated script:
:global FLinterfaces {"vlan20";"ether2";"lte1"}
:global FLpingHosts {ether2="208.67.220.220";lte1="64.6.64.6";vlan20="1.0.0.1"}

Who is online

Users browsing this forum: No registered users and 22 guests