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

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.

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

Replies are listed 'Best First'.
Re^2: How do I use a template, csv file and script to generate multiple switch configurations
by Anonymous Monk on Apr 16, 2013 at 01:11 UTC

    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!

        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.

        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.