http://www.perlmonks.org?node_id=1028756

hvh2000 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! I'm a noob to Perl, though I see a lot of potential here to simplify my life (and others'). I have a situation at work that I need your help with. I would like to use a perl script to generate a number of configuration files (13 or so) for Cisco switches. I have a template file, which contains the configurations, plus some variables. I also have a CSV file which contains the hostname, ip address, and SNMP location variables I wish to assign. I'm running the script on a perl implementation on cygwin, though I also could run it on windows, or dust off my linux vm and do it there. The script is in part from an o'reily cisco ios cookbook, as follows:

#!/usr/bin/perl use strict; use warnings; use autodie; $template_file_name="configtemplate.txt"; while(<>) { ($location, $hostname, $ip) = split (/,/); open(TFILE, "< $template_file_name") || die "config template fil +e $template_file_name: $!\n"; $ofile_name = $name . ".txt"; open(OFILE, "> $ofile_name") || die "output config file $ofile_n +ame: $!\n"; while (<TFILE>) { s/##location##/$location/; s/##hostname##/$name/; s/##ip##/$ip/; printf OFILE $_; } }

The CSV variables file looks like this:

ip,hostname,location 172.30.240.1,CUSOM-176-SWI-001,1st Fl N IDF 172.30.240.2,CUSOM-176-SWI-002,1st Fl N IDF

and the configuration template has a number of text lines, 3 of which contain the above referenced variables "ip", "hostname" and "location" on separate lines, along with a bunch of other stuff that is static and the same across devices. Your help is much appreciated.