cdp.grammar: Parse Cisco CDP information
author"Tomas Zeman <tzeman@volny.cz>"
Thu, 11 Mar 2010 14:25:56 +0100
changeset 30 db3aeb17baa7
parent 29 963890fd6465
child 31 509d9985d152
cdp.grammar: Parse Cisco CDP information
cdp.grammar
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cdp.grammar	Thu Mar 11 14:25:56 2010 +0100
@@ -0,0 +1,100 @@
+# Grammar file to parse Cisco CDP information
+# show cdp neighbor detail
+#
+# Copyright (c) 2010  Tomas Zeman <tzeman@volny.cz>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted providing that the following conditions 
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+{ # perl code follows
+
+$::res = {};
+$::res->{cdp} = ();
+$::cur_cdp = {};
+my $seen_neighbor = 0;
+
+sub init_cur_cdp() {
+	$::cur_cdp = {
+		'neighbor' => '',
+		'ip' => [],
+		lport => '',
+		rport => '',
+	};
+}
+
+sub push_cur_cdp() {
+	push @{$::res->{cdp}}, $::cur_cdp if ($seen_neighbor);
+	$seen_neighbor = 0;
+}
+
+}
+
+file:		<skip: qr/[^\S\n]*/>	# Ignore non-newline whitespace
+		line(s) eofile
+
+line:		neighbor_line
+		| entry_address
+		| neighbor_port
+		| word(s) eol
+		| word(s) eofile
+		| emptyline
+		| <error>
+
+emptyline:	eol
+
+word:		/\S+/  # any non-space
+		{ $item[1] }
+
+eofile:		/^\Z/
+		{ push_cur_cdp; }
+
+eol:		/\n/
+
+identifier:	/[0-9a-zA-Z:_\.-]+/
+		{ $item[1] }
+
+ip:		/\d+\.\d+\.\d+\.\d+/
+		{ $item[1] }
+
+iface_name:	/\w+(-\w+)?( )?\d+[0-9\/\.:]*/
+		{ $item[1] }
+
+neighbor_line:	"Device ID:" identifier eol
+		{
+		push_cur_cdp;
+		init_cur_cdp;
+		$::cur_cdp->{'neighbor'} = $item{identifier};
+		$seen_neighbor = 1;
+		}
+
+neighbor_port:	"Interface:" iface_name"," "Port ID (outgoing port):" iface_name eol
+		{
+		$::cur_cdp->{lport} = $item[2];
+		$::cur_cdp->{rport} = $item[5];
+		}
+
+entry_address: "Entry address(es):" eol entry_ip_address(s)
+
+entry_ip_address: "IP address:" ip eol
+		{ push @{$::cur_cdp->{ip}}, $item{ip}; }
+