LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help with a little perl script and assigning variables (https://www.linuxquestions.org/questions/programming-9/help-with-a-little-perl-script-and-assigning-variables-675942/)

sal_paradise42 10-12-2008 11:42 PM

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.

chrism01 10-13-2008 02:00 AM

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(!)

sal_paradise42 10-13-2008 06:35 PM

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

keefaz 10-13-2008 07:52 PM

/^class_map/ shouldn't be /^class-map/ ?

chrism01 10-13-2008 08:10 PM

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...
:(

sal_paradise42 10-14-2008 05:06 PM

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

keefaz 10-14-2008 05:18 PM

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

chrism01 10-14-2008 06:51 PM

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


sal_paradise42 10-15-2008 12:44 PM

Thanks a lot. After a few minor tweaks this is working.


All times are GMT -5. The time now is 09:07 PM.