Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by hvh2000 (Initiate)
on Apr 15, 2013 at 19:43 UTC ( #1028756=perlquestion: print w/ replies, xml ) Need Help??
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.

Comment on How do I use a template, csv file and script to generate multiple switch configurations
Select or Download Code
Re: How do I use a template, csv file and script to generate multiple switch configurations
by hdb (Prior) on Apr 15, 2013 at 19:52 UTC

    At a glance, it looks like you need to replace &lt; with < and &gt; with > to make it work. Ie.

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

    Some comments:

    The line $ofile_name = $name . ".txt"; uses a variable $name which is not defined. Should this be $hostname probably?

    In your file with the CSV variables change the first line to # ip,hostname,location and add a line to filter out comment lines starting with # like this:

    while(<>) { next if /^#/; ...

    I have not run the code as the template file is not present, so there might be glitches in my code.

      Hello, The config template looks like this:

      conf t no ip http server no ip http secure-server service timestamps debug datetime localtime show-timezone service timestamps log datetime localtime show-timezone service password-encryption service compress-config ! hostname ##hostname## ! ! vlan internal allocation policy ascending ! ! interface Vlan1 ip address ##ip## 255.255.255.0 no ip redirects ! snmp-server location ##location## snmp-server contact x1208! ! ! line con 0 logging sync login line vty 0 15 logging sync exec-timeout 360 0 privilege level 15 transport input telnet ssh ntp server 152.38.254.2 prefer banner motd ^ ****************** Warning! Warning! Warning! *********************** +* Unauthorized access is a violation of the law. This service may be monitored for administrative and security reasons +. Such monitoring may be reported to law enforcement agencies. By proceeding, you consent to this monitoring. ##hostname## ****************** Warning! Warning! Warning! *********************** +* ^ end wr me

        Also, I seem to be having a bit of an issue with the perl configuration, or with the way the script is initializing, I get a bunch of odd errors like this:

        hvanheerden@NWN-MNC-HVANHEE ~ $ perl makeconfig.pl > configtemplate.conf.txt Global symbol "$template_file_name" requires explicit package name at +makeconfig.pl line 7. Global symbol "$location" requires explicit package name at makeconfig +.pl line 10.

Re: How do I use a template, csv file and script to generate multiple switch configurations
by hvh2000 (Initiate) on Apr 15, 2013 at 21:53 UTC

    Hello: I have modified the script to look like this:

    #!/usr/bin/perl use strict; use warnings; use autodie; my $template_file_name="/home/hvanheerden/configtemplate.txt"; my ($ip, $hostname, $location) = split; my $ofile_name = $hostname . ".txt"; while(<>) { next if /^#/; ($ip, $hostname, $location) = split (/,/); open(TFILE, "< $template_file_name") || die "config template file $ +template_file_name: $!\n"; $ofile_name = $hostname . ".txt"; open(OFILE, "> /home/hvanheerden/$ofile_name") || die "output confi +g file $ofile_name: $!\n"; while (<TFILE>) { s/##location##/$location/; s/##hostname##/$hostname/; s/##ip##/$ip/; printf OFILE $_; } }
    Thanks for those that have provided feedback, this has been a learning experience so far. Please continue to advise, as I'm still not quite there yet. I am observing some errors, and the script seems to be deleting the content of my template file.

      Hi

      >> I am observing some errors,

      Always give full descriptions of any errors please, including error messages.

      This will help us help you.

      J.C.

        Ah, never have truer words been spoken! You'd think I'd've learned that by now... :) Here are the errors I'm seeing:

        $ perl makeconfig.pl < host-ip.csv syntax error at makeconfig.pl line 14, near ") {" syntax error at makeconfig.pl line 34, near "}" Execution of makeconfig.pl aborted due to compilation errors.

        And the script as I currently have it. I'm assuming that I've gone overboard with defining variables, among other foibles.

        #!/usr/bin/perl use strict; use warnings; use autodie; my $template_file_name="/home/hvanheerden/configtemplate.txt"; my ($ip, $hostname, $location) = split; my $ofile_name=$hostname . ".txt"; my $TFILE="$template_file_name" while(<>) { next if /^#/; ($ip, $hostname, $location) = split (/,/); open(TFILE, "< $template_file_name") || die "config template file $ +template_file_name: $!\n"; $ofile_name = $hostname . ".txt"; open(OFILE, "> /home/hvanheerden/$ofile_name") || die "output confi +g file $ofile_name: $!\n"; while (<TFILE>) { s/##location##/$location/; s/##hostname##/$hostname/; s/##ip##/$ip/; printf OFILE $_; } }

        Thanks for the help. Err... Please continue to help! Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028756]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-12-29 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls