LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-12-2008, 11:42 PM   #1
sal_paradise42
Member
 
Registered: Jul 2003
Location: Utah
Distribution: Gentoo FreeBSD 5.4
Posts: 150

Rep: Reputation: 16
help with a little perl script and assigning variables


hello,
I have the following input
Quote:
class-map match-all VOIP-Control
match access-group 101
match access-group 103
class-map match-all VOIP-RTP
match access-group 100
What I am trying to do is place each match to the parent class-map so I like to end up with the following:
Quote:
VOIP-Control access-group 101
VOIP-Control access-group 103
VOIP-RTP access-group 100
and "VOIP-Control" and "access-group \d+" are two different variables, eventually I will like to send this to a mysql database.
I want to make it so there can be multiple matching statements, but the parent is always accurate.
I tried a few things, but I am stuck trying to make that work.

Right now all I have is a blank foreach loop:
Code:
foreach my $class (@dump_class) {
        print $class;
}
with the first text as the output.
Any help will be great.
 
Old 10-13-2008, 02:00 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,246

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
Code:
if( $class /^class_map/ )
{
    $class_name = split(/ /, $class)[2];
}
else
{
    @group_val = split(/ /, $class)[1,2];
    print "$class_name @group-val\n";
}
NB: untested(!)
 
Old 10-13-2008, 06:35 PM   #3
sal_paradise42
Member
 
Registered: Jul 2003
Location: Utah
Distribution: Gentoo FreeBSD 5.4
Posts: 150

Original Poster
Rep: Reputation: 16
doesn't seem to work. I am posting my entire script as there is a few things I like to point out (is ugly)
Code:
1 #!/usr/bin/perl
2 
3 #open directory with latest configuration and dump individual contents into database
4 #
5 
6 my @dir_contents;
7 my $diropen = "/var/random/test/";
8 
9 opendir(DIR,$diropen) || die("Cannot open directory !\n");
10 @dir_contents= readdir(DIR);
11 closedir(DIR);
12 my $nodes_id = 0;
13 foreach my $file (@dir_contents)
14 {
15 my ($hostname,@dump_class,$class_name,$class_match,$policy_name,$class_name,$class_action,@dump_int,$interface_name,$ip_address,$ip_vrf_name,$interface_dlci,@dump_ospf,$pid_number,$vrf_member,$redistribution,$net_statement,@dump_static,$vrf_name,$static_route,$mask,$gateway,@dump_acl,@acl,$acl_numb,$acl_statement,@dump_snmp,$statement,$access_class);
16      if(!(($file eq ".") || ($file eq "..")))
17      {
18              open(FILE,"/var/random/test/$file") or die "couldn't open $conf $!\n";
19              local $/ = "!";
20              my @configs = <FILE>;
21                      $hostname = $file;
22                 foreach my $conf (@configs) {
23                      if ($conf =~ /(class-map.*)!/s) {
24                           push @dump_class,$1;
25                }
26                      if ($conf =~ /\n(interface\s.*)\n!/s) {
27                       push @dump_int,$1;
28               }
29                      if ($conf =~ /\n(router ospf\s.*)\n!/s) {
30                              push @dump_ospf,$1;
31                      }
32                      if ($conf =~ /(ip route (?:vrf )?(?:\S+ )?(?:\d{1,3}\.){3}\d{1,3} (?:\d{1,3}\.){3}\d{1,3} (?:\d{1,3}\.){3}\d{1,3}.*)/s) {
33                              push @dump_static,$1;
34                      }
35                      if ($conf =~ /access-list \d+ .*/s) {
36                              @acl = split("\n",$conf);
37                              foreach my $line (@acl) {
38                                      if ($line =~ /^(access-list .*)$/) {
39                                      push @dump_acl,$1;
40                              }
41                                      if ($line =~ /^(snmp-server .*)$/) {
42                                              push @dump_snmp,$1;
43                                      }
44                      }
45                 }
46                     if ($conf =~ /line vty 0 4.*(access-class \d+ in)/s) {
47                             $access_class  = $1;
48                    }
49                     }
50                     $nodes_id++;
51                     #start defining the arrays extracted from the configurations here
52              foreach my $class (@dump_class) {
53                      if( $class =~ /^class_map/ )
54                          {
55                                  $class_name = split(/ /, $class);#[2];
56                          }
57                          else
58                          {
59                          @group_val = split(/ /, $class); #[1,2];
60                          print "$class_name @group_val\n";
61                          }
62                         }
63      }
64 }
In line 19 I am changing the record separator to "!" due to the fact that I need to do multiline matching and all the relevant lines are between "!" in the text file I am reading. I think is causing problems later in the match, hence the problem I am running into trying to align the output correctly. I applied the recommended configuration into lines 53 to 60 (brackets after the line do not work, not sure if I am just a big noobcake and was suppose to change something, they are commented out)
The output is just the entire line:
Quote:
class-map match-all VOIP-Control
match access-group 101
match access-group 103
class-map match-all VOIP-RTP
match access-group 100
Is it because of the record separator? if so how do I fix that?
TIA
 
Old 10-13-2008, 07:52 PM   #4
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,341

Rep: Reputation: 73
/^class_map/ shouldn't be /^class-map/ ?
 
Old 10-13-2008, 08:10 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,246

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
Yep, that was a typo by me. I always use vars 'like_this', never 'like-this', so I get into the habit of underscores...
Sorry...
 
Old 10-14-2008, 05:06 PM   #6
sal_paradise42
Member
 
Registered: Jul 2003
Location: Utah
Distribution: Gentoo FreeBSD 5.4
Posts: 150

Original Poster
Rep: Reputation: 16
Ok must be something elementary that I am missing here. This is what I used:
Quote:
foreach my $class (@dump_class2) {
if( $class =~ /^class-map/ )
{
my @test_arr;
@test_arr = split(" ", $class );
#print "$test_arr[2]\n";
}
else
{
@group_val = split(/ /, $class ); #[1,2];
print "$test_arr[2] $group_val[2] $group_val[3]\n";
#print "$test_arr[2] $group_val[1,2]\n";
}
}
but it just gives me:
Quote:
access-group 101
access-group 103
access-group 100
If I put the brackets as in your original suggestion, it just breaks due to syntax errors. So I apologize if I missing something obvious.
thanks
 
Old 10-14-2008, 05:18 PM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,341

Rep: Reputation: 73
Try uncomment (and replace the \n with a space):
#print "$test_arr[2]\n";

eg, make it like:
print "$test_arr[2] . ' ';

[edit]
Sorry, I overlooked your second if block, I see you use:
print "$test_arr[2] $group_val[2] $group_val[3]\n";

Last edited by keefaz; 10-14-2008 at 05:22 PM.
 
Old 10-14-2008, 06:51 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,246

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
Well, I did say it was untested; I wrote it in a hurry as in 'you need something like this'


anyway, this has been tested
Code:
open(IN, "<", "t.t") or die "Unable to open t.t: $!\n";
while ( defined ( $in_rec = <IN> ) )
{
   chomp($in_rec);
    if( $in_rec =~ /^class-map/ )
    {
        $class_name = (split(/ /, $in_rec))[2];
        next;
    }
    if( $in_rec =~ /^match/ )
    {
        @group_val = (split(/ /, $in_rec))[1,2];
        print "$class_name @group_val\n";
    }
}
Output
Code:
VOIP-Control access-group 101
VOIP-Control access-group 103
VOIP-RTP access-group 100
 
Old 10-15-2008, 12:44 PM   #9
sal_paradise42
Member
 
Registered: Jul 2003
Location: Utah
Distribution: Gentoo FreeBSD 5.4
Posts: 150

Original Poster
Rep: Reputation: 16
Thanks a lot. After a few minor tweaks this is working.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in accessing variables declared in another perl script john83reuben Programming 3 03-21-2008 02:45 AM
Perl: Source shell script to for environment variables? stefanlasiewski Programming 3 02-07-2006 06:27 PM
Perl Script: Variable Names composed of Variables wwnexc Programming 4 02-06-2006 03:47 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
C++ Assigning attributes to variables ? xconspirisist Programming 16 11-05-2003 06:08 AM


All times are GMT -5. The time now is 06:17 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration