in reply to Range Operator in If Statement
Wow, thanks everybody, what a response..!!
kejohm,
In terms of updating to Perl 5.10 and using the Smart Match Operator. I did look into that but updating the Perl version wasn't really an option at the moment bc it is a production server and a bunch of things running depend on that. But I did want to try to use that at some point...
ww,
Thanks for the reply, The example you show is sort of what I was trying to achieve with the ranges, maybe I'll give that one a try as well. Thanks again!
LanX,
Oh ok I gotcha... Bc it was working just not in the way I thought it would...? Thanks for the info.
NetWallah,
Hey, that looks cool I'll have to look into that Module as well. Seems alot like the Acme::Tools Module that I eventually went with from toolic's suggestion... Thanks I'll have to download that Module.
TJPride,
That's a good one..! I like that! Don't know why I didn't think of that. Clean and simple, since the control structure, once it finds a match, it will drop out of the if/elsif clause which leads to not having to use a range to test on, but just one test instead... Cool thanks..!
GrandFather,
Hey thanks for the reply. Sorry it took me a while to respond back but I decided to go with toolic's suggestion with the ACME::Tools solution... Works Good!
But in terms of a backstory. The thing I need this for is a script to run along side with MRTG and Interafce Templates to check which GigabitEthernet port it is and then separate them into groups to test for Backplane usage on a Cisco switch. Backplane is Bundeled ports (usually 8 consecutive one's) ranging from (Gi3/1<-->Gi3/48; Gi4/1<-->Gi4/48; ... all the way to ... Gi7/1<-->Gi7/48).
So basically some of the groups would be in ranges as follows... For example the range Gi3/1..Gi3/8 would be "Bundle-Gi3A", and Gi3/9..Gi3/16 would be "Bundle-Gi3B" all the way up to Bundle-Gi3F. As for Gi4 -to- Gi7 it would be the same Letters only difference would be the Gi number...
Here is my entire solution to the original question:
The original question had to do with the Subroutine "get_bundle()"
I know it's a SIMPLE program but here it is. Basically you just pass it the interface name (i.e. "Gi3/7") broken into two pieces, (1) the Gi number "Gi3" and (2) the reference number "7", which is passed from an MRTG Template/Script and then passed back to the Template to be added to the config ("cfg") file.
I'm SURE there is a 100 different ways to do this, but this works for my current situation...
I REALLLY want to thank all of you for your suggestions, it was MUCH MUCH more then I expected..!!
Thanks Again Everybody,
Matt
kejohm,
In terms of updating to Perl 5.10 and using the Smart Match Operator. I did look into that but updating the Perl version wasn't really an option at the moment bc it is a production server and a bunch of things running depend on that. But I did want to try to use that at some point...
ww,
Thanks for the reply, The example you show is sort of what I was trying to achieve with the ranges, maybe I'll give that one a try as well. Thanks again!
LanX,
Oh ok I gotcha... Bc it was working just not in the way I thought it would...? Thanks for the info.
NetWallah,
Hey, that looks cool I'll have to look into that Module as well. Seems alot like the Acme::Tools Module that I eventually went with from toolic's suggestion... Thanks I'll have to download that Module.
TJPride,
That's a good one..! I like that! Don't know why I didn't think of that. Clean and simple, since the control structure, once it finds a match, it will drop out of the if/elsif clause which leads to not having to use a range to test on, but just one test instead... Cool thanks..!
GrandFather,
Hey thanks for the reply. Sorry it took me a while to respond back but I decided to go with toolic's suggestion with the ACME::Tools solution... Works Good!
But in terms of a backstory. The thing I need this for is a script to run along side with MRTG and Interafce Templates to check which GigabitEthernet port it is and then separate them into groups to test for Backplane usage on a Cisco switch. Backplane is Bundeled ports (usually 8 consecutive one's) ranging from (Gi3/1<-->Gi3/48; Gi4/1<-->Gi4/48; ... all the way to ... Gi7/1<-->Gi7/48).
So basically some of the groups would be in ranges as follows... For example the range Gi3/1..Gi3/8 would be "Bundle-Gi3A", and Gi3/9..Gi3/16 would be "Bundle-Gi3B" all the way up to Bundle-Gi3F. As for Gi4 -to- Gi7 it would be the same Letters only difference would be the Gi number...
Here is my entire solution to the original question:
The original question had to do with the Subroutine "get_bundle()"
I know it's a SIMPLE program but here it is. Basically you just pass it the interface name (i.e. "Gi3/7") broken into two pieces, (1) the Gi number "Gi3" and (2) the reference number "7", which is passed from an MRTG Template/Script and then passed back to the Template to be added to the config ("cfg") file.
#!/usr/bin/perl use warnings; use strict; use Getopt::Long; use Acme::Tools qw(between); # Declare Command Line Option's Variables my $Gi_number; my $ref_number; my $get_what; # Declare regular variables my $bundle; my $target_lines; #my $target_name = "target_name"; my @number; my $GInum; my $userGraph; # Process Command-Line Options with the GetOptions::Long Module if ( @ARGV > 0 ) { GetOptions('Gi_number=s' => \$Gi_number, 'ref_number=s' => \$ref_number); } ### Extract the 'Gi' Number (i.e. Gi4 --> '4') and set it to $GInum @number = ($Gi_number =~ /(\d)/); $GInum = $number[0]; &get_bundle(); ### Build the the UserDefined Graph Name for the routers.cgi*Graph[] L +ine $userGraph = "Bundle-$Gi_number$bundle"; if ($bundle) { $target_lines .= "routers.cgi*Graph[\$target_name]: $userGraph tot +al average active\n"; } else { print "WARNING: No Bundle Letter has been assigned to \$bundle +... Try again.\n"; exit 2; } ###################################################################### +############## # SUB - get_bundle() --- This sub will take the interface reference nu +mber Gi3/"7" # # which is the '7' and get the corresponding Bundle letter +s.# ###################################################################### +############## sub get_bundle() { if (between($ref_number, 1, 8)) { $bundle = "A"; } if (between($ref_number, 9, 16)) { $bundle = "B"; } if (between($ref_number, 17, 24)) { $bundle = "C"; } if (between($ref_number, 25, 32)) { $bundle = "D"; } if (between($ref_number, 33, 40)) { $bundle = "E"; } if (between($ref_number, 41, 48)) { $bundle = "F"; } } ###################################################################### +########### ### END: get_bundle() print "$target_lines";
I'm SURE there is a 100 different ways to do this, but this works for my current situation...
I REALLLY want to thank all of you for your suggestions, it was MUCH MUCH more then I expected..!!
Thanks Again Everybody,
Matt
In Section
Seekers of Perl Wisdom