Reputation: 23
I am looking for a collecter information by SNMP on Cisco router. I collect the information without any problem. The problem is in my Perl regex.
Received by the array here SNMP:
$ mystring = 'iso.3.6.1.2.1.2.2.1.2.2 = STRING: "GigabitEthernet1/2"
iso.3.6.1.2.1.2.2.1.2.3 = STRING: "GigabitEthernet3/1"
iso.3.6.1.2.1.2.2.1.2.4 = STRING: "GigabitEthernet3/2"
iso.3.6.1.2.1.2.2.1.2.5 = STRING: "GigabitEthernet3/3"
iso.3.6.1.2.1.2.2.1.2.6 = STRING: "GigabitEthernet3/4"
iso.3.6.1.2.1.2.2.1.2.7 = STRING: "GigabitEthernet3/5"
iso.3.6.1.2.1.2.2.1.2.8 = STRING: "GigabitEthernet3/6"
iso.3.6.1.2.1.2.2.1.2.9 = STRING: "GigabitEthernet3/7"
iso.3.6.1.2.1.2.2.1.2.10 = STRING: "GigabitEthernet3/8"
iso.3.6.1.2.1.2.2.1.2.11 = STRING: "GigabitEthernet3/9"
iso.3.6.1.2.1.2.2.1.2.12 = STRING: "GigabitEthernet3/10"
iso.3.6.1.2.1.2.2.1.2.13 = STRING: "GigabitEthernet3/11"
iso.3.6.1.2.1.2.2.1.2.14 = STRING: "GigabitEthernet3/12"
iso.3.6.1.2.1.2.2.1.2.15 = STRING: "GigabitEthernet3/13"
iso.3.6.1.2.1.2.2.1.2.16 = STRING: "GigabitEthernet3/14"
iso.3.6.1.2.1.2.2.1.2.17 = STRING: "GigabitEthernet3/15"
iso.3.6.1.2.1.2.2.1.2.18 = STRING: "GigabitEthernet3/16" ';
@ array = ($ mystring = ~ m /. ([0-9]|[1-9][0-9]|[1-9][0-9][0-9]) = / gms);
print join ("\ n", @ array);
I have the latest issue of the mib but how to get the port? for example
array(
5->GigabitEthernet3/3
6->GigabitEthernet3/4
);
in a table.
Upvotes: 2
Views: 1365
Reputation: 385914
What you call array is actually an associative array. A hash table is a form of associative array.
use strict; use warnings;
my %port_by_mib;
for (split /\n/, $mystring) {
my ($mib) = /\.(\d+) / or die;
my ($port) = /"([^"]+)"/ or die;
$port_by_mib{$mib} = $port;
}
use strict
runs Perl in a stricter mode. This disallows dangerous or error-prone features. use warnings
will issue warnings that help debugging a script.my
keyword declares lexical variables. Declaring your variables is required in strict mode. If you forgot to declare a name, you will get a global symbol "$foo" requires explicit package name ...
fatal error.%
is the sigil for hash tables. Sigils are both a type declaration and a namespace selector.split
cuts $mystring
whenever the regex matches. for
loops over the resulting list of substrings and assigns each piece to the $_
default argument./\.(\d+) /
matches and saves any sequence of digits that is preceded by a dot and followed by a space. The script die
s (i.e throws a fatal error) when no such match is found. The left hand side of the assignment has a list context, so the RHS is executed in list context as well, causing $mib
to be initialized with the first capture buffer./"([^"]+)"/
matches text in double-quotes, and extracts that text. Upvotes: 2