-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
3201 lines (2778 loc) · 77.5 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* HSDN PHP Looking Glass version 1.2.22b
*
* General Features:
* - Supports the Telnet and SSH (through Putty/plink or sshpass)
* - Supports the Cisco, MikroTik v5/v6, Juniper, Huawei (Comware),
* Quagga (Zebra) and OpenBGPD routers.
* - Supports the IPv4 and IPv6 protocols
* - Automatic conversion IPs to subnets using RADb (for MikroTik)
* - Drawing graph of BGP AS pathes using GraphViz toolkit
* - Works on php 5.2.0 and above
*
* System Requirements:
* - php version 5.2.0 and above with Sockets and Filter
* http://www.php.net/
* - Putty for SSH connections usign `plink' command
* http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
* - GraphViz toolkit for drawing BGP pathes graph
* http://www.graphviz.org/
* - php pear package Image_GraphViz
* http://pear.php.net/package/Image_GraphViz
*
*
* Copyright (C) 2012-2018 Information Networks Ltd. <[email protected]>
* http://www.hsdn.org/
*
* Copyright (C) 2000-2002 Cougar <[email protected]>
* http://www.version6.net/
*
* Copyright (C) 2014 Regional Networks Ltd. <[email protected]>
* http://www.regnets.ru/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//error_reporting(0);
// ------------------------------------------------------------------------
// Do not edit below this line, unless you fully understand the implications.
// ------------------------------------------------------------------------
// Configurations defaults. DO NOT EDIT THIS!
// For your configurations, please use the file `lg_config.php'
$_CONFIG = array
(
'asn' => '12345',
'company' => 'Nome da Empresa',
'logo' => 'lg_logo.gif',
'color' => '#444',
'sshcommand' => 'sshpass',
'plink' => '/usr/local/bin/plink',
'sshpass' => '/usr/bin/sshpass',
'ipwhois' => 'https://stat.ripe.net/',
'aswhois' => 'https://stat.ripe.net/',
'routers' => array(),
);
@ob_end_flush();
if (file_exists('lg_config.php') AND is_readable('lg_config.php'))
{
require_once 'lg_config.php';
}
$router = isset($_REQUEST['router']) ? trim($_REQUEST['router']) : FALSE;
$protocol = isset($_REQUEST['protocol']) ? trim($_REQUEST['protocol']) : FALSE;
$command = isset($_REQUEST['command']) ? trim($_REQUEST['command']) : FALSE;
$query = isset($_REQUEST['query']) ? trim($_REQUEST['query']) : FALSE;
if ($command != 'graph' OR !isset($_REQUEST['render']) OR !isset($_CONFIG['routers'][$router]))
{
// HTML header
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!--
=================================================
Powered by HSDN PHP Looking Glass
- https://git.dev.hsdn.org/pub/lg
- https://github.com/hsdn/lg
=================================================
-->
<title>AS<?php print $_CONFIG['asn'] ?> Looking Glass</title>
<meta charset="utf-8">
<link rel="shortcut icon" href="favicon.ico">
<style type="text/css">
<!--
body { font: 14px normal Arial, Helvetica, sans-serif; margin: 30px 10%; color: #000; background: #fff; }
h2 { font-size: 24px; font-weight: normal; }
form { margin: 0; padding: 0 0 15px 0; }
p, object { margin: 0; padding: 0 0 15px 0; }
hr { margin: 0 0 15px 0; border: none; color: #000; background-color: #000; height: 1px; }
a:link, a:visited { color: <?php print $_CONFIG['color'] ?>; }
a:hover { color: #ccc; }
table { border: 0; }
table th { background: <?php print $_CONFIG['color'] ?>; color: #fff; white-space: nowrap; font-size: 14px; text-align: center; }
.form { margin: auto; text-align: left; background: #efefef; border: 5px solid #efefef; }
.center { text-align: center; }
.error { color: red; font-weight: bold; }
.warning { color: blue; font-weight: bold; }
.legend { font-size: 12px; margin: auto; }
//-->
</style>
<script type="text/javascript">
<!--
function load() {
var loading = document.getElementById('loading');
if (loading !== null) {
loading.style.display = 'none';
}
}
//-->
</script>
</head>
<body onload="load();">
<?php if (isset($_CONFIG['logo']) AND $_CONFIG['logo']): ?>
<div class="center"><a href="?"><img src="<?php print $_CONFIG['logo'] ?>" border="0" alt="lg"></a></div>
<?php endif ?>
<div class="center"><h2>AS<?php print $_CONFIG['asn'] ?> Looking Glass</h2></div>
<hr>
<?php
flush();
}
$queries = array
(
'ios' => array
(
'ipv4' => array
(
'bgp' => 'show ip bgp %s',
'advertised-routes' => 'show ip bgp neighbors %s advertised-routes',
'received-routes' => 'show ip bgp neighbors %s received-routes',
'routes' => 'show ip bgp neighbors %s routes',
'summary' => 'show ip bgp summary',
'ping' => 'ping ip %s',
'trace' => 'traceroute ip %s'
),
'ipv6' => array
(
'bgp' => 'show bgp ipv6 unicast %s',
'advertised-routes' => 'show bgp ipv6 neighbors %s advertised-routes',
'received-routes' => 'show bgp ipv6 neighbors %s received-routes',
'routes' => 'show bgp ipv6 neighbors %s routes',
'summary' => 'show bgp ipv6 unicast summary',
'ping' => 'ping ipv6 %s',
'trace' => 'traceroute ipv6 %s'
)
),
'quagga' => array
(
'ipv4' => array
(
'bgp' => 'show ip bgp %s',
'advertised-routes' => 'show ip bgp neighbors %s advertised-routes',
'received-routes' => 'show ip bgp neighbors %s received-routes',
'routes' => 'show ip bgp neighbors %s routes',
'summary' => 'show ip bgp summary',
'ping' => 'ping -t 5 -c 5 %s',
'trace' => 'traceroute -Iaw 1 %s',
),
'ipv6' => array
(
'bgp' => 'show ipv6 bgp %s',
'advertised-routes' => 'show ipv6 bgp neighbors %s advertised-routes',
'received-routes' => 'show ipv6 bgp neighbors %s received-routes',
'routes' => 'show ipv6 bgp neighbors %s routes',
'summary' => 'show ipv6 bgp summary',
'ping' => 'ping6 -c 5 %s',
'trace' => 'traceroute6 -Ialw 1 %s',
)
),
'mikrotik' => array
(
'ipv4' => array
(
'bgp' => '/ip route print detail where bgp dst-address in %s',
'advertised-routes' => '/routing bgp advertisements print peer=%s',
'routes' => '/ip route print where gateway=%s',
'summary' => '/routing bgp peer print status where address-families=ip',
'ping' => '/ping count=5 size=56 %s',
'trace' => '/tool traceroute %s size=60 count=1',
),
'ipv6' => array
(
'bgp' => '/ipv6 route print detail where bgp dst-address=%s',
'advertised-routes' => '/routing bgp advertisements print peer=%s',
'routes' => '/ipv6 route print where gateway=%s',
'summary' => '/routing bgp peer print status where address-families=ipv6',
'ping' => '/ping count=5 size=56 %s',
'trace' => '/tool traceroute %s size=60 count=1',
)
),
'junos' => array
(
'ipv4' => array
(
'bgp' => 'show bgp %s',
'advertised-routes' => 'show route advertising-protocol bgp %s',
'routes' => 'show route receive-protocol bgp %s active-path',
'summary' => 'show bgp summary',
'ping' => 'ping count 5 %s',
'trace' => 'traceroute %s as-number-lookup',
),
'ipv6' => array
(
'bgp' => 'show bgp %s',
'advertised-routes' => 'show route advertising-protocol bgp %s',
'routes' => 'show route receive-protocol bgp %s active-path',
'summary' => 'show bgp summary',
'ping' => 'ping count 5 %s',
'trace' => 'traceroute %s',
)
),
'openbgpd' => array
(
'ipv4' => array
(
'bgp' => 'bgpctl show ip bgp %s',
'advertised-routes' => 'bgpctl show rib neighbor %s out',
'received-routes' => 'bgpctl show rib neighbor %s in',
'routes' => 'bgpctl show rib selected neighbor %s',
'summary' => 'bgpctl show summary',
'ping' => 'ping -c 5 %s',
'trace' => 'traceroute %s',
),
'ipv6' => array
(
'bgp' => 'show bgp %s',
'advertised-routes' => 'bgpctl show rib neighbor %s out',
'received-routes' => 'bgpctl show rib neighbor %s in',
'routes' => 'bgpctl show rib selected neighbor %s',
'summary' => 'bgpctl show summary',
'ping' => 'ping6 -c 5 %s',
'trace' => 'traceroute6 %s',
)
),
'huawei' => array
(
'ipv4' => array
(
'bgp' => 'display bgp routing-table %s',
'advertised-routes' => 'display bgp routing-table peer %s advertised-routes | no-more',
'received-routes' => 'display bgp routing-table peer %s received-routes',
'routes' => 'display bgp routing-table peer %s received-routes active',
'summary' => 'display bgp peer | no-more',
'ping' => 'ping -i LoopBack0 %s',
'trace' => 'tracert -i LoopBack0 %s',
),
'ipv6' => array
(
'bgp' => 'display bgp ipv6 routing-table %s',
'advertised-routes' => 'display bgp ipv6 routing-table peer %s advertised-routes | no-more',
'received-routes' => 'display bgp ipv6 routing-table peer %s received-routes',
'routes' => 'display bgp ipv6 routing-table peer %s received-routes active',
'summary' => 'display bgp ipv6 peer | no-more',
'ping' => 'ping ipv6 -a 2804:ff4:1::1 %s',
'trace' => 'tracert ipv6 -a 2804:ff4:1::1 %s',
)
),
);
if (isset($_CONFIG['routers'][$router]) AND
isset($queries[$_CONFIG['routers'][$router]['os']][$protocol]) AND
(isset($queries[$_CONFIG['routers'][$router]['os']][$protocol][$command]) OR $command == 'graph'))
{
if ($protocol == 'ipv6' AND (!isset($_CONFIG['routers'][$router]['ipv6']) OR
$_CONFIG['routers'][$router]['ipv6'] !== TRUE))
{
$protocol = 'ipv4';
print '<div class="center"><p class="warning">The router does not support IPv6. Using IPv4.</p></div>';
print '<hr>';
}
$url = $_CONFIG['routers'][$router]['url'];
if (($command == 'ping' OR $command == 'trace') AND
isset($_CONFIG['routers'][$router]['pingtraceurl']) AND
$_CONFIG['routers'][$router]['pingtraceurl'] != FALSE)
{
$url = $_CONFIG['routers'][$router]['pingtraceurl'];
}
$url = @parse_url($url);
$os = $_CONFIG['routers'][$router]['os'];
if ($command == 'graph' AND isset($queries[$os][$protocol]['bgp']))
{
$exec = $queries[$os][$protocol]['bgp'];
}
else
{
$exec = $queries[$os][$protocol][$command];
}
if (strpos($exec, '%s') !== FALSE)
{
if (preg_match('/^.[.a-z0-9_-]+\.[a-z]+$/i', $query))
{
if ($command != 'advertised-routes')
{
$query = get_host($query);
}
}
if ($query AND ($command == 'bgp' OR $command == 'graph') AND ($os == 'mikrotik' OR ($protocol == 'ipv6' AND $os == 'ios')))
{
if (strpos($query, '/') === FALSE AND $radb = get_radb($query))
{
$route = FALSE;
if (strpos($query, ':') AND isset($radb['route6']))
{
$route = $radb['route6'];
}
else if (isset($radb['route']))
{
$route = $radb['route'];
}
if ($route)
{
if ($command != 'graph')
{
print '<p>Endereço <b>'.$query.'</b> convertido em uma sub-rede <b>'.$route.'</b> usando dados de <a href="http://radb.net/" target="_blank">Merit RADb</a></p>';
}
$query = $route;
}
}
}
$exec = sprintf($exec, escapeshellcmd($query));
if (!$query)
{
$exec = FALSE;
if ($query === FALSE)
{
print '<div class="center"><p class="error">Can\'t resolve the hostname.</p></div>';
}
else
{
print '<div class="center"><p class="error">Parameter missing.</p></div>';
}
}
}
else if ($query != '' AND $command != 'graph')
{
print '<div class="center"><p class="warning">No parameter needed.</p></div>';
print '<hr>';
}
if ($exec)
{
if ($os == 'junos')
{
// @see JunOS Routing Table Names (http://www.net-gyver.com/?p=602)
$table = ($protocol == 'ipv6') ? 'inet6.0' : 'inet.0';
if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+)$/", $exec, $exec_exp))
{
$exec = 'show bgp neighbor '.$exec_exp[1];
}
else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) ro\w*$/", $exec, $exec_exp))
{
$exec = 'show route receive-protocol bgp '.$exec_exp[1];
}
else if (preg_match("/^show bgp neighbors ([\d\.A-Fa-f:]+) routes all$/", $exec, $exec_exp))
{
$exec = 'show route receive-protocol bgp '.$exec_exp[1].' all';
}
else if (preg_match("/^show bgp neighbors ([\d\.A-Fa-f:]+) routes damping suppressed$/", $exec, $exec_exp))
{
$exec = 'show route receive-protocol bgp '.$exec_exp[1].' damping suppressed';
}
else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) advertised-routes ([\d\.A-Fa-f:\/]+)$/", $exec, $exec_exp))
{
$exec = 'show route advertising-protocol bgp '.$exec_exp[1].' '.$exec_exp[2].' exact detail';
}
else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) receive-protocol ([\d\.A-Fa-f:\/]+)$/", $exec, $exec_exp))
{
$exec = 'show route receive-protocol bgp '.$exec_exp[1].' '.$exec_exp[2].' exact detail';
}
else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) a[\w\-]*$/", $exec, $exec_exp))
{
$exec = 'show route advertising-protocol bgp '.$exec_exp[1];
}
else if (preg_match("/^show bgp\s+([\d\.A-Fa-f:]+\/\d+)$/", $exec, $exec_exp))
{
$exec = 'show route protocol bgp '.$exec_exp[1].' terse exact';
}
else if (preg_match("/^show bgp\s+([\d\.A-Fa-f:]+)$/", $exec, $exec_exp))
{
$exec = 'show route protocol bgp '.$exec_exp[1].' table '.$table;
}
else if (preg_match("/^show bgp\s+([\d\.A-Fa-f:\/]+) exact$/", $exec, $exec_exp))
{
$exec = 'show route protocol bgp '.$exec_exp[1].' exact detail all';
}
else if (preg_match("/^show bgp re\s+(.*)$/", $exec, $exec_exp))
{
$re = $exec_exp[1];
if (!preg_match('/^\^/', $re))
{
$re = "^.*".$re;
}
if (!preg_match('/\$$/', $re))
{
$re = $re.".*\$";
}
$exec = 'show route aspath-regex "'.str_replace('_', ' ', $re).'" all';
}
}
if ($command == 'graph')
{
$ER = error_reporting(0);
@include 'Image/GraphViz.php';
if (!class_exists('Image_GraphViz'))
{
print '<div class="center"><p class="error">Class Image_GraphViz not found!</p></div>';
}
else
{
if (isset($_REQUEST['render']))
{
$format = ($_REQUEST['render'] == 'png') ? 'png' : 'svg';
if ($output = process($url, $exec, TRUE) AND $as_bgp_path = parse_bgp_path($output))
{
$as_best_path = $as_bgp_path['best'];
$as_pathes = $as_bgp_path['pathes'];
if (sizeof($as_pathes) < 1)
{
get_blank_graph('Not found BGP information of request.', $format);
}
get_path_graph($router, $query, $as_pathes, $as_best_path, $format);
}
get_blank_graph('Unable to get BGP information.', $format);
}
?>
<div class="center">
<p>Gráfico de roteamento BGP para <b><?php print $query ?></b>, router: <b><?php print $_CONFIG['routers'][$router]['description'] ?></b></p>
<p><a href="?command=bgp&protocol=<?php print $protocol ?>&query=<?php print $query ?>&router=<?php print $router ?>">Execute o comando bgp neste roteador</a></p>
<table border="0" class="legend">
<tr><td bgcolor="#CCCCFF" width="15"> </td><td>Upstream AS</td><td width="80"> </td><td bgcolor="#CCFFCC" width="15"> </td><td>Peering AS</td><td width="80"> </td><td bgcolor="white"><div style="height:12px;width:37px;background-image:url('data:image/gif;base64,R0lGODlhJQAMAOcAAAQCBISChJQ2NMTCxERCRcSCTGRiZYRWNEw2HKSipOTi5CQiJWRCJORCROyiXPzClISGnFRSVXRydKyi5DQyNRQSFJSSqdwCBPTy99xqlNTS1PwiJNSGvMSm7FxGRGxijIR+fPzk5JySzPSKVLS0tCQlNFxSdCQXDERCXOTC3LS21Dw6PSwCBNQeHAwKDPxSLHxyo+x2hNTS9MTG5KRyROzs7JSGwRwaHeyitPzU1KSivGxsbGRagVxaXXx8fLSq8ZycnPw0NOSaXLyCTExKTrxqbHwiJPylpDQzRPz+/GxuhCwqN4yOjJRkPEQ+WeTk/CwsLPRCTKSa3PwUFBQOCtza/Py0tNSPVGxKLIyKn1RVZBQWHPwDBPz09PxkZPybnIR3rFxcbExKZPzExDw6VPx8fNzO/KyqxJyatPQeLBQCBMzMzERGVGRldFw+JDQiFHRyhNzb3PQqPPTu/PyEhLy8vLyy/AwNFPxVVMzK6pSOxBweLOza7Lyq9HxunDQuQfxMTLwmJPzavPyUlMyb3JxaXLx+TFRKbCQeLOxufKysrPSuvORKZMSy/OTO7Hx+lPwKDIyCt/x0dPzc3Pw8PPysrPy8vPxsbPzNzPyMjPxaXIyKjMyOzHRqlcyKVCweFLy+3JSWlNyWW4R+tJxqPMTC3LSyzFw6PPwaHPwsLPzs7AwGBFQ5JGxGLPSiYLSi5hwSDKSV1ExCXLy61tzW/Dw1TJSSlOzi9PxERKya3BwWJMQqLISClNTW5CwmNKx2ROzs/PSmtGxsfHx7jJyarOyeXJxmPHRNLFxafExKbJyOzIx+fPR2hIx+tAQGBERGRWRmZOTm5CQmJWRGLPzGnFRWVDQ2NZSWrPT298Sq9GxmjlxWeTw+PQwODNTW9pSKxBweHaSmvWRehFxeXExOTCwuNNze/IyOpExOXDw+TKyuypyetXR2iMzO7Lyu+FROcPwODHRunPweHfSmZLSm7ExGZOzm/Kye44SGhMTGxIRaNKSmpHR2dBQWFNTW1PwmJCwAAAAAJQAMAAAIagCTCBxIsKBBVQYTKlxo8AsXL5YYSpzokAuXKZIwTdxYsKJFi/LK5OC40ePHj6kyhRh4pKXLlzBjXjpJ82OQRElq6tw5ZWfNKCQb+rQYZFDQhCZPpqIz6ajCpCFHOl1YccqlMVMnHoG4MSAAOw==')"></div></td><td>Melhor rota</td></tr>
</table>
<br>
<div id="loading" style="display:inline"><p><b>Please wait...</b></p></div>
<!--[if IE]>
<p><img src="?command=graph&protocol=<?php print $protocol ?>&query=<?php print $query ?>&router=<?php print $router ?>&render=png" alt="" title=""></p>
<![endif]-->
<![if ! IE]>
<object data="?command=graph&protocol=<?php print $protocol ?>&query=<?php print $query ?>&router=<?php print $router ?>&render=true" type="image/svg+xml"></object>
<![endif]>
<br>
</div>
<?php
}
error_reporting($ER);
}
else
{
print '<p><b>Router:</b> '.$_CONFIG['routers'][$router]['description'].'<br><b>Command:</b> '.$exec.'</p><pre><code>';
flush();
process($url, $exec);
print '</code></pre>';
}
}
}
else
{
$routers = group_routers($_CONFIG['routers']);
// HTML form
?>
<form method="get" action="">
<div class="center">
<table class="form" cellpadding="2" cellspacing="2">
<tr><th>Tipo de consulta</th><th>Parâmetros adicionais</th><th>Router</th></tr>
<tr><td>
<table border="0" cellpadding="2" cellspacing="2">
<tr><td><input type="radio" name="command" id="bgp" value="bgp" checked="checked"></td><td><label for="bgp">bgp</label></td></tr>
<tr><td><input type="radio" name="command" id="advertised-routes" value="advertised-routes"></td><td><label for="advertised-routes">bgp advertised-routes</label></td></tr>
<tr><td><input type="radio" name="command" id="summary" value="summary"></td><td><label for="summary">bgp summary</label></td></tr>
<tr><td><input type="radio" name="command" id="graph" value="graph"></td><td><label for="graph">bgp graph</label></td></tr>
<tr><td><input type="radio" name="command" id="trace" value="trace"></td><td><label for="trace">traceroute</label></td></tr>
<tr><td><input type="radio" name="command" id="ping" value="ping"></td><td><label for="ping">ping</label></td></tr>
<tr><td></td><td style="padding-top:10px">
<select name="protocol">
<option value="ipv4">IPv4</option>
<option value="ipv6">IPv6</option>
</select></td></tr>
</table></td>
<td align="center"><input name="query" size="30"></td>
<td align="right">
<select name="router" style="min-width: 180px">
<?php foreach ($routers as $group => $group_data): ?>
<?php if ($group != ''): ?>
<optgroup label="<?php print htmlspecialchars($group) ?>">
<?php endif ?>
<?php foreach ($group_data as $router_id => $router_data): ?>
<option value="<?php print $router_id ?>"><?php print htmlspecialchars($router_data['description']) ?></option>
<?php endforeach ?>
<?php if ($group != ''): ?>
</optgroup>
<?php endif ?>
<?php endforeach ?>
</select></td></tr>
<tr><td align="center" colspan="3"><p><input type="submit" value="Enviar"> | <input type="reset" value="Limpar"></p></td></tr>
</table>
</div>
</form>
<?php
}
// HTML footer
?>
<hr>
<div class="center">
<p><small>Mais informações: <a href="https://stat.ripe.net/AS<?php print $_CONFIG['asn'] ?>" target="_blank">RIPEstat</a> <a href="http://bgp.he.net/AS<?php print $_CONFIG['asn'] ?>" target="_blank">he.net</a> <a href="https://www.robtex.com/as/AS<?php print $_CONFIG['asn'] ?>.html" target="_blank">robtex.com</a> <a href="http://www.peeringdb.com/view.php?asn=<?php print $_CONFIG['asn'] ?>" target="_blank">PeeringDB</a> <a href="https://rdap.registro.br/autnum/<?php print $_CONFIG['asn'] ?>" target="_blank">RDAP registro.br</a></small></p>
<p>Copyright © <?php print date('Y') ?> <?php print htmlspecialchars($_CONFIG['company']) ?></p>
</div>
</body>
</html>
<?php
// ------------------------------------------------------------------------
/**
* Execute command and print output
*/
function process($url, $exec, $return_buffer = FALSE)
{
global $_CONFIG, $router, $protocol, $os, $command, $query, $ros;
$buffer = '';
$lines = $line = $is_exception = FALSE;
$index = 0;
$str_in = array();
switch ($url['scheme'])
{
case 'ssh':
switch ($_CONFIG['sshcommand'])
{
// Use sshpass command
case 'sshpass':
$ssh_path = $_CONFIG['sshpass'];
$params = array();
if (isset($url['pass']) AND $url['pass'] != '')
{
$params[] = '-p '.$url['pass'];
}
$params[] = 'ssh';
if (isset($url['user']) AND $url['user'] != '')
{
$params[] = '-l '.$url['user'];
}
if (isset($url['port']) AND $url['port'] != '')
{
$params[] = '-p '.$url['port'];
}
$params[] = '-o StrictHostKeyChecking=no';
break;
// Use plink command
case 'plink':
default:
$ssh_path = $_CONFIG['plink'];
$params = array('-ssh');
if (isset($url['user']) AND $url['user'] != '')
{
$params[] = '-l '.$url['user'];
}
if (isset($url['pass']) AND $url['pass'] != '')
{
$params[] = '-pw '.$url['pass'];
}
if (isset($url['port']) AND $url['port'] != '')
{
$params[] = '-P '.$url['port'];
}
}
$params[] = $url['host'];
$exec = escapeshellcmd($exec)."\n";
// Get MikroTik additional summary information
if (preg_match('/^\/routing bgp peer print status/i', $exec) AND $os == 'mikrotik' AND $return_buffer != TRUE)
{
if ($instance = @shell_exec('echo n | '.$ssh_path.' '.implode(' ', $params).' /routing bgp instance print'))
{
$instance_list = parse_list($instance);
print 'BGP router identifier '.$instance_list['router-id'].', local AS number '.link_as($instance_list['as'])."\n";
}
}
// Get MikroTik version for traceroute
if (preg_match('/^\/tool traceroute/i', $exec) AND $os == 'mikrotik' AND $return_buffer != TRUE)
{
if ($instance = @shell_exec('echo n | '.$ssh_path.' '.implode(' ', $params).' /system resource print'))
{
if (preg_match('/version: (\d+)/', $instance, $ver))
{
if (isset($ver[1]))
{
if ($ver[1] == 6)
{
$exec .= ' count=1';
}
$ros = $ver[1];
}
}
}
$exec .= "\n";
}
// Huawei disable screen breaks (issue #21) -- needs more tests
/*if ($os == 'huawei')
{
@shell_exec('echo n | '.$ssh_path.' '.implode(' ', $params).' screen-length 0 temporary');
}*/
if ($fp = @popen('echo n | '.$ssh_path.' '.implode(' ', $params).' '.$exec, 'r'))
{
while (!feof($fp))
{
if (!$output = fgets($fp, 1024))
{
continue;
}
$line = !$return_buffer ? parse_out($output, TRUE) : $output;
if ($line === TRUE)
{
if (!$return_buffer)
{
print '<p class="error">Command aborted.</p>';
}
break;
}
if ($line === FALSE OR $return_buffer === TRUE)
{
$lines .= $output;
continue;
}
print $line;
flush();
if ($line === NULL)
{
$line = $output;
}
}
pclose($fp);
}
if (!$line)
{
print '<p class="error">Command failed.</p>';
}
break;
case 'telnet':
if (!isset($url['port']) OR $url['port'] == '')
{
$url['port'] = 23;
}
if (!isset($url['user']) OR $url['user'] == '')
{
$url['user'] = FALSE;
}
if (!isset($url['pass']) OR $url['pass'] == '')
{
$url['pass'] = FALSE;
}
try
{
if ($os == 'mikrotik')
{
$url['user'] .= '+ct';
$prompt = '/[^\s]{2,} [>]/';
}
else
{
$prompt = '/[^\s]{2,}[\$%>] {0,1}$/';
}
$exec .= "\n";
$telnet = new Telnet($url['host'], $url['port'], 10, $prompt);
$telnet->connect();
$telnet->login($url['user'], $url['pass']);
// Huawei disable screen breaks (issue #21) -- needs more tests
/*if ($os == 'huawei')
{
$telnet->write('screen-length 0 temporary');
}*/
$telnet->write(($os == 'junos') ? $exec.' | no-more' : $exec);
$i = $j = 0;
do
{
$c = $telnet->getc();
if ($c === false)
{
break;
}
if ($c == $telnet->IAC)
{
if ($telnet->negotiateTelnetOptions())
{
continue;
}
}
$buffer .= $c;
// Clear buffer berofe backspace or escape notice
if ($c == "\x08" OR $buffer == "Type escape sequence to abort.\r\n")
{
$buffer = '';
continue;
}
//$c = preg_replace('/\[\d;?(\d+)?;?(\d)?m/x', ' ', $c); // Strip remaining
//$c = preg_replace('/\x1B\x5B\x30\x6D/x', '\x0A', $c); // Convert to \n
$buffer = preg_replace('/[\x80-\xFF]/x', ' ', $buffer); // Strip Ext ASCII
$buffer = preg_replace('/[\x00-\x09]/x', ' ', $buffer); // Strip Low ASCII // \x0B-\x1F
$buffer = preg_replace('/\x1b\x5b;?([^\x6d]+)?\x6d/x', '', $buffer); // Strip colors
if (preg_match($prompt, substr($buffer, -4)))
{
if ($j < 2)
{
$j++;
if (substr($buffer, -2) != "\r\n")
{
$telnet->write();
$i = 0;
continue;
}
}
$telnet->disconnect();
break;
}
if (substr($buffer, -10) == ' --More-- ')
{
$telnet->write();
}
else if (substr($buffer, -2) == "\r\n")
{
// Clear buffer on first empty line
if ($i == 1 AND $buffer == "\r\n")
{
$buffer = '';
}
// JunOS
if (strpos($buffer, '---(more)---') !== FALSE)
{
$buffer = ltrim(str_replace('---(more)---', '', $buffer), "\r\n");
}
$i++;
if ($i > 1)
{
$line = !$return_buffer ? parse_out($buffer, TRUE) : $buffer;
if ($line === TRUE)
{
if (!$return_buffer)
{
print '<p class="error">Command aborted.</p>';
}
$telnet->disconnect();
break;
}
if ($line === FALSE OR $return_buffer === TRUE)
{
$lines .= $buffer;
}
else
{
print $line;
flush();
if ($line === NULL)
{
$line = $buffer;
}
}
}
$buffer = '';
}
}
while ($c != $telnet->NULL OR $c != $telnet->DC1);
if (!$line)
{
print '<p class="error">Command failed.</p>';
}
}
catch (Exception $exception)
{
$is_exception = TRUE;
if (!$return_buffer)
{
print '<p class="error">Telnet error: '.$exception->getMessage().'</p>';
}
}
break;
}
if ($lines)
{
if ($return_buffer)
{
return $lines;
}
if ($line = parse_out($lines))
{
print $line;
}
}
flush();
}
/**
* Parse output contents
*/
function parse_out($output, $check = FALSE)
{
global $_CONFIG, $router, $protocol, $os, $command, $exec, $query, $index, $lastip, $best, $count, $str_in, $ros;
$output = str_replace("\r\n", "\n", $output);
// MikroTik
if (preg_match("/^\/(ip|ipv6) route print detail/i", $exec) AND $os == 'mikrotik')
{
if ($check)
{
return FALSE;
}
$output_parts = explode("\n" , trim($output), ($protocol != 'ipv6' ? 4 : 3));
if (!isset($output_parts[($protocol != 'ipv6' ? 3 : 2)]))
{
return 'Records for '.strip_tags($query).' is not found';
}
$summary_parts = explode("\n\n" , $output_parts[($protocol != 'ipv6' ? 3 : 2)]);
$output = implode("\n", array_slice($output_parts, 0, ($protocol != 'ipv6' ? 3 : 2)))."\n\n";
foreach ($summary_parts as $summary_part)
{
$data_exp = explode(' ', trim($summary_part), 3);
$summary_part = preg_replace_callback(
"/bgp-as-path=\"([^\"]+)\"/x",
function ($matches) {
return stripslashes('bgp-as-path=\"'.link_as($matches[1]).'\"');
},
$summary_part
);
if (strpos($data_exp[1], 'A') !== FALSE)
{
$output .= '<span style="color:#ff0000">'.$summary_part."</span>\n\n";
}
else
{
$output .= $summary_part."\n\n";
}
}
return $output;
}
// MikroTik
if (preg_match("/^\/routing bgp advertisements print/i", $exec) AND $os == 'mikrotik')
{
return preg_replace_callback(
"/^(.{8}\s)([\d\.A-Fa-f:\/]+)(\s+)/",
function ($matches) {
return $matches[1].link_command("bgp", $matches[2]).$matches[3];
},
$output
);
}
// MikroTik
if (preg_match("/^\/(ip|ipv6) route print/i", $exec) AND $os == 'mikrotik')
{
return preg_replace_callback(
"/(^[\s\d]+)(\s+[A-z]+\s+)([\d\.A-Fa-f:\/]+)(\s+)/",
function ($matches) {
return $matches[1].$matches[2].link_command("bgp", $matches[3]).$matches[4];
},
$output
);
}
// MikroTik
if (preg_match("/^\/routing bgp peer print status/i", $exec) AND $os == 'mikrotik')
{
if ($check)
{
return FALSE;
}
$output_parts = explode("\n" , trim($output), 2);
if (!isset($output_parts[1]))
{
return 'Records for '.strip_tags($query).' is not found';
}
$summary_parts = explode("\n\n" , $output_parts[1]);
$table_array[] = array
(
'remote-address' => 'Neighbor',
'name' => 'PeerName',
'remote-id' => 'RemoteID',
'remote-as' => 'AS',
'withdrawn-received' => 'MsgRcvd',
'updates-received' => 'MsgSent',
'uptime' => 'Up/Down',
'state' => 'State',
'prefix-count' => 'PfxRcd',
'updates-sent' => 'PfxSnd',