bgpd: start listener on first instance
[jleu-quagga.git] / tools / mrlg.cgi
blobac468eef139ed7f4e14eb8e50306cc8e4af9066c
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/
12 ## This file is part of GNU Zebra.
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.
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.
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.
29 require 5.002;
30 use POSIX;
31 use Net::Telnet ();
35 ## Set the URL for your site.
36 $url="http://www.sample.com/mrlg.cgi";
38 ## Set your router variables in sub set_router and modify the selections in Main to match.
41 ############################################################
42 #Main
43 ############################################################
46 ## Set the router default
47 @Form{'router'} = "router1";
49 ## Get the form results now so we can override the default router
50 get_form();
52 print "Content-type: text/html\n\n";
54 print '
55 <html>
56 <head>
57 <title>Multi-Router Looking Glass for Zebra</title>
58 </head>
59 <body bgcolor=white>
61 <font face=arial size=3 color=blue>
62 <h1>Multi-Router Looking Glass for Zebra</h1>
63 Copyright 2000 - John Fraizer, EnterZone Inc.
64 <br>
67 print '
68 <font color=black>
70 print "<form METHOD=\"POST\" action=\"$url\">\n";
71 print "<B>Router:</B> <SELECT Name=\"router\" Size=1>\n";
72 print "<OPTION Value=\"$Form{'router'}\">$Form{'router'}\n";
73 print '
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>
94 ## Set up the address, pw and ports, etc for the selected router.
95 set_router();
97 ## Set up which command is to be executed (and then execute it!)
98 set_command();
101 print '
102 <br><br>
103 </font>
104 <font color=blue face=arial size=2>
105 Multi-Router Looking Glass for Zebra version 1.0<br>
106 Written by: John Fraizer -
107 <a href="http://www.ez-hosting.net/">EnterZone, Inc</a><br>
108 Source code: <a href="ftp://ftp.enterzone.net/looking-glass/">ftp://ftp.enterzone.net/looking-glass/</a>
109 </body>
110 </html>
113 ## All done!
115 exit (0);
119 ############################################################
120 sub get_form
121 ############################################################
124 #read STDIN
125 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
127 # Split the name-value pairs
128 @pairs = split(/&/, $buffer);
130 # For each name-value pair:
131 foreach $pair (@pairs)
134 # Split the pair up into individual variables.
135 local($name, $value) = split(/=/, $pair);
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;
141 $value =~ tr/+/ /;
142 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
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;
149 @Form{$name} = $value ;
155 ############################################################
156 sub set_router
157 ############################################################
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.
168 if ($Form{'router'} eq 'router1')
170 $server = '10.1.1.1';
171 $login_pass = 'zebra';
172 $bgpd = "2605";
173 $zebra = "";
174 $full_tables=1;
178 elsif ($Form{'router'} eq 'router2')
180 $server = '10.1.1.2';
181 $login_pass = 'zebra';
182 $bgpd = "2605";
183 $zebra = "2601";
186 elsif ($Form{'router'} eq 'router3')
188 $server = '10.1.1.3';
189 $login_pass = 'zebra';
190 $bgpd = "2605";
191 $zebra = "2601";
192 $full_tables=1;
195 elsif ($Form{'router'} eq 'router4')
197 $server = '10.1.1.4';
198 $login_pass = 'zebra';
199 $bgpd = "2605";
200 $zebra = "2601";
207 ############################################################
208 sub set_command
209 ############################################################
211 if ($Form{'query'} eq '1')
213 sh_ip_bgp('ip');
216 elsif ($Form{'query'} eq '2')
218 sh_ip_bgp_sum('ip');
221 if ($Form{'query'} eq '3')
223 sh_ip_route('ip');
226 if ($Form{'query'} eq '4')
228 sh_int();
230 if ($Form{'query'} eq '5')
232 sh_ip_bgp('ipv6');
234 if ($Form{'query'} eq '6')
236 sh_ip_bgp_sum('ipv6');
238 if ($Form{'query'} eq '7')
240 sh_ip_route('ipv6');
243 ############################################################
244 sub sh_ip_bgp
245 ############################################################
247 my $protocol = shift(@_);
248 $port = $bgpd;
249 if ($protocol ne 'ip' && $protocol ne 'ipv6')
251 print "Invalid protocol: $protocol\n";
252 print "protocol must be 'ip' or 'ipv6'\n\n";
253 return;
255 $command = "show $protocol bgp $Form{'arg'}";
256 if ($Form{'arg'} eq '')
258 if ($full_tables eq '1')
260 execute_command();
262 else
264 print "Sorry. Displaying the FULL routing table would put too much load on the router!\n\n";
267 else
269 execute_command();
273 ############################################################
274 sub sh_ip_bgp_sum
275 ############################################################
277 my $protocol = shift(@_);
278 $port = $bgpd;
279 if ($protocol ne 'ip' && $protocol ne 'ipv6')
281 print "Invalid protocol: $protocol\n";
282 print "protocol must be 'ip' or 'ipv6'\n\n";
283 return;
285 $command = "show $protocol bgp summary";
286 execute_command();
289 ############################################################
290 sub sh_ip_route
291 ############################################################
294 if ($zebra eq '')
296 print "Sorry. The <b>show ip route</b> command is disabled for this router."
298 else
301 $port = $zebra;
302 my $protocol = shift(@_);
303 if ($protocol ne 'ip' && $protocol ne 'ipv6')
305 print "Invalid protocol: $protocol\n";
306 print "protocol must be 'ip' or 'ipv6'\n\n";
307 return;
309 $command = "show $protocol route $Form{'arg'}";
310 if ($Form{'arg'} eq '')
312 if ($full_tables eq '1')
314 execute_command();
316 else
318 print "Sorry. Displaying the FULL routing table would put too much load on the router!\n\n";
321 else
323 execute_command();
328 ############################################################
329 sub sh_int
330 ############################################################
332 if ($zebra eq '')
334 print "Sorry. The <b>show interface</b> command is disabled for this router."
336 else
338 $port = $zebra;
339 $command = "show interface $Form{'arg'}";
340 execute_command();
346 ############################################################
347 sub execute_command
348 ############################################################
349 ## This code is based on:
351 ## Zebra interactive console
352 ## Copyright (C) 2000 Vladimir B. Grebenschikov <vova@express.ru>
358 print "Executing command = $command";
360 # my $port = ($opt_z ? 'zebra' : 0) ||
361 # ($opt_b ? 'bgpd' : 0) ||
362 # ($opt_o ? 'ospfd' : 0) ||
363 # ($opt_r ? 'ripd' : 0) || 'bgpd';
365 my $cmd = $command;
368 my $t = new Net::Telnet (Timeout => 10,
369 Prompt => '/[\>\#] $/',
370 Port => $port);
372 $t->open ($server);
374 $t->cmd ($login_pass);
376 if ($cmd)
378 docmd ($t, $cmd);
383 ############################################################
384 sub docmd
385 ############################################################
387 my ($t, $cmd) = @_;
388 my @lines = $t->cmd ($cmd);
389 print "<pre>\n";
390 print join ('', grep (!/[\>\#] $/, @lines)), "\n";
391 print "</pre>";