Creating and using Dynamic Config Scripts

If your work or play is involved in dealing with large, complex data networks, and you have one or more standard components that need to be replicated to multiple individual components, then you may find yourself repeating a set of configuration steps every time you need to deploy a new device to the network.

Fortunately, Mikrotik RouterOS provides a means to save existing router configurations and to quickly and easily build dynamic configuration scripts to automate this kind of roll-out process.

The best way to begin is to use a config export file:

 

Click ‘new terminal’ in winbox,

enter ‘/export file=config’ at the RouterOS shell,

look in ‘files’ under winbox,

drag the file entry called ‘config.rsc’ to your desktop or other convenient PC filesystem location.

 

The shell command actually defines the filename – so you can use ‘/export file=myconf’ to create a file called ‘myconf.rsc’

 

The .rsc is just a text file – open it up in any text editor – which contains a full list of shell commands required to replicate the current configuration of your router.


You can copy-all the contents of that file, and paste into a ‘new terminal’ (right click inside the routerOS shell and choose ‘paste’) to get a complete duplicate of your original router config.

 

CAREFUL though!  You will duplicate the router configuration in every way INCLUDING the interface MAC addresses!  Therefore, if you are making wireless CPE kits for example, intended to all use the same access point, duplicating MAC addresses of the original wireless interface is obviously going to get you in to some trouble! ;-)

Therefore, before applying any config to a new router, I strongly recommend that you first go through the config with your text editor and at least remove all references to mac addresses.  Remember that any command or attribute in the config file that is omotted simply implies that the default setting will be accepted.

 

Therefore, you can also make your config file a lot simpler by also removing any commands that you are happy to keep as the default setting.

 

You can also make any adjustments to the config before applying – for example you may want to change some typical settings like WPA encrypttion or admin passwords, SSID names, router ID names and so on.

 

When you are happy with your config file, you can save it to your config library for later use or reference.

 

You can also use the powerful routerOS scripting language to make dynamic configuration scripts.  One example is how we make a pptp client to use the ether1 mac address as the login username:

 

In routerOS, to retrieve the value of the mac address of ether1, we can use this command:

 

/interface ethernet get ether1 mac-address

 

So to add a pptp client with that string as username, we can do:

 

/interface pptp-client add add-default-route=no allow=pap,chap,mschap1,mschap2 \

    comment="pptp client added by setup script" connect-to=pptp.mynas.com disabled=no max-mru=1460 \

    max-mtu=1460 mrru=disabled name=pptp-out1 password=duxman profile=default-encryption \

    user=[/interface ethernet get ether1 mac-address]

 

You can also make a dynamic configuration using variables defined at the beginning of your script for easy customization.  In the following example, you might want to set the wireless SSID of a device using a standard config.  Rather than hunt through the config file for the ssid value in the wireless interface property, you can define it at the beginning of the file:

 

# sample config.rsc

# change the following value to use as SSID

 

:global ssid “MySSID”

 

# now refer to the variable with ‘$’ prepend:

 

/interface wireless set ssid=$ssid

 

Anywhere that you want to set common variables, you can use this method to put the value definitions in a convenient place at the top of your file.

 

Sometimes, you may find that your scripts fail part way through applying them.  For example, if your configuration script includes a firewall rule half way through that effectively cuts you off from the router on the port you are connected to, or if the script creates a bridge or reassigns an IP address that you may need for access to the device.  In this case, you can first paste your config file into a system script, and then execute the entire script at once:

In winbox, click on ‘system -> scripts’ and then click ‘+’.  Give the script some name (if you want to, like “initial-config” then paste your entire config file into the content field.  Save and close the dialog and then click ‘run’ to execute.

 

You can review more routerOS scripting info at the official Wiki site: http://wiki.mikrotik.com/wiki/Manual:Scripting

 

Or feel free to contact us if you have further questions! :-)