Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by hdb (Monsignor)
on Apr 15, 2013 at 19:52 UTC ( [id://1028759]=note: print w/replies, xml ) Need Help??


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

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.

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

    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.

        You need to declare your variables before using them, using the "my" function. Change the relevant line to:

        my $template_file_name = "configtemplate.txt";

        Same things for other variables displaying the same error message.For example:

        my ($location, $hostname, $ip) = split ...

        You also need to call it like perl makeconfig.pl < file_with_csv_variables.csv, otherwise you overwrite your template file.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-29 11:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found