Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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 ( [id://1028791]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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

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!

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

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

    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; }

        Thank you! Thank you! Thank you! That worked. I really appreciate the help.

        Its perfectly working. Thanks a lot..
Re^4: How do I use a template, csv file and script to generate multiple switch configurations
by prasath1017 (Initiate) on Jun 26, 2014 at 22:33 UTC
    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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1028791]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found