CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Would like some advice on the best way to approach this issue

 



Alpinesun
New User

Jan 31, 2011, 1:57 PM

Post #1 of 5 (1047 views)
Would like some advice on the best way to approach this issue Can't Post

Hi all!

First post on the website and very new to perl scripting so go easy on me! :)

So I'm currently writing my first script to help create some text files for some Cisco device configurations. What I've achieved so far is to ask the user some questions by inputting using <STDIN> and then based off those inputs the script opens a file with a base configuration template, takes the user imputs and then writes it to a new and completed configuration file. This work perfectly if you want to create one device config at a time.

What I would like to do is create multiple configurations at the same time, example:

I would like to create two or more router configurations for a new site. Each router has a unique router name that the user inputs in using a <STDIN> but how do I take more than one name and then run the copy and replace function on multiple entries? I'm guessing <STDIN> isn't the best way to achieve this?

What I would like to do is ask the user for multiple device number entries (E.G 001 002 003) and then run my find and replace routine.

Here is my script so far:

#!/usr/bin/perl

system(clear);

print "Is this for NetA or NetB? (Enter a or b): ";
chomp ($net = <STDIN>);
print "Please enter the site number: ";
chomp ($site = <STDIN>);
print "Please enter the device number (001 etc) ";
chomp ($switchnum = <STDIN>);

#This takes in all the info required to generate a device name

$file = '/home/alpy/template.txt';
#This is where my master template is stored. It's a basic Cisco config file where I've substituted some key elements for keywords on which I go and find and replace with the users imput

open my $in, '<', $file or die "Can't read old file: $!";
open my $out, '>', "0$site-net$net-xx$switchnum.txt" or die "Can't write new file: $!";

print $out "### This is a config for device 0$site-net$net-xx$switchnum ### \n";

while( <$in> )
{
print "Creating an net$net access switch configuration on site $site with a switch number of $switchnum (0$site-net$net-xx$switchnum)\n";
s/\b(swname)\b/0$site-net$net-xx$switchnum/g;
print $out $_;
}
close $out;

Any help would be fantastic! I'm just not sure which perl function to use so I can have multiple user inputed data the while script uses and generates the new files.

Thanks in advance


Karazam
User

Feb 1, 2011, 10:45 AM

Post #2 of 5 (1036 views)
Re: [Alpinesun] Would like some advice on the best way to approach this issue [In reply to] Can't Post

So basically I think you want something like


Code
print "Please enter the device number (001 etc) "; 
my @switchnums = split /\s+/, <>;

for my $x ( @switchnums ) { # see if it works
print "$x\n";
}


(<> has the same meaning as <STDIN>)
One quirk with this is, that if the user puts blank space before the first argument, you'll wind up with an empty first element in the array. To fix this, you can


Code
print "Please enter the device number (001 etc) "; 
my $line = <>;
$line =~ s/^\s+|\s+$//g; # remove leading and trailing white space
my @switchnums = split /\s+/, $line;

for my $x ( @switchnums ) { # see if it works
print "$x\n";
}


Of course you might want to check that the input is sane in other ways before proceeding, but that's another matter.

Hope this helps. Smile


(This post was edited by Karazam on Feb 1, 2011, 10:48 AM)


Alpinesun
New User

Feb 1, 2011, 11:58 AM

Post #3 of 5 (1033 views)
Re: [Karazam] Would like some advice on the best way to approach this issue [In reply to] Can't Post

Karazam,

Thank you for replying! I'll give it ago and get back to you ASAP! :)

Alpy


Alpinesun
New User

Feb 28, 2011, 2:22 AM

Post #4 of 5 (962 views)
Post deleted by Alpinesun [In reply to]

 


Alpinesun
New User

Feb 28, 2011, 2:51 AM

Post #5 of 5 (961 views)
Re: [Alpinesun] Would like some advice on the best way to approach this issue [In reply to] Can't Post

Ignore me! I've worked it out! ;)

Thank you

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives