| author | Tomas Zeman <tzeman@volny.cz> |
| Thu, 23 Aug 2012 14:06:18 +0200 | |
| changeset 45 | 9ba98ba61f6b |
| parent 30 | db3aeb17baa7 |
| permissions | -rw-r--r-- |
|
30
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
1 |
# Grammar file to parse Cisco CDP information |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
2 |
# show cdp neighbor detail |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
3 |
# |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
4 |
# Copyright (c) 2010 Tomas Zeman <tzeman@volny.cz> |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
5 |
# All rights reserved. |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
6 |
# |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
7 |
# Redistribution and use in source and binary forms, with or without |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
8 |
# modification, are permitted providing that the following conditions |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
9 |
# are met: |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
10 |
# 1. Redistributions of source code must retain the above copyright |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
11 |
# notice, this list of conditions and the following disclaimer. |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
12 |
# 2. Redistributions in binary form must reproduce the above copyright |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
13 |
# notice, this list of conditions and the following disclaimer in the |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
14 |
# documentation and/or other materials provided with the distribution. |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
15 |
# |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
16 |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
17 |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
20 |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
21 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
22 |
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
23 |
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
24 |
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
25 |
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
26 |
# POSSIBILITY OF SUCH DAMAGE. |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
27 |
# |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
28 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
29 |
{ # perl code follows
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
30 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
31 |
$::res = {};
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
32 |
$::res->{cdp} = ();
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
33 |
$::cur_cdp = {};
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
34 |
my $seen_neighbor = 0; |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
35 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
36 |
sub init_cur_cdp() {
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
37 |
$::cur_cdp = {
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
38 |
'neighbor' => '', |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
39 |
'ip' => [], |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
40 |
lport => '', |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
41 |
rport => '', |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
42 |
}; |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
43 |
} |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
44 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
45 |
sub push_cur_cdp() {
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
46 |
push @{$::res->{cdp}}, $::cur_cdp if ($seen_neighbor);
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
47 |
$seen_neighbor = 0; |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
48 |
} |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
49 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
50 |
} |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
51 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
52 |
file: <skip: qr/[^\S\n]*/> # Ignore non-newline whitespace |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
53 |
line(s) eofile |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
54 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
55 |
line: neighbor_line |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
56 |
| entry_address |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
57 |
| neighbor_port |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
58 |
| word(s) eol |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
59 |
| word(s) eofile |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
60 |
| emptyline |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
61 |
| <error> |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
62 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
63 |
emptyline: eol |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
64 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
65 |
word: /\S+/ # any non-space |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
66 |
{ $item[1] }
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
67 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
68 |
eofile: /^\Z/ |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
69 |
{ push_cur_cdp; }
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
70 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
71 |
eol: /\n/ |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
72 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
73 |
identifier: /[0-9a-zA-Z:_\.-]+/ |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
74 |
{ $item[1] }
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
75 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
76 |
ip: /\d+\.\d+\.\d+\.\d+/ |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
77 |
{ $item[1] }
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
78 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
79 |
iface_name: /\w+(-\w+)?( )?\d+[0-9\/\.:]*/ |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
80 |
{ $item[1] }
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
81 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
82 |
neighbor_line: "Device ID:" identifier eol |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
83 |
{
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
84 |
push_cur_cdp; |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
85 |
init_cur_cdp; |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
86 |
$::cur_cdp->{'neighbor'} = $item{identifier};
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
87 |
$seen_neighbor = 1; |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
88 |
} |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
89 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
90 |
neighbor_port: "Interface:" iface_name"," "Port ID (outgoing port):" iface_name eol |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
91 |
{
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
92 |
$::cur_cdp->{lport} = $item[2];
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
93 |
$::cur_cdp->{rport} = $item[5];
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
94 |
} |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
95 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
96 |
entry_address: "Entry address(es):" eol entry_ip_address(s) |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
97 |
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
98 |
entry_ip_address: "IP address:" ip eol |
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
99 |
{ push @{$::cur_cdp->{ip}}, $item{ip}; }
|
|
db3aeb17baa7
cdp.grammar: Parse Cisco CDP information
"Tomas Zeman <tzeman@volny.cz>"
parents:
diff
changeset
|
100 |