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