Community discussions

MikroTik App
 
16mc
newbie
Topic Author
Posts: 31
Joined: Tue Dec 15, 2009 8:55 pm

Variables from a file

Fri Jan 08, 2010 9:17 pm

I have done a few basic scripts and I'm now embarking on a bigger project.

I'd like to update several hundred CPE configuration in the field. But in order to not make a mess of things I need to save the IP info and a few other things like the radio name or RB identity before killing config and reloading the new one.

Saving what I need is no problem using either export or print, and sending it to a file but how do I get the information back into the newly configured CPE? can the variable be taken from a text file on the cpe/router?

Is this the best approach? and If some one has done this and want to share that would be great.

Thanks
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

Re: Variables from a file

Fri Jan 08, 2010 9:51 pm

Assuming the a text file named example.txt in the root directory of the file system with the following content:
ip=1.2.3.4
other=test
The below script will extract the value of the variable named 'ip'. It's rather verbose and could be shortened.
# slurp in file contents into variable
:local fileContents [/file get [/file find name=example.txt] contents];
# set a search variable that contains the variable name for the value to be extracted
:local variableName "ip";
# find the character offset for that variable name and add the length of the variable plus one for the = character, offset now points to character value
:local start [:find ($variableName . "=") $fileContents];
:set start ($start + [:len $variableName] + 1);
# find the character offset for the next newline as that indicates the variable value end
:local end [:find $fileContents "\r\n" $start];
# extract that substring
:local value [:pick $fileContents $start $end];
# print it
:put ("The value of '$variableName' is: " . $value);
Hope that helps.

I'd probably rather make use of the auto-execute on files named ending in .auto.rsc, though. Have an offsite system generate all the configuration files after receiving the static information via SNMP/script/whatever, then upload the configurations.
 
16mc
newbie
Topic Author
Posts: 31
Joined: Tue Dec 15, 2009 8:55 pm

Re: Variables from a file

Mon Jan 11, 2010 4:00 pm

Thanks for the details, I'll work it in to what I have.

Thanks again.