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.


In reply to How do I use a template, csv file and script to generate multiple switch configurations by hvh2000

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.