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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-12-2008, 11:42 PM
|
#1
|
|
Member
Registered: Jul 2003
Location: Utah
Distribution: Gentoo FreeBSD 5.4
Posts: 150
Rep:
|
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.
|
|
|
|
10-13-2008, 02:00 AM
|
#2
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,973
|
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(!)
|
|
|
|
10-13-2008, 06:35 PM
|
#3
|
|
Member
Registered: Jul 2003
Location: Utah
Distribution: Gentoo FreeBSD 5.4
Posts: 150
Original Poster
Rep:
|
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
|
|
|
|
10-13-2008, 07:52 PM
|
#4
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
/^class_map/ shouldn't be /^class-map/ ?
|
|
|
|
10-13-2008, 08:10 PM
|
#5
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,973
|
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...

|
|
|
|
10-14-2008, 05:06 PM
|
#6
|
|
Member
Registered: Jul 2003
Location: Utah
Distribution: Gentoo FreeBSD 5.4
Posts: 150
Original Poster
Rep:
|
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
|
|
|
|
10-14-2008, 05:18 PM
|
#7
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
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.
|
|
|
|
10-14-2008, 06:51 PM
|
#8
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,973
|
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
|
|
|
|
10-15-2008, 12:44 PM
|
#9
|
|
Member
Registered: Jul 2003
Location: Utah
Distribution: Gentoo FreeBSD 5.4
Posts: 150
Original Poster
Rep:
|
Thanks a lot. After a few minor tweaks this is working.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:15 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|