in reply to Re: How do I use a template, csv file and script to generate multiple switch configurations
in thread How do I use a template, csv file and script to generate multiple switch configurations

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.

  • Comment on Re^2: How do I use a template, csv file and script to generate multiple switch configurations

Replies are listed 'Best First'.
Re^3: How do I use a template, csv file and script to generate multiple switch configurations
by hvh2000 (Initiate) on Apr 16, 2013 at 01:19 UTC

    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!

      I can also add that my .csv now 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 my config template looks something like this:
      hostname ##hostname## interface Vlan1 ip address ##ip## 255.255.255.0 snmp-server location ##location##
      or at least, those are the lines from it that contain the variables.

        A missing ; and a bit of re-ordering was all to be done. Now it should do the job but a lot of it is implicit and not obvious to the Perl starter. So be careful when expanding on it...

        #!/usr/bin/perl use strict; use warnings; use autodie; # usage: perl makeconfig.pl < host-ip.csv my $template_file_name="configtemplate.txt"; while(<>) { next if /^#/; chomp; my ($ip, $hostname, $location) = split (/,/); my $ofile_name=$hostname . ".txt"; open(TFILE, "< $template_file_name") || die "config template f +ile $template_file_name: $!\n"; $ofile_name = $hostname . ".txt"; open(OFILE, "> $ofile_name") || die "output config file $ofile +_name: $!\n"; while (<TFILE>) { s/##location##/$location/; s/##hostname##/$hostname/; s/##ip##/$ip/; printf OFILE $_; } close OFILE; close TFILE; }
      Hi, Getting the below error: >perl test.pl syntax error at test.pl line 16, near ") next " syntax error at test.pl line 28, near ") s/##location##/$location/" Execution of test.pl aborted due to compilation errors.

        I don't get that error on line 16 when testing the file. It helps us help you much better if you post the exact error messages and also the exact code where you encounter the error.

        The problem with the code as given is just a small syntax error caused by a forgotten semicolon in line 10. Maybe you want to learn about how to debug such trivial errors?