1#!/usr/bin/perl
2##
3## Zebra Looking Glass version 1.0
4## 01 FEB 2000
5## Copyright (C) 2000 John W. Fraizer III <john.fraizer@enterzone.net>
6## *All* copyright notices must remain in place to use this code.
7##
8## The latest version of this code is available at:
9## ftp://ftp.enterzone.net/looking-glass/
10##
11##
12## This file is part of GNU Zebra.
13##
14## GNU Zebra is free software; you can redistribute it and/or modify it
15## under the terms of the GNU General Public License as published by the
16## Free Software Foundation; either version 2, or (at your option) any
17## later version.
18##
19## GNU Zebra is distributed in the hope that it will be useful, but
20## WITHOUT ANY WARRANTY; without even the implied warranty of
21## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22## General Public License for more details.
23##
24## You should have received a copy of the GNU General Public License
25## along with GNU Zebra; see the file COPYING.  If not, write to the
26## Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27## Boston, MA 02111-1307, USA.
28
29require 5.002;
30use POSIX;
31use Net::Telnet ();
32
33
34
35## Set the URL for your site.
36$url="http://www.sample.com/mrlg.cgi";
37
38## Set your router variables in sub set_router and modify the selections in Main to match.
39
40
41############################################################
42#Main
43############################################################
44{
45
46## Set the router default
47@Form{'router'} = "router1";
48
49## Get the form results now so we can override the default router 
50get_form();
51
52print "Content-type: text/html\n\n";
53
54print '
55<html>
56<head>
57<title>Multi-Router Looking Glass for Zebra</title>
58</head>
59<body bgcolor=white>
60	
61<font face=arial size=3 color=blue>
62<h1>Multi-Router Looking Glass for Zebra</h1>
63Copyright 2000 - John Fraizer, EnterZone Inc.
64<br>
65';
66
67print '
68<font color=black>
69';
70print "<form METHOD=\"POST\" action=\"$url\">\n";
71print "<B>Router:</B>  <SELECT Name=\"router\" Size=1>\n";
72print "<OPTION Value=\"$Form{'router'}\">$Form{'router'}\n";
73print '
74<OPTION Value="router1">router1
75<OPTION Value="router2">router2
76<OPTION Value="router3">router3
77<OPTION Value="router4">router4
78</select>
79<br><br>
80<B>Query</B>:
81<br>
82<input type=radio name=query value=1>show ip bgp<br>
83<input type=radio name=query value=2>show ip bgp summary<br>
84<input type=radio name=query value=3>show ip route<br>
85<input type=radio name=query value=4>show interface<br>
86<input type=radio name=query value=5>show ipv6 bgp<br>
87<input type=radio name=query value=6>show ipv6 bgp summary<br>
88<input type=radio name=query value=7>show ipv6 route<br>
89<br>
90<B>Argument:</B> <input type=text name=arg length=20 maxlength=60>
91<input type="submit" value="Execute"></form>	
92';
93
94## Set up the address, pw and ports, etc for the selected router.
95set_router();
96
97## Set up which command is to be executed (and then execute it!)
98set_command();
99
100
101print '
102<br><br>
103</font>
104<font color=blue face=arial size=2>
105Multi-Router Looking Glass for Zebra version 1.0<br>
106Written by: John Fraizer -
107<a href="http://www.ez-hosting.net/">EnterZone, Inc</a><br>
108Source code: <a href="ftp://ftp.enterzone.net/looking-glass/">ftp://ftp.enterzone.net/looking-glass/</a>
109</body>
110</html>
111';
112
113## All done!
114
115exit (0); 
116}
117
118
119############################################################
120sub get_form
121############################################################
122{
123        
124        #read STDIN
125        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
126
127        # Split the name-value pairs
128        @pairs = split(/&/, $buffer);
129  
130        # For each name-value pair:
131        foreach $pair (@pairs)
132                {
133                
134                # Split the pair up into individual variables.
135                local($name, $value) = split(/=/, $pair);
136
137                # Decode the form encoding on the name and value variables.
138                $name =~ tr/+/ /;
139                $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
140 
141                $value =~ tr/+/ /;
142                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
143
144                # If they try to include server side includes, erase them, so they
145                # aren't a security risk if the html gets returned.  Another
146                # security hole plugged up.
147                $value =~ s/<!--(.|\n)*-->//g;
148        
149                @Form{$name} = $value ;
150                 
151                }
152        
153}       
154
155############################################################
156sub set_router
157############################################################
158
159## $server is the IP address of the router running zebra
160## $login_pass is the password of the router
161## $bgpd is the port that bgpd will answer on
162## $zebra is the port that zebra will answer on
163## if $zebra is "", it will disable sh ip route and sh int for that router.
164## if $full_tables is set to "1" for a router, full BGP and IP ROUTE table dumps will be allowed via the looking glass.
165## This is a BAD thing to do if you have multiple full views on a router.  That's why the option is there.
166
167{
168if ($Form{'router'} eq 'router1')
169        {
170$server = '10.1.1.1';
171$login_pass = 'zebra';
172$bgpd = "2605";
173$zebra = "";
174$full_tables=1;
175
176        }
177
178elsif ($Form{'router'} eq 'router2')
179        {
180$server = '10.1.1.2';
181$login_pass = 'zebra';
182$bgpd = "2605";
183$zebra = "2601";
184        }
185
186elsif ($Form{'router'} eq 'router3')
187        {
188$server = '10.1.1.3';
189$login_pass = 'zebra';
190$bgpd = "2605";
191$zebra = "2601";
192$full_tables=1;
193        }
194
195elsif ($Form{'router'} eq 'router4')
196        {
197$server = '10.1.1.4';
198$login_pass = 'zebra';
199$bgpd = "2605";
200$zebra = "2601";
201        }
202
203
204}
205
206
207############################################################
208sub set_command
209############################################################
210{
211if ($Form{'query'} eq '1')
212	{
213	sh_ip_bgp('ip');
214	}
215
216elsif ($Form{'query'} eq '2')
217	{
218	sh_ip_bgp_sum('ip');
219	}
220
221if ($Form{'query'} eq '3')
222	{
223	sh_ip_route('ip');
224	}
225
226if ($Form{'query'} eq '4')
227	{
228	sh_int();
229	}
230if ($Form{'query'} eq '5')
231	{
232	sh_ip_bgp('ipv6');
233	}
234if ($Form{'query'} eq '6')
235	{
236	sh_ip_bgp_sum('ipv6');
237	}
238if ($Form{'query'} eq '7')
239	{
240	sh_ip_route('ipv6');
241	}
242}
243############################################################
244sub sh_ip_bgp
245############################################################
246{
247my $protocol = shift(@_);
248$port = $bgpd;
249if ($protocol ne 'ip' && $protocol ne 'ipv6')
250	{
251	print "Invalid protocol: $protocol\n";
252	print "protocol must be 'ip' or 'ipv6'\n\n";
253	return;
254	}
255$command = "show $protocol bgp $Form{'arg'}";
256if ($Form{'arg'} eq '')
257	{
258	if ($full_tables eq '1')
259		{
260		execute_command();
261		}
262	else
263		{
264		print "Sorry.  Displaying the FULL routing table would put too much load on the router!\n\n";
265		}
266	}
267else
268	{
269	execute_command();
270	}
271}
272
273############################################################
274sub sh_ip_bgp_sum
275############################################################
276{
277	my $protocol = shift(@_);
278	$port = $bgpd;
279	if ($protocol ne 'ip' && $protocol ne 'ipv6')
280		{
281		print "Invalid protocol: $protocol\n";
282		print "protocol must be 'ip' or 'ipv6'\n\n";
283		return;
284		}
285	$command = "show $protocol bgp summary";
286	execute_command();
287}
288
289############################################################
290sub sh_ip_route
291############################################################
292{
293
294if ($zebra eq '')
295	{
296	print "Sorry. The <b>show ip route</b> command is disabled for this router."
297	}
298else
299	{
300
301	$port = $zebra;
302	my $protocol = shift(@_);
303	if ($protocol ne 'ip' && $protocol ne 'ipv6')
304		{
305		print "Invalid protocol: $protocol\n";
306		print "protocol must be 'ip' or 'ipv6'\n\n";
307		return;
308		}
309	$command = "show $protocol route $Form{'arg'}";
310	if ($Form{'arg'} eq '')
311		{
312		if ($full_tables eq '1')
313			{
314			execute_command();
315			}
316		else
317			{
318			print "Sorry.  Displaying the FULL routing table would put too much load on the router!\n\n";
319			}
320		}
321	else
322		{
323		execute_command();
324		}
325	}
326}
327
328############################################################
329sub sh_int
330############################################################
331{
332if ($zebra eq '')
333	{
334	print "Sorry. The <b>show interface</b> command is disabled for this router."
335	}
336else
337	{
338	$port = $zebra;
339	$command = "show interface $Form{'arg'}";
340	execute_command();
341	}
342}
343
344
345
346############################################################
347sub execute_command
348############################################################
349## This code is based on:
350##
351## Zebra interactive console
352## Copyright (C) 2000 Vladimir B. Grebenschikov <vova@express.ru>
353##
354
355
356{
357
358print "Executing command = $command";
359
360#  my $port = ($opt_z ? 'zebra' : 0) ||
361#             ($opt_b ? 'bgpd' : 0) ||
362#             ($opt_o ? 'ospfd' : 0) ||
363#	     ($opt_r ? 'ripd' : 0) || 'bgpd';
364
365my $cmd = $command;
366
367
368  my $t = new Net::Telnet (Timeout => 10,
369			   Prompt  => '/[\>\#] $/',
370			   Port    => $port);
371
372  $t->open ($server);
373
374  $t->cmd ($login_pass);
375
376  if ($cmd)
377    {
378      docmd ($t, $cmd);
379    }
380
381}
382
383############################################################
384sub docmd
385############################################################
386{
387  my ($t, $cmd) = @_;
388  my @lines = $t->cmd ($cmd);
389  print "<pre>\n";
390  print join ('', grep (!/[\>\#] $/, @lines)), "\n";
391  print "</pre>";
392}
393
394
395
396