Simple Perl Script to Produce a Configuration from a Template

Everyone has their own method of leveraging configuration templates. Engineers utilize Network Management Systems, text files with find & replace functionality, and homegrown scripts. There are even some deranged individuals that use word processor form letter / mail merge functionality.

Personally, I tend to use the Perl script that I’m about to share with you. While the script can work on multiple platforms, I will just provide instructions to set it up on a Linux box. It’s not a perfect solution, so take it easy on me. Linux and Perl setup are outside the scope of this blog.

Here is a quick rundown of how it works:

  • makemyconfig – Perl Script that is run to process a template text file, refers to a “defaults” file that has default values for some variables, prompts end-user for desired value of variables, and outputs to the desired configuration file.
  • makemyconfig.conf – This is the file that contains default values that can be used for the referenced template. For example, %user%=jsmith for the local username. This file is called directly from the Perl script – it isn’t passed as an input argument. It will be re-used by the Perl script regardless of what template is being used.
  • AccessSwitch.template – This is the template file that has the desired configuration, but denotes variables as text enclosed with “%” signs.
  • MyConfig.txt – This is the output file containing the configuration that I want to FTP/TFTP/paste to my network device.

The script is basically run with the syntax, “makemyconfig templatefile outputconfigfile.”

While this may seem much more complex than a text file with find and replace, I have found it to be very simple to use – especially when the outputted configuration file resides in a FTP directory that I can leverage to deploy the configuration to the target device.

If I haven’t bored you already and you’re interested in giving it a shot, here are the steps to get it running.

1. Download the Perl script and place it in /usr/local/bin

Download the makemyconfig Perl script from my Google Drive and copy it into /usr/local/bin on your Linux host. Unfortunately, you will probably have issues accessing it with wget and curl, so if you are running a GUI-less Linux server, it may be easier to download off-box and then FTP/SFTP/etc.

Note: I encourage everyone to review the script before running it. It is a horrible habit to run untested third-party scripts on a production management station. 

2. Make the script executable

Use the “change mode” command (chmod) to make the Perl script executable:

root@myhost:~$ cd /usr/local/bin
root@myhost:~$ chmod a+x makemyconfig

3. Create the “defaults” file in /usr/local/etc

Create a defaults file in /usr/local/etc/ (that exact path is referenced in the above Perl script, so for simplicity sake, I would just place it there). Use the syntax %variable%=value.

Change directory to /usr/local/etc

root@myhost:~$ cd /usr/local/etc

Create the file “makemyconfig.conf”

root@myhost:~$ touch makemyconfig.conf

Modify the file “makemyconfig.conf” (I’m not going to argue Vi/Vim/eMacs – nano is easier for me to reference here)

root@myhost:~$ nano makemyconfig.conf

Once nano opens the empty “makemyconfig.conf” file, add the following default values:

%user%=jsmith
%password%=MyPassword
%enablesecret%=SuperSecretPassword

Hit “Ctrl-x” to exit
Type “y” and hit enter to save
Hit “Enter” to save to the name “makemyconfig.conf”

4. Create your template file

At this point, you must decide on a directory where you want to store your generated configurations. In my shared work environment, we typically run the script from our respective /home directories to make it easier to “own” configurations. Change your path to that desired directory.

root@myhost:~$ cd /home/myusername

Create your template configuration in this location. Similar to the defaults file, place “%” around all of your dynamic variables. For instance, if “enablesecret” is your variable, type “enable secret %enablesecret%” as the applicable Cisco configuration command.

Create configuration file

root@myhost:~$ touch AccessSwitch.template

Edit configuration file

root@myhost:~$ nano AccessSwitch.template

Type your configuration into the editor:
hostname %hostname%
ip domain-name packetpushers.local
username %user% priv 15 secret %password%
enable secret %enablesecret%

Hit” Ctrl-x” to exit
Type “y” and hit enter to save
Hit enter to save to the name “AccessSwitch.template”

5. Run the script

At this point you can run the script referencing the template file that you just created and whatever name you want your output configuration to have. Note that the following command is wrapped – it should be on one line.

root@myhost:~$ /usr/local/bin/makemyconfig AccessSwitch.template MyConfig.txt

In our example, you will be interactively prompted with the following:

%enablesecret% (SuperSecretPass):
%hostname% (): 3750-hi-bldg3-01
%password% (MyPass):
%user% (jsmith):

You’ll notice that the entries that were in the “defaults” file were automatically provided as an option in paranthesis. If you just hit “enter” when prompted, the default value will be used, whereas you can override it by typing in a different value. Using default values is great in some cases. However, if you have a single site that uses all of the same passwords, SNMP strings, etc., you might as well have an empty default file and hard-code those values in your template.

After the script finishes, you should have a new file in your directory – in this case MyConfig.txt, that contains all of the parsed configuration:

hostname 3750-hi-bldg3-01
ip domain-name packetpushers.local
username jsmith priv 15 secret MyPassword
enable secret SuperSecretPass

Due to an assortment of issues, you may wind up with myriad templates that you may use together to build device configurations (general configuration, hardware specific, OS specific, customer specific, Place-In-Network, etc.).

I hope at least one engineer out there finds this script to be valuable.

InfiniteMonkey

InfiniteMonkey

InfiniteMonkey

Latest posts by InfiniteMonkey (see all)

  • Phil Sykes

    ‘Everyone has their own method of leveraging configuration templates’ – I think that’s a pretty good summary of the problem we face attempting to automate things. You missed out MS Excel as a config generation tool, which is a favourite of mine.

    • Will

      Do share

      • Infinite Monkey

        A simple way could be to use the concatenate function. For instance, use column “A” as the labels for the values you need to enter. Use column “B” for the values that you want to enter to build the dynamic config. Use column “C” to create the dynamic configuration. For instance: A1 = “Username”, A2 = “Password”, B1 = “jsmith”, B2 = “MyPassword”, C1 = “=concatenate(“username “,A1,” secret “,A2).” Column “B” in this case would be the data that is changed every time you create a new config. You would then copy all of your config from column “C”.

7ads6x98y