help with a little perl script and assigning variables
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.