mrlg.cgi 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. require 5.002;
  29. use POSIX;
  30. use Net::Telnet ();
  31. ## Set the URL for your site.
  32. $url="http://www.sample.com/mrlg.cgi";
  33. ## Set your router variables in sub set_router and modify the selections in Main to match.
  34. ############################################################
  35. #Main
  36. ############################################################
  37. {
  38. ## Set the router default
  39. @Form{'router'} = "router1";
  40. ## Get the form results now so we can override the default router
  41. get_form();
  42. print "Content-type: text/html\n\n";
  43. print '
  44. <html>
  45. <head>
  46. <title>Multi-Router Looking Glass for Zebra</title>
  47. </head>
  48. <body bgcolor=white>
  49. <font face=arial size=3 color=blue>
  50. <h1>Multi-Router Looking Glass for Zebra</h1>
  51. Copyright 2000 - John Fraizer, EnterZone Inc.
  52. <br>
  53. ';
  54. print '
  55. <font color=black>
  56. ';
  57. print "<form METHOD=\"POST\" action=\"$url\">\n";
  58. print "<B>Router:</B> <SELECT Name=\"router\" Size=1>\n";
  59. print "<OPTION Value=\"$Form{'router'}\">$Form{'router'}\n";
  60. print '
  61. <OPTION Value="router1">router1
  62. <OPTION Value="router2">router2
  63. <OPTION Value="router3">router3
  64. <OPTION Value="router4">router4
  65. </select>
  66. <br><br>
  67. <B>Query</B>:
  68. <br>
  69. <input type=radio name=query value=1>show ip bgp<br>
  70. <input type=radio name=query value=2>show ip bgp summary<br>
  71. <input type=radio name=query value=3>show ip route<br>
  72. <input type=radio name=query value=4>show interface<br>
  73. <input type=radio name=query value=5>show ipv6 bgp<br>
  74. <input type=radio name=query value=6>show ipv6 bgp summary<br>
  75. <input type=radio name=query value=7>show ipv6 route<br>
  76. <br>
  77. <B>Argument:</B> <input type=text name=arg length=20 maxlength=60>
  78. <input type="submit" value="Execute"></form>
  79. ';
  80. ## Set up the address, pw and ports, etc for the selected router.
  81. set_router();
  82. ## Set up which command is to be executed (and then execute it!)
  83. set_command();
  84. print '
  85. <br><br>
  86. </font>
  87. <font color=blue face=arial size=2>
  88. Multi-Router Looking Glass for Zebra version 1.0<br>
  89. Written by: John Fraizer -
  90. <a href="http://www.ez-hosting.net/">EnterZone, Inc</a><br>
  91. Source code: <a href="ftp://ftp.enterzone.net/looking-glass/">ftp://ftp.enterzone.net/looking-glass/</a>
  92. </body>
  93. </html>
  94. ';
  95. ## All done!
  96. exit (0);
  97. }
  98. ############################################################
  99. sub get_form
  100. ############################################################
  101. {
  102. #read STDIN
  103. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  104. # Split the name-value pairs
  105. @pairs = split(/&/, $buffer);
  106. # For each name-value pair:
  107. foreach $pair (@pairs)
  108. {
  109. # Split the pair up into individual variables.
  110. local($name, $value) = split(/=/, $pair);
  111. # Decode the form encoding on the name and value variables.
  112. $name =~ tr/+/ /;
  113. $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  114. $value =~ tr/+/ /;
  115. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  116. # If they try to include server side includes, erase them, so they
  117. # aren't a security risk if the html gets returned. Another
  118. # security hole plugged up.
  119. $value =~ s/<!--(.|\n)*-->//g;
  120. @Form{$name} = $value ;
  121. }
  122. }
  123. ############################################################
  124. sub set_router
  125. ############################################################
  126. ## $server is the IP address of the router running zebra
  127. ## $login_pass is the password of the router
  128. ## $bgpd is the port that bgpd will answer on
  129. ## $zebra is the port that zebra will answer on
  130. ## if $zebra is "", it will disable sh ip route and sh int for that router.
  131. ## if $full_tables is set to "1" for a router, full BGP and IP ROUTE table dumps will be allowed via the looking glass.
  132. ## This is a BAD thing to do if you have multiple full views on a router. That's why the option is there.
  133. {
  134. if ($Form{'router'} eq 'router1')
  135. {
  136. $server = '10.1.1.1';
  137. $login_pass = 'zebra';
  138. $bgpd = "2605";
  139. $zebra = "";
  140. $full_tables=1;
  141. }
  142. elsif ($Form{'router'} eq 'router2')
  143. {
  144. $server = '10.1.1.2';
  145. $login_pass = 'zebra';
  146. $bgpd = "2605";
  147. $zebra = "2601";
  148. }
  149. elsif ($Form{'router'} eq 'router3')
  150. {
  151. $server = '10.1.1.3';
  152. $login_pass = 'zebra';
  153. $bgpd = "2605";
  154. $zebra = "2601";
  155. $full_tables=1;
  156. }
  157. elsif ($Form{'router'} eq 'router4')
  158. {
  159. $server = '10.1.1.4';
  160. $login_pass = 'zebra';
  161. $bgpd = "2605";
  162. $zebra = "2601";
  163. }
  164. }
  165. ############################################################
  166. sub set_command
  167. ############################################################
  168. {
  169. if ($Form{'query'} eq '1')
  170. {
  171. sh_ip_bgp('ip');
  172. }
  173. elsif ($Form{'query'} eq '2')
  174. {
  175. sh_ip_bgp_sum('ip');
  176. }
  177. if ($Form{'query'} eq '3')
  178. {
  179. sh_ip_route('ip');
  180. }
  181. if ($Form{'query'} eq '4')
  182. {
  183. sh_int();
  184. }
  185. if ($Form{'query'} eq '5')
  186. {
  187. sh_ip_bgp('ipv6');
  188. }
  189. if ($Form{'query'} eq '6')
  190. {
  191. sh_ip_bgp_sum('ipv6');
  192. }
  193. if ($Form{'query'} eq '7')
  194. {
  195. sh_ip_route('ipv6');
  196. }
  197. }
  198. ############################################################
  199. sub sh_ip_bgp
  200. ############################################################
  201. {
  202. my $protocol = shift(@_);
  203. $port = $bgpd;
  204. if ($protocol ne 'ip' && $protocol ne 'ipv6')
  205. {
  206. print "Invalid protocol: $protocol\n";
  207. print "protocol must be 'ip' or 'ipv6'\n\n";
  208. return;
  209. }
  210. $command = "show $protocol bgp $Form{'arg'}";
  211. if ($Form{'arg'} eq '')
  212. {
  213. if ($full_tables eq '1')
  214. {
  215. execute_command();
  216. }
  217. else
  218. {
  219. print "Sorry. Displaying the FULL routing table would put too much load on the router!\n\n";
  220. }
  221. }
  222. else
  223. {
  224. execute_command();
  225. }
  226. }
  227. ############################################################
  228. sub sh_ip_bgp_sum
  229. ############################################################
  230. {
  231. my $protocol = shift(@_);
  232. $port = $bgpd;
  233. if ($protocol ne 'ip' && $protocol ne 'ipv6')
  234. {
  235. print "Invalid protocol: $protocol\n";
  236. print "protocol must be 'ip' or 'ipv6'\n\n";
  237. return;
  238. }
  239. $command = "show $protocol bgp summary";
  240. execute_command();
  241. }
  242. ############################################################
  243. sub sh_ip_route
  244. ############################################################
  245. {
  246. if ($zebra eq '')
  247. {
  248. print "Sorry. The <b>show ip route</b> command is disabled for this router."
  249. }
  250. else
  251. {
  252. $port = $zebra;
  253. my $protocol = shift(@_);
  254. if ($protocol ne 'ip' && $protocol ne 'ipv6')
  255. {
  256. print "Invalid protocol: $protocol\n";
  257. print "protocol must be 'ip' or 'ipv6'\n\n";
  258. return;
  259. }
  260. $command = "show $protocol route $Form{'arg'}";
  261. if ($Form{'arg'} eq '')
  262. {
  263. if ($full_tables eq '1')
  264. {
  265. execute_command();
  266. }
  267. else
  268. {
  269. print "Sorry. Displaying the FULL routing table would put too much load on the router!\n\n";
  270. }
  271. }
  272. else
  273. {
  274. execute_command();
  275. }
  276. }
  277. }
  278. ############################################################
  279. sub sh_int
  280. ############################################################
  281. {
  282. if ($zebra eq '')
  283. {
  284. print "Sorry. The <b>show interface</b> command is disabled for this router."
  285. }
  286. else
  287. {
  288. $port = $zebra;
  289. $command = "show interface $Form{'arg'}";
  290. execute_command();
  291. }
  292. }
  293. ############################################################
  294. sub execute_command
  295. ############################################################
  296. ## This code is based on:
  297. ##
  298. ## Zebra interactive console
  299. ## Copyright (C) 2000 Vladimir B. Grebenschikov <vova@express.ru>
  300. ##
  301. {
  302. print "Executing command = $command";
  303. # my $port = ($opt_z ? 'zebra' : 0) ||
  304. # ($opt_b ? 'bgpd' : 0) ||
  305. # ($opt_o ? 'ospfd' : 0) ||
  306. # ($opt_r ? 'ripd' : 0) || 'bgpd';
  307. my $cmd = $command;
  308. my $t = new Net::Telnet (Timeout => 10,
  309. Prompt => '/[\>\#] $/',
  310. Port => $port);
  311. $t->open ($server);
  312. $t->cmd ($login_pass);
  313. if ($cmd)
  314. {
  315. docmd ($t, $cmd);
  316. }
  317. }
  318. ############################################################
  319. sub docmd
  320. ############################################################
  321. {
  322. my ($t, $cmd) = @_;
  323. my @lines = $t->cmd ($cmd);
  324. print "<pre>\n";
  325. print join ('', grep (!/[\>\#] $/, @lines)), "\n";
  326. print "</pre>";
  327. }