From ec411aa73eafae30a8d35016f417341162ca471d Mon Sep 17 00:00:00 2001
From: root The router does not support IPv6. Using IPv4. Address '.$query.' is converted to a subnet '.$route.' using the Merit RADb Can\'t resolve the hostname. Parameter missing. No parameter needed. Class Image_GraphViz not found! BGP routing graph for , router: Run a bgp command on this router Please wait...AS Looking Glass
+ 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=%s',
+ 'bgp-within' => '/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',
+ 'bgp-within' => '/ip route print detail where bgp dst-address in %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',
+ 'bgp-within' => 'display bgp routing-table community | include %s',
+ 'received-routes' => 'display bgp routing-table peer %s received-routes',
+ 'routes' => 'display bgp routing-table peer %s received-routes active',
+ 'summary' => 'display bgp peer',
+ 'ping' => 'ping %s',
+ 'trace' => 'tracert %s',
+ ),
+ 'ipv6' => array
+ (
+ 'bgp' => 'display bgp ipv6 routing-table %s',
+ 'advertised-routes' => 'display bgp ipv6 routing-table peer %s advertised-routes',
+ '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',
+ 'ping' => 'ping ipv6 %s',
+ 'trace' => 'tracert ipv6 %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 '
';
+ }
+
+ $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);
+
+ $routing_instance = $_CONFIG['routers'][$router]['routing-instance'];
+
+ $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 == 'bgp-within' 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 '
';
+ }
+
+ if ($exec)
+ {
+ if ($os == 'junos')
+ {
+ // @see JunOS Routing Table Names (http://www.net-gyver.com/?p=602)
+ if ($routing_instance){
+ $table = ($protocol == 'ipv6') ? $routing_instance.'.inet6.0' : $routing_instance.'.inet.0';
+ }else{
+ $table = ($protocol == 'ipv6') ? 'inet6.0' : 'inet.0';
+ }
+
+ if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+)$/", $exec, $exec_exp))
+ {
+ if ($routing_instance){
+ $exec = 'show bgp neighbor instance '.$routing_instance.' '.$exec_exp[1];
+ }else{
+ $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]." table ".$table;
+ }
+ 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].' table '.$table.' 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].' table '.$table.' 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].' table '.$table.' 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].' table '.$table.' 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].' table '.$table;
+ }
+ else if (preg_match("/^show bgp\s+([\d\.A-Fa-f:]+\/\d+)$/", $exec, $exec_exp))
+ {
+ $exec = 'show route protocol bgp '.$exec_exp[1].' table '.$table.' 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].' table '.$table.' 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.".*\$";
+ }
+
+ if ($routing_instance){
+ $exec = 'show route instance '.$routing_instance.' aspath-regex "'.str_replace('_', ' ', $re).'" all';
+ }else{
+ $exec = 'show route aspath-regex "'.str_replace('_', ' ', $re).'" all';
+ }
+ }
+ else if (preg_match("/^show bgp summary$/", $exec, $exec_exp))
+ {
+ if ($routing_instance){
+ $exec = 'show bgp summary instance '.$routing_instance;
+ }else{
+ $exec = 'show bgp summary';
+ }
+ }
+ else if (preg_match("/^show route advertising-protocol bgp\s+([\d\.A-Fa-f:\/]+)$/", $exec, $exec_exp))
+ {
+ $exec = 'show route advertising-protocol bgp '.$exec_exp[1].' table '.$table;
+ }
+ else if (preg_match("/^traceroute (ip|ipv6) ([\d\.A-Fa-f:\/]+)$/", $exec, $exec_exp))
+ {
+ if ($routing_instance){
+ $exec = 'traceroute '.$exec_exp[1].' routing-instance '.$routing_instance.' '.$exec_exp[2];
+ }else{
+ $exec = 'traceroute '.$exec_exp[1].' '.$exec_exp[2];
+ }
+ }
+
+ }
+
+ if ($command == 'graph')
+ {
+ $ER = error_reporting(0);
+
+ @include 'Image/GraphViz.php';
+
+ if (!class_exists('Image_GraphViz'))
+ {
+ print '
+
+
+ Upstream AS Peering AS Best route
+
+
Command: '.$exec.'
';
+ flush();
+
+ process($url, $exec);
+
+ print '';
+ }
+ }
+}
+else
+{
+ $routers = group_routers($_CONFIG['routers']);
+
+// HTML form
+?>
+
+
+ Information: RIPEstat he.net robtex.com PeeringDB
+Copyright ©
+Command failed.
'; + } + + 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 'Command aborted.
'; + } + + $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 (empty($lines) AND !$line) + { + print 'Command failed.
'; + } + } + catch (Exception $exception) + { + $is_exception = TRUE; + + if (!$return_buffer) + { + print 'Telnet error: '.$exception->getMessage().'
'; + } + } + + 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 .= ''.$summary_part."\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', + ); + + foreach ($summary_parts as $summary_part) + { + $data_exp = explode(' ', trim($summary_part), 3); + + if (sizeof($data_exp) != 3) + { + continue; + } + + $data = parse_list($data_exp[2]); + + $table_array[] = array + ( + 'remote-address' => isset($data['remote-address']) ? link_whois($data['remote-address']) : '', + 'name' => isset($data['name']) ? $data['name'] : '', + 'remote-id' => isset($data['remote-id']) ? $data['remote-id'] : '', + 'remote-as' => isset($data['remote-as']) ? link_as($data['remote-as']) : '', + 'withdrawn-received' => isset($data['withdrawn-received']) ? $data['withdrawn-received'] : '', + 'updates-received' => isset($data['updates-received']) ? $data['updates-received'] : '', + 'uptime' => isset($data['uptime']) ? $data['uptime'] : '', + 'state' => $data_exp[1], + 'prefix-count' => (isset($data['remote-address']) AND isset($data['prefix-count'])) + ? link_command('routes', $data['remote-address'], $data['prefix-count']) + : '', + 'updates-sent' => (isset($data['name']) AND isset($data['updates-sent'])) + ? link_command('advertised-routes', $data['name'], $data['updates-sent']) + : '', + ); + } + + return + str_replace('Flags', 'State', $output_parts[0])."\n\n". + build_table($table_array)."\n\n". + 'Total number of neighbors '.(sizeof($table_array) - 1); + } + + // MikroTik + if (preg_match("/^\/ping/i", $exec) AND $os == 'mikrotik') + { + if (preg_match('/^HOST/', $output) AND $index == 0) + { + $index++; + + return 'PING '.$query.' ('.get_ptr($query).'): 56 data bytes'."\n"; + } + + if ($index > 0) + { + $exp = explode(' ', preg_replace('/[\s\t]+/', ' ', trim($output))); + $exp = array_slice($exp, 0, 4); + + if (!$exp OR $exp[0] == '' OR $exp[0] == 'HOST' OR (isset($exp[1]) AND $exp[1] == 'timeout')) + { + return; + } + + if (preg_match('/sent=([0-9]+) received=([0-9]+) packet-loss=([^ ]+)/', $output, $stat)) + { + $return = "\n".'--- '.$query.' ping statistics ---'."\n". + $stat[1].' packets transmitted, '.$stat[2].' packets received, '.trim($stat[3]).' packet loss'."\n"; + + if (preg_match('/min-rtt=([0-9]+)ms avg-rtt=([0-9]+)ms/', $output, $stat2)) + { + $return .= 'round-trip min/avg/max = '.$stat2[1].'/'.$stat2[2]; + } + + if (preg_match('/max-rtt=([0-9]+)ms/', $output, $stat3)) + { + $return .= '/'.$stat3[1]; + } + + return $return; + } + + // Пинг больше 99, отдача max-rtt отдельной строкой + if (preg_match('/max-rtt=([0-9]+)ms/', $output, $stat3)) + { + $return = '/'.$stat3[1]; + + return $return; + } + + $index++; + + return $exp[1].' bytes from '.$exp[0].': icmp_seq='.intval($index-1).' ttl='.$exp[2].' time='.$exp[3]."\n"; + } + } + + // MikroTik + if (preg_match("/^\/tool trace/i", $exec) AND $os == 'mikrotik' AND $ros >= 6) + { + if (preg_match('/^ # ADDRESS/', $output) AND $index == 0) + { + $index++; + + return 'traceroute to '.$query.' ('.get_ptr($query).'), 64 hops max, 60 byte packets'."\n"; + } + + if ($index > 0) + { + $exp = explode(' ', preg_replace('/[\s\t]+/', ' ', trim($output))); + $exp = array_slice($exp, 0, 9); + + if (!$exp OR empty($exp[0]) OR (sizeof($exp) == 4 AND isset($exp[3]) AND $exp[3] != 'timeout')) + { + return; + } + + $new_exp[0] = (strlen($exp[0]) < 2) ? ' '.$exp[0] : $exp[0]; + + if ($exp[3] == 'timeout') + { + $new_exp[1] = '* * *'; + $new_exp = array_slice($new_exp, 0, 2); + } + else + { + $radb = get_radb($exp[1]); + + $new_exp[1] = get_ptr($exp[1]); + $new_exp[2] = '('.$exp[1].')'; + $new_exp[3] = '['.(isset($radb['origin']) ? 'AS '.link_as($radb['origin']) : '').']'; + $new_exp[4] = $exp[5].'ms'; + $new_exp[5] = $exp[6].'ms'; + $new_exp[6] = $exp[7].'ms'; + } + + if ($index >= 64) + { + return TRUE; + } + + $str = implode(' ', $new_exp)."\r\n"; + + if (@in_array($str, $str_in)) + { + return; + } + + $str_in[] = $str; + + $index++; + + return $str; + } + } + + // MikroTik + if (preg_match("/^\/tool trace/i", $exec) AND $os == 'mikrotik' AND $ros <= 5) + { + if (preg_match('/^ # ADDRESS/', $output) AND $index == 0) + { + $index++; + + return 'traceroute to '.$query.' ('.get_ptr($query).'), 64 hops max, 60 byte packets'."\n"; + } + + if ($index > 0) + { + $exp = explode(' ', preg_replace('/[\s\t]+/', ' ', trim($output))); + $exp = array_slice($exp, 0, 5); + + if (!$exp OR empty($exp[0])) + { + return; + } + + $exp[0] = (strlen($exp[0]) < 2) ? ' '.$exp[0] : $exp[0]; + + if ($exp[1] == '0.0.0.0' OR $exp[1] = '::') + { + $exp[1] = '* * *'; + $exp = array_slice($exp, 0, 2); + } + else + { + $radb = get_radb($exp[1]); + + $exp[6] = $exp[4]; + $exp[5] = $exp[3]; + $exp[4] = $exp[2]; + $exp[3] = '['.(isset($radb['origin']) ? 'AS '.link_as($radb['origin']) : '').']'; + $exp[2] = '('.$exp[1].')'; + $exp[1] = get_ptr($exp[1]); + } + + if ($index >= 64) + { + return TRUE; + } + + $index++; + + return implode(' ', $exp)."\r\n"; + } + } + + // Other OS parsers + if (preg_match('/^show bgp ipv6 unicast/i', $exec) AND $os = 'ios') + { + $output = str_replace('% Incomplete command.', 'Please use network prefix in Parameter.
', $output); + } + + if ($exec == 'show ip bgp summary') + { + $output = preg_replace_callback( + "/( local AS number )(\d+)/", + function ($matches) { + return $matches[1].link_as($matches[2]); + }, + $output + ); + + // JunOS + if ($os == 'junos') + { + $output = preg_replace_callback( + "/^([\dA-Fa-f:\.]+\s+)(\d+)/", + function ($matches) { + return $matches[1].link_as($matches[2]); + }, + $output + ); + } + else + { + $output = preg_replace_callback( + "/^([\d\.]+\s+\d+\s+)(\d+)/", + function ($matches) { + return $matches[1].link_as($matches[2]); + }, + $output + ); + } + + $output = preg_replace_callback( + "/^(\d+\.\d+\.\d+\.\d+)(\s+.*\s+)([1-9]\d*)\n$/", + function ($matches) { + return $matches[1].$matches[2].link_command("routes", $matches[1], $matches[3])."\n"; + }, + $output + ); + $output = preg_replace_callback( + "/^(\d+\.\d+\.\d+\.\d+)(\s+)/", + function ($matches) { + return link_command("bgp", "neighbors+".$matches[1], $matches[1]).$matches[2]; + }, + $output + ); + + // IPv6 neighbours + $output = preg_replace_callback( + "/^(.{15} 4\s+)(\d+)/", + function ($matches) { + return $matches[1].link_as($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/^([\dA-Fa-f]*:[\dA-Fa-f:]*)(\s+)/", + function ($matches) { + return link_command("bgp", "neighbors+".$matches[1], $matches[1]).$matches[2]; + }, + $output + ); + $output = preg_replace_callback( + "/^([\dA-Fa-f]*:[\dA-Fa-f:]*)\n$/", + function ($matches) { + return link_command("bgp", "neighbors+".$matches[1], $matches[1])."\n"; + }, + $output + ); + + return $output; + } + + if ($exec == 'show ipv6 bgp summary' OR $exec == 'show bgp ipv6 unicast summary') + { + $output = preg_replace_callback( + "/^(.{15} 4\s+)(\d+)/", + function ($matches) { + return $matches[1].link_as($matches[2]); + }, + $output + ); + + if (preg_match("/^([\dA-Fa-f]*:[\dA-Fa-f:]*)\s+4\s+/", $output, $lastip_exp)) + { + $lastip = $lastip_exp[1]; + + $output = preg_replace_callback( + "/^(.{15} 4\s+)(\d+)/", + function ($matches) { + return $matches[1].link_as($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/^([\dA-Fa-f:]+)(\s+.*\s+)([1-9]\d*)$/", + function ($matches) use ($lastip) { + return link_command("routes", $lastip, $matches[3]); + }, + $output + ); + $output = preg_replace_callback( + "/^([\dA-Fa-f:]+)(\s+)/", + function ($matches) { + return link_command("routes", $matches[1]).$matches[2]; + }, + $output + ); + + unset($lastip); + } + + if (preg_match("/^([\dA-Fa-f:]+)\n$/", $output, $lastip_exp)) + { + $lastip = $lastip_exp[1]; + + $output = preg_replace_callback( + "/^([\dA-Fa-f:]+)/", + function ($matches) { + return link_command("routes", $matches[1]); + }, + $output + ); + } + + if (isset($lastip) AND preg_match("/^(\s+.*\s+)([1-9]\d*)\n$/", $output)) + { + $output = preg_replace_callback( + "/^(\s+.*\s+)([1-9]\d*)\n$/", + function ($matches) use ($lastip) { + return $matches[1].link_command("routes", $lastip, $matches[2])."\n"; + }, + $output + ); + + unset($lastip); + } + + return $output; + } + + if (preg_match("/^show bgp summary(?:\s+instance\s+[\d\w-]*)?$/", $exec)) + { + // JunOS + if (preg_match("/^([\dA-Fa-f:][\d\.A-Fa-f:]+)\s+/", $output, $lastip_exp)) + { + $lastip = $lastip_exp[1]; + + // IPv4 + $output = preg_replace_callback( + "/^(\d+\.\d+\.\d+\.\d+)(\s+.*\s+)([1-9]\d*)(\s+\d+\s+\d+\s+\d+\s+\d+\s+[\d:]+\s+)(\d+)\/(\d+)\/(\d+)(\s+)/", + function ($matches) { + return $matches[1].$matches[2]. + link_command("routes", $matches[1], $matches[3]).$matches[4]. + link_command("routes", $matches[1], $matches[5])."/". + link_command("routes", $matches[1], $matches[6])."/". + link_command("routes", $matches[1], $matches[7]).$matches[8]; + }, + $output + ); + + // IPv4/IPv6 + $output = preg_replace_callback( + "/^([\dA-Fa-f:][\d\.A-Fa-f:]+\s+)(\d+)(\s+)/", + function ($matches) { + return $matches[1].link_as($matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/^([\dA-Fa-f:][\d\.A-Fa-f:]+)(\s+)/", + function ($matches) { + return link_command("bgp", "neighbors+".$matches[1], $matches[1]).$matches[2]; + }, + $output + ); + } + + if (isset($lastip) AND preg_match("/( [^:]+: )(\d+)\/(\d+)\/(\d+)$/", $output)) + { + $output = preg_replace_callback( + "/^( [^:]+: )(\d+)\/(\d+)\/(\d+)\n$/", + function ($matches) use ($lastip) { + return "\\1". + link_command("routes", $lastip, $matches[2])."/". + link_command("bgp", "neighbors+".$lastip."+routes+all", $matches[3])."/". + link_command("bgp", "neighbors+".$lastip."+routes+damping+suppressed", $matches[4])."\n"; + }, + $output + ); + + unset($lastip); + } + + return $output; + } + + if (preg_match("/^show ip bgp\s+n\w*\s+[\d\.]+\s+(ro|re|a)/i", $exec) OR + preg_match("/^show (ipv6 bgp|bgp ipv6)\s+n\w*\s+[\dA-Fa-f:]+\s+(ro|re|a)/i", $exec) OR + preg_match("/^show (ipv6 bgp|bgp ipv6)\s+re/i", $exec) OR + preg_match("/^show ip bgp\s+[\d\.]+\s+[\d\.]+\s+(l|s)/i", $exec) OR + preg_match("/^show (ip bgp|bgp ipv6) prefix-list/i", $exec) OR + preg_match("/^show (ip bgp|bgp ipv6) route-map/i", $exec)) + { + $output = preg_replace_callback( + "/^([\*r ](>|d|h| ).{59})([\d\s,\{\}]+)([ie\?])\n$/", + function ($matches) { + return $matches[1].link_as($matches[3]).$matches[4]."\n"; + }, + $output + ); + $output = preg_replace_callback( + "/^([\*r ](>|d|h| )[i ])([\d\.A-Fa-f:\/]+)(\s+)/", + function ($matches) { + return $matches[1].link_command("bgp", $matches[3]).$matches[4]; + }, + $output + ); + $output = preg_replace_callback( + "/^([\*r ](>|d|h| )[i ])([\d\.A-Fa-f:\/]+)\n$/", + function ($matches) { + return $matches[1].link_command("bgp", $matches[3])."\n"; + }, + $output + ); + $output = preg_replace_callback( + "/^(( ){20}.{41})([\d\s,\{\}]+)([ie\?])\n$/", + function ($matches) { + return $matches[1].link_as($matches[3]).$matches[4]."\n"; + }, + $output + ); + $output = preg_replace_callback( + "/(, remote AS )(\d+)(,)/", + function ($matches) { + return $matches[1].link_as($matches[2]).$matches[3]."\n"; + }, + $output + ); + + return $output; + } + + // JunOS + if (preg_match("/^show route receive-protocol bgp\s+[\d\.A-Fa-f:]+/i", $exec)) + { + $output = preg_replace_callback( + "/(Community: )([\d: ]+)/", + function ($matches) { + return $matches[1].link_community($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(Communities: )([\d: ]+)/", + function ($matches) { + return $matches[1].link_community($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(^\s+AS path: )([\d ]+)/", + function ($matches) { + return $matches[1].link_as($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/^([\d\.\s].{24})([\d\.]+)(\s+)/", + function ($matches) { + return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/^([\d\.\/]+)(\s+)/", + function ($matches) { + return link_command("bgp", $matches[1]).$matches[2]; + }, + $output + ); + $output = preg_replace_callback( + "/^([\d\.A-Fa-f:\/]+)(\s+)/", + function ($matches) { + return link_command("bgp", $matches[1]."+exact", $matches[1]).$matches[2]; + }, + $output + ); + $output = preg_replace_callback( + "/^([\d\.A-Fa-f:\/]+)\s*\n$/", + function ($matches) { + return link_command("bgp", $matches[1]."+exact", $matches[1]); + }, + $output + ); + $output = preg_replace_callback( + "/^([ \*] )([\d\.A-Fa-f:\/]+)(\s+)/", + function ($matches) { + return $matches[1].link_command("bgp", $matches[2]."+exact", $matches[2]).$matches[3]; + }, + $output + ); + + return $output; + } + + // JunOS + if (preg_match("/^show route advertising-protocol bgp\s+([\d\.A-Fa-f:]+)(?:\s+table\s+[\d\w\-\.]*)?$/i", $exec, $ip_exp)) + { + $ip = $ip_exp[1]; + + $output = preg_replace_callback( + "/^([\d\.\s].{64})([\d\s,\{\}]+)([I\?])\n$/", + function ($matches) { + return $matches[1].link_as($matches[2]).$matches[3]."\n"; + }, + $output + ); + $output = preg_replace_callback( + "/^([\d\.\s].{24})([\d\.]+)(\s+)/", + function ($matches) { + return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/^([\d\.\/]+)(\s+)/", + function ($matches) { + return link_command("bgp", $matches[1]).$matches[2]; + }, + $output + ); + $output = preg_replace_callback( + "/^([\d\.A-Fa-f:\/]+)(\s+)/", + function ($matches) { + return link_command("bgp", $matches[1]."+exact", $matches[1]).$matches[2]; + }, + $output + ); + $output = preg_replace_callback( + "/^([\d\.A-Fa-f:\/]+)\s*\n$/", + function ($matches) { + return link_command("bgp", $matches[1]."+exact", $matches[1])."\n"; + }, + $output + ); + $output = preg_replace_callback( + "/^([ \*] )([\d\.A-Fa-f:\/]+)(\s+)/", + function ($matches) use ($ip) { + return $matches[1].link_command("bgp", "neighbors+".$ip."+advertised-routes+".$matches[2], $matches[2]).$matches[3]; + }, + $output + ); + + return $output; + } + + if (preg_match("/^show ip bgp n\w*\s+([\d\.]+)/i", $exec) OR + preg_match("/^show ip bgp n\w*$/i", $exec)) + { + if (!isset($lastip) AND preg_match("/^BGP neighbor is ([\d\.]+)(,?)/", $output, $lastip_exp)) + { + $lastip = $lastip_exp[1]; + } + + $output = preg_replace_callback( + "/(Prefix )(advertised)( [1-9]\d*)/", + function ($matches) use ($lastip) { + return $matches[1].link_command("advertised-routes", $lastip, $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/( Prefixes Total: )(\d+)( )/", + function ($matches) use ($lastip) { + return $matches[1].link_command("advertised-routes", $lastip, $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/(prefixes )(received)( [1-9]\d*)/", + function ($matches) use ($lastip) { + return $matches[1].link_command("routes", $lastip, $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/( Prefixes Current: \s+)(\d+)(\s+)(\d+)/", + function ($matches) use ($lastip) { + return $matches[1]. + link_command("advertised-routes", $lastip, $matches[2]).$matches[3]. + link_command("routes", $lastip, $matches[4]); + }, + $output + ); + $output = preg_replace_callback( + "/(\s+)(Received)( prefixes:\s+[1-9]\d*)/", + function ($matches) use ($lastip) { + return $matches[1].link_command("routes", $lastip, $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/( Saved \(soft-reconfig\):\s+)(\d+|n\/a)(\s+)(\d+)/", + function ($matches) use ($lastip) { + return $matches[1].$matches[2].$matches[3].link_command("received-routes", $lastip, $matches[4]); + }, + $output + ); + $output = preg_replace_callback( + "/( [1-9]\d* )(accepted)( prefixes)/", + function ($matches) use ($lastip) { + return $matches[1].link_command("routes", $lastip, $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/^( [1-9]\d* )(accepted|denied but saved)( prefixes consume \d+ bytes)/", + function ($matches) use ($lastip) { + return $matches[1].link_command("received-routes", $lastip, $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/^(BGP neighbor is )(\d+\.\d+\.\d+\.\d+)(,?)/", + function ($matches) { + return $matches[1].link_whois($matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace("/^( Description: )(.*)$/", '\\1\\2', $output); + $output = preg_replace_callback( + "/(,\s+remote AS )(\d+)(,)/", + function ($matches) { + return $matches[1].link_as($matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/(, local AS )(\d+)(,)/", + function ($matches) { + return $matches[1].link_as($matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/( update prefix filter list is\s+:?\*?)(\S+)/", + function ($matches) { + return $matches[1].link_command("bgp", "prefix-list+".$matches[2], $matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(Route map for \S+ advertisements is\s+:?\*?)(\S+)/", + function ($matches) { + return $matches[1].link_command("bgp", "route-map+".$matches[2], $matches[2]); + }, + $output + ); + + return $output; + } + + if (preg_match("/^show (ipv6 bgp|bgp ipv6) n\w*\s+([\dA-Fa-f:]+)/i", $exec, $ip_exp)) + { + $ip = $ip_exp[1]; + + $output = preg_replace_callback( + "/(Prefix )(advertised)( [1-9]\d*)/", + function ($matches) use ($ip) { + return $matches[1].link_command("advertised-routes", "'.$ip.'", $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/( [1-9]\d* )(accepted)( prefixes)/", + function ($matches) use ($ip) { + return $matches[1].link_command("routes", $ip, $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace("/^( Description: )(.*)$/", '\\1\\2', $output); + $output = preg_replace_callback( + "/(\s+remote AS )(\d+)(,)/", + function ($matches) { + return $matches[1].link_as($matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/(\s+local AS )(\d+)(,)/", + function ($matches) { + return $matches[1].link_as($matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/( update prefix filter list is\s+:?\*?)(\S+)/", + function ($matches) { + return $matches[1].link_command("bgp", "prefix-list+".$matches[2], $matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(Route map for \S+ advertisements is\s+:?\*?)(\S+)/", + function ($matches) { + return $matches[1].link_command("bgp", "route-map+".$matches[2], $matches[2]); + }, + $output + ); + + return $output; + } + + if (preg_match("/^show bgp n\w*\s+(?:instance\s+[\d\w-]*)?\s*([\d\.A-Fa-f:]+)/i", $exec, $ip_exp)) + { + $ip = $ip_exp[1]; + + $output = preg_replace_callback( + "/(\s+AS )(\d+)/", + function ($matches) { + return $matches[1].link_as($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(\s+AS: )(\d+)/", + function ($matches) { + return $matches[1].link_as($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/^( Active prefixes:\s+)(\d+)/", + function ($matches) use ($ip) { + return $matches[1].link_command("routes", $ip, $matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/^( Received prefixes:\s+)(\d+)/", + function ($matches) use ($ip) { + return $matches[1].link_command("bgp", "neighbors+".$ip."+routes+all", $matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/^( Advertised prefixes:\s+)(\d+)/", + function ($matches) use ($ip) { + return $matches[1].link_command("advertised-routes", $ip, $matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/^( Suppressed due to damping:\s+)(\d+)/", + function ($matches) use ($ip) { + return $matches[1].link_command("bgp", "neighbors+".$ip."+routes+damping+suppressed", $matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/^( )(Export)(: )/", + function ($matches) use ($ip) { + return $matches[1].link_command("advertised-routes", $ip, $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/( )(Import)(: )/", + function ($matches) use ($ip) { + return $matches[1].link_command("bgp", "neighbors+".$ip."+routes+all", $matches[2]).$matches[3]; + }, + $output + ); + + return $output; + } + + // JunOS + if (preg_match("/^show route protocol bgp .* terse/i", $exec)) + { + if (preg_match("/^([\+\-\*]){1}/", $output, $exp) AND strpos($output, 'Active Route') === FALSE) + { + if ($exp[1] == '*' OR $exp[1] == '+') + { + $best = "#ff0000"; + } + else if ($exp[1] == '-') + { + $best = "#008800"; + } + else + { + $best = ''; + } + } + + if (isset($best) AND $best != '') + { + $output = ''.$output.''; + } + + $output = preg_replace_callback( + "/^([\* ] )([\d\.A-Fa-f:\/]+)(\s+)/", + function ($matches) { + return $matches[1].link_command("bgp", $matches[2]."+exact", $matches[2]).$matches[3]; + }, + $output + ); + + return $output; + } + + // JunOS + if (preg_match("/^show route protocol bgp /i", $exec) OR preg_match("/^show route aspath-regex /i", $exec)) + { + if (preg_match("/^ (.)BGP /", $output, $exp)) + { + if ($exp[1] == '*') + { + $best = '#FF0000'; + } + else + { + $best = ''; + } + } + else if (preg_match("/^:?\n?[\n\d\.A-Fa-f:\/\s]{19}([\*\+\- ]{1})\[BGP\//", $output, $exp)) + { + if ($exp[1] == '*' OR $exp[1] == '+') + { + $best = "#ff0000"; + } + else if ($exp[1] == '-') + { + $best = "#008800"; + } + else + { + $best = ''; + } + } + else if (preg_match("/^[\n]+$/", $output)) + { + $best = ''; + } + + $output = preg_replace_callback( + "/( from )([0-9\.A-Fa-f:]+)/", + function ($matches) { + return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/( Source: )([0-9\.A-Fa-f:]+)/", + function ($matches) { + return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(\s+AS: )([\d ]+)/", + function ($matches) { + return link_as($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(Community: )([\d: ]+)/", + function ($matches) { + return $matches[1].link_community($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(Communities: )([\d: ]+)/", + function ($matches) { + return $matches[1].link_community($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(^\s+AS path: )([\d ]+)/", + function ($matches) { + return $matches[1].link_as($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/^(:?\n?[\dA-Fa-f:]+[\d\.A-Fa-f:\/]+)(\s*)/", + function ($matches) { + return "".link_command("bgp", $matches[1]."+exact", $matches[1])."".$matches[2]; + }, + $output + ); + + if (isset($best) AND $best != '') + { + $output = ''.$output.''; + } + + return $output; + } + + if (preg_match("/bgp/", $exec)) + { + $output = preg_replace("|^(BGP routing table entry for) (\S+)|", "\\1 \\2", $output); + + if (preg_match("|^(Paths: .*) best #(\d+)|", $output, $best_exp)) + { + $best = $best_exp[2]; + + $output = preg_replace("|^(Paths: .*) best #(\d+)|", '\\1 best #\\2', $output); + } + + // Fix for IPv6 route output where there are no addional 3 spaces before addresses + if (preg_match("/ Advertised to non peer-group peers:/", $output) AND preg_match("/ ipv6 /", $exec)) + { + $count--; + } + + if (( preg_match("/^ (\d+.*)/", $output) AND + !preg_match("/^ \d+\./", $output) AND + !preg_match("/[a-z\:\.]+/", $output) + ) OR + preg_match("/^ Local/", $output) OR + preg_match("/, \([^)]+\)/", $output)) + { + $count++; + + $output = preg_replace_callback( + "/^([^\(A-z\n]+)/", + function ($matches) { + return link_as($matches[1]); + }, + $output + ); + $output = preg_replace_callback( + "/(, \(aggregated by )(\d+) ([^\)]+)/", + function ($matches) { + return $matches[1].link_as($matches[2])." ".link_whois($matches[3]); + }, + $output + ); + } + + if (isset($best) AND $best == $count) + { + $output = ''.$output.''; + } + + $output = preg_replace_callback( + "/( from )([0-9\.A-Fa-f:]+)( )/", + function ($matches) { + return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]).$matches[3]; + }, + $output + ); + $output = preg_replace_callback( + "/(Community: )([\d: ]+)/", + function ($matches) { + return $matches[1].link_community($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(Communities: )([\d: ]+)/", + function ($matches) { + return $matches[1].link_community($matches[2]); + }, + $output + ); + $output = preg_replace_callback( + "/(^\s+AS path: )([\d ]+)/", + function ($matches) { + return link_as($matches[2]); + }, + $output + ); + + return $output; + } + + if (preg_match("/^trace/", $exec)) + { + $output = preg_replace("/\[AS0\]\s(.*)/", "\\1", $output); + + // IPv4 + $output = preg_replace_callback( + "/(\[AS)(\d+)(\])\s(.*)(\))(.*)/", + function ($matches) { + return $matches[4].$matches[5]." ".$matches[1]." ".link_as($matches[2]).$matches[3].$matches[6]; + }, + $output + ); + // IPv6 + $output = preg_replace_callback( + "/(\[AS)(\d+)(\])\s([^\s]+)\s(.*)/", + function ($matches) { + return $matches[4]." ".$matches[1]." ".link_as($matches[2]).$matches[3].$matches[5]; + }, + $output + ); + $output = preg_replace_callback( + "/\((.*)\) (\[AS\s+)(\d+)(\])/", + function ($matches) { + return get_as($matches[1], $matches[3]); + }, + $output + ); + + return $output; + } + + return $output; +} + +/** + * Parse `bgp' output contents and return AS pathes + */ +function parse_bgp_path($output) +{ + global $os, $exec, $query, $count; + + $best = FALSE; + $pathes = array(); + + $output = str_replace("\r\n", "\n", $output); + + // MikroTik + if (preg_match("/^\/(ip|ipv6) route print detail/i", $exec) AND $os == 'mikrotik') + { + $output_parts = explode("\n" , trim($output), 4); + + if (!isset($output_parts[3])) + { + return FALSE; + } + + $summary_parts = explode("\n\n" , $output_parts[3]); + + foreach ($summary_parts as $i => $summary_part) + { + $data_exp = explode(' ', trim($summary_part), 3); + + if (strpos($data_exp[1], 'A') !== FALSE) + { + $best = $i; + } + + if (preg_match("/bgp-as-path=\"([^\"]+)\"/", $summary_part, $exp)) + { + if ($path = parse_as_path($exp[1])) + { + $pathes[] = $path; + } + } + } + + return array + ( + 'best' => $best, + 'pathes' => $pathes + ); + } + + // JunOS + if (preg_match("/^show route protocol bgp .* terse/i", $exec)) + { + $lines = explode("\n", $output); + + foreach ($lines as $line) + { + if (preg_match("/^[\+\-\*]{1}/", $line, $exp) AND strpos($line, 'Active Route') === FALSE) + { + $line = preg_replace('/ {2,}/',' ',$line); + $line = explode(' ', $line); + + if ($line[0] == '*' OR $line[0] == '+') + { + if ($count == 0) + { + $count++; + } + } + + $line = array_slice($line, 6, -1); + $line = implode(' ', $line); + + if ($path = parse_as_path($line)) + { + $pathes[] = $path; + } + } + } + + $best = $count - 1; + + return array + ( + 'best' => $best, + 'pathes' => $pathes + ); + } + + // JunOS + if (preg_match("/^show route protocol bgp /i", $exec) OR preg_match("/^show route aspath-regex /i", $exec)) + { + $lines = explode("\n", $output); + + foreach ($lines as $line) + { + if (preg_match("/^ (.)BGP /", $line, $exp)) + { + if ($exp[1] == '*') + { + if ($count == 0) + { + $count++; + } + } + } + else if (preg_match("/^:?\n?[\n\d\.A-Fa-f:\/\s]{19}([\*\+\- ]{1})\[BGP\//", $line, $exp)) + { + if ($exp[1] == '*' OR $exp[1] == '+') + { + if ($count == 0) + { + $count++; + } + } + } + + if (preg_match("/^\s+AS path: ([\d ]+)/", $line, $exp)) + { + if ($path = parse_as_path($exp[1])) + { + $pathes[] = $path; + } + } + } + + $best = $count - 1; + + return array + ( + 'best' => $best, + 'pathes' => $pathes + ); + } + + // Other OS + if (preg_match("/bgp/", $exec)) + { + if (preg_match("|(Paths: .*) best #(\d+)|", $output, $best_exp)) + { + $best = $best_exp[2] - 1; + } + + $lines = explode("\n", $output); + + foreach ($lines as $line) + { + if (( preg_match("/^ (\d+.*)/", $line) AND + !preg_match("/^ \d+\./", $line) AND + !preg_match("/[a-z\:\.]+/", $line) + ) OR + preg_match("/^ Local/", $line) OR + preg_match("/, \([^)]+\)/", $line)) + { + $line = preg_replace("/^([^\(A-z\n]+).*/", "\\1", $line); + + if ($path = parse_as_path($line)) + { + $pathes[] = $path; + } + } + } + + return array + ( + 'best' => $best, + 'pathes' => $pathes + ); + } + + return FALSE; +} + +/** + * Parse list separated by spaces + */ +function parse_list($array) +{ + preg_match_all('/([^=]+)=([^\s]+)[\n\s]/is', $array, $out); + + $array = array(); + + if (isset($out[1])) + { + foreach ($out[1] as $key => $val) + { + $array[trim($out[1][$key])] = trim($out[2][$key], '"'); + } + } + + return $array; +} + +/** + * Parse AS path + */ +function parse_as_path($line) +{ + $path = preg_split('/([^\d]+)/', trim($line)); + + $return = array(); + + foreach ($path as $asn) + { + if ($asn != '') + { + $return[] = 'AS'.$asn; + } + } + + return array_unique($return); +} + +/** + * Build list table + */ +function build_table($table_array) +{ + $size_array = array(); + + foreach ($table_array as $priority => $data) + { + foreach ($data as $field => $value) + { + $size_array[$field][$priority] = $value; + } + } + + $size_max = array(); + + foreach ($size_array as $field => $value) + { + $size_max[$field] = array_map('mb_strlen', array_map('strip_tags', $value)); + } + + $max = array_map('max', $size_max); + + $return = ''; + + foreach ($table_array as $index => $data) + { + $line = array(); + + foreach ($data as $field => $value) + { + if (reset(array_keys($data)) == $field) + { + $line[] = $value.str_repeat(' ', $max[$field] - mb_strlen(strip_tags($value))); + } + else + { + $line[] = str_repeat(' ', $max[$field] - mb_strlen(strip_tags($value))).$value; + } + } + + $return .= implode(' ', $line)."\n"; + } + + return $return; +} + +/** + * Link to execute command + */ +function link_command($command, $query, $name = '', $return_uri = FALSE) +{ + global $router, $protocol; + + if ($name == '') + { + $name = $query; + } + + $uri = '?router='.$router. + '&protocol='.$protocol. + '&command='.$command. + '&query='.$query; + + if ($return_uri) + { + return $uri; + } + + return ''.$name.''; +} + +/** + * Link to AS community + */ +function link_community($line) +{ + global $_CONFIG; + + $communitylist = preg_split('/[^\d:]+/', $line); + + foreach ($communitylist as $i => $community) + { + $communitylist[$i] = ''.$community.''; + } + + return implode(' ', $communitylist); +} + +/** + * Link to AS whois + */ +function link_as($line, $word = FALSE) +{ + global $_CONFIG; + + //print_r($line); + + return preg_replace("/(?:AS)?([\d]+)/is", + "".($word ? 'AS' : '')."\\1", $line); +} + +function get_as($ip, $original_as) +{ + if ($original_as == 15835) + { + $as = 65535; + + if($conn = fsockopen ('whois.cymru.com', 43)) + { + fputs($conn, $ip."\r\n"); + + $output = ''; + + while(!feof($conn)) + { + $output .= fgets($conn,128); + } + + $output = explode("\n", $output); + if (isset($output[1])) + { + $_as = explode("|", $output[1]); + } + + if (isset($_as[0])) + { + $as = trim($_as[0]); + + if ($as == 'NA') + { + $as = 65535; + } + } + + fclose($conn); + } + + return '(' . $ip .') [AS ' . link_as($as) . ']'; + } + else + { + return '(' . $ip .') [AS ' . link_as($original_as) . ']'; + } +} + +/** + * Link to whois IP + */ +function link_whois($line, $name = '') +{ + global $_CONFIG; + + if ($name == '') + { + $name = $line; + } + + return ''.$name.''; +} + +/** + * Get and print BGP path graph + */ +function get_path_graph($router, $query, $as_pathes, $as_best_path, $format = 'svg') +{ + global $_CONFIG, $_REQUEST; + + $font_size = 9; // default font size + $graph = new Image_GraphViz(); + + $graph->addNode($router, array + ( + 'label' => $_CONFIG['routers'][$router]['description'], + 'shape' => 'box', + 'style' => 'filled', + 'fillcolor' => '#FFCCCC', + 'fontsize' => $font_size + 2 + )); + + $query_ptr = get_ptr($query); + + // Draw requested node + $graph->addNode($query, array + ( + 'URL' => link_command('trace', $query, '', TRUE), + 'target' => '_blank', + 'label' => $query_ptr ? $query."\n".$query_ptr : $query, + 'shape' => 'box', + 'style' => 'filled', + 'fillcolor' => '#FFFFCC', + 'fontsize' => $font_size + )); + + $as_pathes_best = $as_list = $as_peer_list = array(); + + // Draw path ASs + foreach ($as_pathes as $as_path_id => $as_path_array) + { + $peer_as = reset($as_path_array); + + if (!isset($as_peer_list[$peer_as]) OR $as_peer_list[$peer_as] !== TRUE) + { + $as_peer_list[$peer_as] = ($as_best_path === $as_path_id); + } + + $as_list = array_merge($as_list, $as_path_array); + } + + $as_list = array_unique($as_list); + + // Draw our AS + if (!in_array('AS'.$_CONFIG['asn'], $as_list)) + { + $group_as = 'AS'.$_CONFIG['asn']; + $group_label = $group_as."\n".$_CONFIG['company']; + + $node_array = array + ( + 'target' => '_blank', + 'label' => $group_label, + 'style' => 'filled', + 'fillcolor' => 'white', + 'fontsize' => $font_size + ); + + if (isset($_CONFIG['routers'][$router]['group']) AND + $_CONFIG['routers'][$router]['group'] !== $_CONFIG['asn'] AND + $_CONFIG['routers'][$router]['group'] !== $group_as) + { + $group_as = 'AS'.ltrim($_CONFIG['routers'][$router]['group'], 'AS'); + $group_label = $_CONFIG['routers'][$router]['group']; + + if ($group_asinfo = get_asinfo($group_as)) + { + $group_label = isset($group_asinfo['description']) ? $group_as."\n".$group_asinfo['description'] : $group_as; + $node_array['URL'] = $_CONFIG['aswhois'].$group_as; + } + + $node_array['label'] = $group_label; + } + else + { + $node_array['URL'] = $_CONFIG['aswhois'].$group_as; + } + + $graph->addNode($group_as, $node_array); + $graph->addEdge(array($group_as => $router), array + ( + 'color' => 'red' + )); + } + + foreach ($as_list as $as_id) + { + $color = isset($as_peer_list[$as_id]) ? ($as_peer_list[$as_id] ? '#CCFFCC' : '#CCCCFF') : 'white'; + + $asinfo = get_asinfo($as_id); + + $graph->addNode($as_id, array + ( + 'URL' => $_CONFIG['aswhois'].$as_id, + 'target' => '_blank', + 'label' => isset($asinfo['description']) ? $as_id."\n".$asinfo['description'] : $as_id, + 'style' => 'filled', + 'fillcolor' => $color, + 'fontsize' => $font_size + )); + } + + // Draw pathes + foreach ($as_pathes as $as_path_id => $as_path_array) + { + $first = $last = FALSE; + + foreach ($as_path_array as $as_path) + { + if (!$first) + { + $first = $last = $as_path; + + continue; + } + + if (!isset($as_pathes_best[$last.$as_path])) + { + $graph->addEdge(array($last => $as_path), array + ( + 'color' => ($as_best_path === $as_path_id) ? 'red' : 'black' + )); + + if ($as_best_path === $as_path_id) + { + $as_pathes_best[$last.$as_path] = TRUE; + } + } + + $last = $as_path; + } + + if (!isset($as_pathes_best[$router.$first])) + { + $graph->addEdge(array($router => $first), array + ( + 'color' => ($as_best_path === $as_path_id) ? 'red' : 'black' + )); + + if ($as_best_path === $as_path_id) + { + $as_pathes_best[$router.$first] = TRUE; + } + } + } + + // Draw last path + $graph->addEdge(array($as_path => $query), array + ( + 'color' => ($as_pathes_best) ? 'red' : 'black' + )); + + $graph->image($format); + + exit; +} + +/** + * Get and print blank image + */ +function get_blank_graph($string, $format = 'svg') +{ + $graph = new Image_GraphViz(); + + $graph->addNode('error', array + ( + 'label' => $string, + 'shape' => 'none', + 'fontcolor' => 'red', + 'fontsize' => 14 + )); + + $graph->image($format); + + exit; +} + +/** + * Get address information from RADb + */ +function get_radb($request) +{ + if (!$fp = @fsockopen('whois.radb.net', 43, $errnum, $errstr, 10)) + { + return FALSE; + } + + fputs($fp, $request."\r\n"); + + $data = ''; + + while (!feof($fp)) + { + $data .= fgets($fp, 128); + } + + fclose($fp); + + $exp = explode("\n\n", $data); + + if (!isset($exp[0]) OR empty($exp[0])) + { + return FALSE; + } + + $lines = explode("\n", $exp[0]); + + $return = array(); + + foreach ($lines as $line) + { + $line = explode(':', $line, 2); + + if (isset($line[0]) AND isset($line[1])) + { + $return[$line[0]] = trim($line[1]); + } + } + + if (sizeof($return) < 1) + { + return FALSE; + } + + return $return; +} + +/** + * Get AS name information from "Team Cymru - IP to ASN Mapping" + */ +function get_asinfo($request) +{ + if (!$dns = dns_get_record($request.'.asn.cymru.com', DNS_TXT) OR !isset($dns[0]['txt'])) + { + return FALSE; + } + + $segments = array_map('trim', explode('|', $dns[0]['txt'], 5)); + + if (sizeof($segments) != 5) + { + return FALSE; + } + + list($segments[4], $segments[5]) = explode(' ', $segments[4], 2); + + $segments[5] = str_replace('_', '"', $segments[5]); + + return array + ( + 'asn' => $segments[0], + 'country' => $segments[1], + 'registrar' => $segments[2], + 'regdate' => $segments[3], + 'asname' => $segments[4], + 'description' => $segments[5], + ); +} + +/** + * Get PTR record for IPv4/IPv6 address + */ +function get_host($hostname) +{ + global $protocol; + + if (filter_var($hostname, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) + { + return $hostname; + } + + $dns_a = (($dns_a = dns_get_record($hostname, DNS_A)) !== FALSE) ? $dns_a : array(); + $dns_aaaa = (($dns_aaaa = dns_get_record($hostname, DNS_AAAA)) !== FALSE) ? $dns_aaaa : array(); + + $ip_array = array(); + + foreach (array_merge($dns_aaaa, $dns_a) as $record) + { + switch ($record['type']) + { + case 'A': + $ip_array[] = $record['ip']; + break; + + case 'AAAA': + $ip_array[] = $record['ipv6']; + break; + + case 'CNAME': + $ip_array = array_push($ip_array, get_host($record['target'])); + break; + } + } + + $ipv4_array = $ipv6_array = array(); + + foreach ($ip_array as $ip) + { + if (strpos($ip, ':') !== FALSE) + { + $ipv6_array[] = $ip; + } + else + { + $ipv4_array[] = $ip; + } + } + + $ip_array = ($protocol == 'ipv6') + ? $ipv6_array + : $ipv4_array; + + if (!$ipv6_array OR !$ipv4_array) + { + $ip_array = array_merge($ipv6_array, $ipv4_array); + } + + if (!$ip_array OR (!$ipv4_array AND $protocol != 'ipv6')) + { + return FALSE; + } + + return end($ip_array); +} + +/** + * Get PTR record for IPv4/IPv6 address + */ +function get_ptr($ip) +{ + if (preg_match("/:/", $ip)) + { + $unpack = unpack('H*hex', @inet_pton($ip)); + $arpa = implode('.', array_reverse(str_split($unpack['hex']))).'.ip6.arpa'; + } + else + { + $arpa = implode('.', array_reverse(explode('.', $ip))).'.in-addr.arpa.'; + } + + if (!$arpa OR !$dns = dns_get_record($arpa, DNS_PTR)) + { + return $ip; + } + + $ptr = array(); + + foreach ($dns as $record) + { + if ($record['type'] == 'PTR') + { + $ptr[] = $record['target']; + } + } + + if (!$ptr) + { + return $ip; + } + + return reset($ptr); +} + +/** + * Group router list + */ +function group_routers($array) +{ + $return = array(); + + foreach ($array as $key => $value) + { + if (isset($value['group']) AND $value['group'] != '') + { + $return[$value['group']][$key] = $value; + + continue; + } + + $return[''][$key] = $value; + } + + return $return; +} + +// ------------------------------------------------------------------------ + +/** +* Telnet class +* +* Used to execute remote commands via telnet connection +* Usess sockets functions and fgetc() to process result +* +* All methods throw Exceptions on error +* +* Written by Dalibor AndzakovicThe router does not support IPv6. Using IPv4.
Address '.$query.' is converted to a subnet '.$route.' using the Merit RADb
'; - } - - $query = $route; - } - } - } - - $exec = sprintf($exec, escapeshellcmd($query)); - - if (!$query) - { - $exec = FALSE; - - if ($query === FALSE) - { - print 'Can\'t resolve the hostname.
Parameter missing.
No parameter needed.
Class Image_GraphViz not found!
BGP routing graph for , router:
-Run a bgp command on this router
-| Upstream AS | Peering AS | Best route |
Please wait...
';
- flush();
-
- process($url, $exec);
-
- print '';
- }
- }
-}
-else
-{
- $routers = group_routers($_CONFIG['routers']);
-
-// HTML form
-?>
-
-
- Information: RIPEstat he.net robtex.com PeeringDB
-Copyright ©
-Command failed.
'; - } - - 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 'Command aborted.
'; - } - - $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 (empty($lines) AND !$line) - { - print 'Command failed.
'; - } - } - catch (Exception $exception) - { - $is_exception = TRUE; - - if (!$return_buffer) - { - print 'Telnet error: '.$exception->getMessage().'
'; - } - } - - 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 .= ''.$summary_part."\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', - ); - - foreach ($summary_parts as $summary_part) - { - $data_exp = explode(' ', trim($summary_part), 3); - - if (sizeof($data_exp) != 3) - { - continue; - } - - $data = parse_list($data_exp[2]); - - $table_array[] = array - ( - 'remote-address' => isset($data['remote-address']) ? link_whois($data['remote-address']) : '', - 'name' => isset($data['name']) ? $data['name'] : '', - 'remote-id' => isset($data['remote-id']) ? $data['remote-id'] : '', - 'remote-as' => isset($data['remote-as']) ? link_as($data['remote-as']) : '', - 'withdrawn-received' => isset($data['withdrawn-received']) ? $data['withdrawn-received'] : '', - 'updates-received' => isset($data['updates-received']) ? $data['updates-received'] : '', - 'uptime' => isset($data['uptime']) ? $data['uptime'] : '', - 'state' => $data_exp[1], - 'prefix-count' => (isset($data['remote-address']) AND isset($data['prefix-count'])) - ? link_command('routes', $data['remote-address'], $data['prefix-count']) - : '', - 'updates-sent' => (isset($data['name']) AND isset($data['updates-sent'])) - ? link_command('advertised-routes', $data['name'], $data['updates-sent']) - : '', - ); - } - - return - str_replace('Flags', 'State', $output_parts[0])."\n\n". - build_table($table_array)."\n\n". - 'Total number of neighbors '.(sizeof($table_array) - 1); - } - - // MikroTik - if (preg_match("/^\/ping/i", $exec) AND $os == 'mikrotik') - { - if (preg_match('/^HOST/', $output) AND $index == 0) - { - $index++; - - return 'PING '.$query.' ('.get_ptr($query).'): 56 data bytes'."\n"; - } - - if ($index > 0) - { - $exp = explode(' ', preg_replace('/[\s\t]+/', ' ', trim($output))); - $exp = array_slice($exp, 0, 4); - - if (!$exp OR $exp[0] == '' OR $exp[0] == 'HOST' OR (isset($exp[1]) AND $exp[1] == 'timeout')) - { - return; - } - - if (preg_match('/sent=([0-9]+) received=([0-9]+) packet-loss=([^ ]+)/', $output, $stat)) - { - $return = "\n".'--- '.$query.' ping statistics ---'."\n". - $stat[1].' packets transmitted, '.$stat[2].' packets received, '.trim($stat[3]).' packet loss'."\n"; - - if (preg_match('/min-rtt=([0-9]+)ms avg-rtt=([0-9]+)ms/', $output, $stat2)) - { - $return .= 'round-trip min/avg/max = '.$stat2[1].'/'.$stat2[2]; - } - - if (preg_match('/max-rtt=([0-9]+)ms/', $output, $stat3)) - { - $return .= '/'.$stat3[1]; - } - - return $return; - } - - // Пинг больше 99, отдача max-rtt отдельной строкой - if (preg_match('/max-rtt=([0-9]+)ms/', $output, $stat3)) - { - $return = '/'.$stat3[1]; - - return $return; - } - - $index++; - - return $exp[1].' bytes from '.$exp[0].': icmp_seq='.intval($index-1).' ttl='.$exp[2].' time='.$exp[3]."\n"; - } - } - - // MikroTik - if (preg_match("/^\/tool trace/i", $exec) AND $os == 'mikrotik' AND $ros >= 6) - { - if (preg_match('/^ # ADDRESS/', $output) AND $index == 0) - { - $index++; - - return 'traceroute to '.$query.' ('.get_ptr($query).'), 64 hops max, 60 byte packets'."\n"; - } - - if ($index > 0) - { - $exp = explode(' ', preg_replace('/[\s\t]+/', ' ', trim($output))); - $exp = array_slice($exp, 0, 9); - - if (!$exp OR empty($exp[0]) OR (sizeof($exp) == 4 AND isset($exp[3]) AND $exp[3] != 'timeout')) - { - return; - } - - $new_exp[0] = (strlen($exp[0]) < 2) ? ' '.$exp[0] : $exp[0]; - - if ($exp[3] == 'timeout') - { - $new_exp[1] = '* * *'; - $new_exp = array_slice($new_exp, 0, 2); - } - else - { - $radb = get_radb($exp[1]); - - $new_exp[1] = get_ptr($exp[1]); - $new_exp[2] = '('.$exp[1].')'; - $new_exp[3] = '['.(isset($radb['origin']) ? 'AS '.link_as($radb['origin']) : '').']'; - $new_exp[4] = $exp[5].'ms'; - $new_exp[5] = $exp[6].'ms'; - $new_exp[6] = $exp[7].'ms'; - } - - if ($index >= 64) - { - return TRUE; - } - - $str = implode(' ', $new_exp)."\r\n"; - - if (@in_array($str, $str_in)) - { - return; - } - - $str_in[] = $str; - - $index++; - - return $str; - } - } - - // MikroTik - if (preg_match("/^\/tool trace/i", $exec) AND $os == 'mikrotik' AND $ros <= 5) - { - if (preg_match('/^ # ADDRESS/', $output) AND $index == 0) - { - $index++; - - return 'traceroute to '.$query.' ('.get_ptr($query).'), 64 hops max, 60 byte packets'."\n"; - } - - if ($index > 0) - { - $exp = explode(' ', preg_replace('/[\s\t]+/', ' ', trim($output))); - $exp = array_slice($exp, 0, 5); - - if (!$exp OR empty($exp[0])) - { - return; - } - - $exp[0] = (strlen($exp[0]) < 2) ? ' '.$exp[0] : $exp[0]; - - if ($exp[1] == '0.0.0.0' OR $exp[1] = '::') - { - $exp[1] = '* * *'; - $exp = array_slice($exp, 0, 2); - } - else - { - $radb = get_radb($exp[1]); - - $exp[6] = $exp[4]; - $exp[5] = $exp[3]; - $exp[4] = $exp[2]; - $exp[3] = '['.(isset($radb['origin']) ? 'AS '.link_as($radb['origin']) : '').']'; - $exp[2] = '('.$exp[1].')'; - $exp[1] = get_ptr($exp[1]); - } - - if ($index >= 64) - { - return TRUE; - } - - $index++; - - return implode(' ', $exp)."\r\n"; - } - } - - // Other OS parsers - if (preg_match('/^show bgp ipv6 unicast/i', $exec) AND $os = 'ios') - { - $output = str_replace('% Incomplete command.', 'Please use network prefix in Parameter.
', $output); - } - - if ($exec == 'show ip bgp summary') - { - $output = preg_replace_callback( - "/( local AS number )(\d+)/", - function ($matches) { - return $matches[1].link_as($matches[2]); - }, - $output - ); - - // JunOS - if ($os == 'junos') - { - $output = preg_replace_callback( - "/^([\dA-Fa-f:\.]+\s+)(\d+)/", - function ($matches) { - return $matches[1].link_as($matches[2]); - }, - $output - ); - } - else - { - $output = preg_replace_callback( - "/^([\d\.]+\s+\d+\s+)(\d+)/", - function ($matches) { - return $matches[1].link_as($matches[2]); - }, - $output - ); - } - - $output = preg_replace_callback( - "/^(\d+\.\d+\.\d+\.\d+)(\s+.*\s+)([1-9]\d*)\n$/", - function ($matches) { - return $matches[1].$matches[2].link_command("routes", $matches[1], $matches[3])."\n"; - }, - $output - ); - $output = preg_replace_callback( - "/^(\d+\.\d+\.\d+\.\d+)(\s+)/", - function ($matches) { - return link_command("bgp", "neighbors+".$matches[1], $matches[1]).$matches[2]; - }, - $output - ); - - // IPv6 neighbours - $output = preg_replace_callback( - "/^(.{15} 4\s+)(\d+)/", - function ($matches) { - return $matches[1].link_as($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/^([\dA-Fa-f]*:[\dA-Fa-f:]*)(\s+)/", - function ($matches) { - return link_command("bgp", "neighbors+".$matches[1], $matches[1]).$matches[2]; - }, - $output - ); - $output = preg_replace_callback( - "/^([\dA-Fa-f]*:[\dA-Fa-f:]*)\n$/", - function ($matches) { - return link_command("bgp", "neighbors+".$matches[1], $matches[1])."\n"; - }, - $output - ); - - return $output; - } - - if ($exec == 'show ipv6 bgp summary' OR $exec == 'show bgp ipv6 unicast summary') - { - $output = preg_replace_callback( - "/^(.{15} 4\s+)(\d+)/", - function ($matches) { - return $matches[1].link_as($matches[2]); - }, - $output - ); - - if (preg_match("/^([\dA-Fa-f]*:[\dA-Fa-f:]*)\s+4\s+/", $output, $lastip_exp)) - { - $lastip = $lastip_exp[1]; - - $output = preg_replace_callback( - "/^(.{15} 4\s+)(\d+)/", - function ($matches) { - return $matches[1].link_as($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/^([\dA-Fa-f:]+)(\s+.*\s+)([1-9]\d*)$/", - function ($matches) use ($lastip) { - return link_command("routes", $lastip, $matches[3]); - }, - $output - ); - $output = preg_replace_callback( - "/^([\dA-Fa-f:]+)(\s+)/", - function ($matches) { - return link_command("routes", $matches[1]).$matches[2]; - }, - $output - ); - - unset($lastip); - } - - if (preg_match("/^([\dA-Fa-f:]+)\n$/", $output, $lastip_exp)) - { - $lastip = $lastip_exp[1]; - - $output = preg_replace_callback( - "/^([\dA-Fa-f:]+)/", - function ($matches) { - return link_command("routes", $matches[1]); - }, - $output - ); - } - - if (isset($lastip) AND preg_match("/^(\s+.*\s+)([1-9]\d*)\n$/", $output)) - { - $output = preg_replace_callback( - "/^(\s+.*\s+)([1-9]\d*)\n$/", - function ($matches) use ($lastip) { - return $matches[1].link_command("routes", $lastip, $matches[2])."\n"; - }, - $output - ); - - unset($lastip); - } - - return $output; - } - - if (preg_match("/^show bgp summary(?:\s+instance\s+[\d\w-]*)?$/", $exec)) - { - // JunOS - if (preg_match("/^([\dA-Fa-f:][\d\.A-Fa-f:]+)\s+/", $output, $lastip_exp)) - { - $lastip = $lastip_exp[1]; - - // IPv4 - $output = preg_replace_callback( - "/^(\d+\.\d+\.\d+\.\d+)(\s+.*\s+)([1-9]\d*)(\s+\d+\s+\d+\s+\d+\s+\d+\s+[\d:]+\s+)(\d+)\/(\d+)\/(\d+)(\s+)/", - function ($matches) { - return $matches[1].$matches[2]. - link_command("routes", $matches[1], $matches[3]).$matches[4]. - link_command("routes", $matches[1], $matches[5])."/". - link_command("routes", $matches[1], $matches[6])."/". - link_command("routes", $matches[1], $matches[7]).$matches[8]; - }, - $output - ); - - // IPv4/IPv6 - $output = preg_replace_callback( - "/^([\dA-Fa-f:][\d\.A-Fa-f:]+\s+)(\d+)(\s+)/", - function ($matches) { - return $matches[1].link_as($matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/^([\dA-Fa-f:][\d\.A-Fa-f:]+)(\s+)/", - function ($matches) { - return link_command("bgp", "neighbors+".$matches[1], $matches[1]).$matches[2]; - }, - $output - ); - } - - if (isset($lastip) AND preg_match("/( [^:]+: )(\d+)\/(\d+)\/(\d+)$/", $output)) - { - $output = preg_replace_callback( - "/^( [^:]+: )(\d+)\/(\d+)\/(\d+)\n$/", - function ($matches) use ($lastip) { - return "\\1". - link_command("routes", $lastip, $matches[2])."/". - link_command("bgp", "neighbors+".$lastip."+routes+all", $matches[3])."/". - link_command("bgp", "neighbors+".$lastip."+routes+damping+suppressed", $matches[4])."\n"; - }, - $output - ); - - unset($lastip); - } - - return $output; - } - - if (preg_match("/^show ip bgp\s+n\w*\s+[\d\.]+\s+(ro|re|a)/i", $exec) OR - preg_match("/^show (ipv6 bgp|bgp ipv6)\s+n\w*\s+[\dA-Fa-f:]+\s+(ro|re|a)/i", $exec) OR - preg_match("/^show (ipv6 bgp|bgp ipv6)\s+re/i", $exec) OR - preg_match("/^show ip bgp\s+[\d\.]+\s+[\d\.]+\s+(l|s)/i", $exec) OR - preg_match("/^show (ip bgp|bgp ipv6) prefix-list/i", $exec) OR - preg_match("/^show (ip bgp|bgp ipv6) route-map/i", $exec)) - { - $output = preg_replace_callback( - "/^([\*r ](>|d|h| ).{59})([\d\s,\{\}]+)([ie\?])\n$/", - function ($matches) { - return $matches[1].link_as($matches[3]).$matches[4]."\n"; - }, - $output - ); - $output = preg_replace_callback( - "/^([\*r ](>|d|h| )[i ])([\d\.A-Fa-f:\/]+)(\s+)/", - function ($matches) { - return $matches[1].link_command("bgp", $matches[3]).$matches[4]; - }, - $output - ); - $output = preg_replace_callback( - "/^([\*r ](>|d|h| )[i ])([\d\.A-Fa-f:\/]+)\n$/", - function ($matches) { - return $matches[1].link_command("bgp", $matches[3])."\n"; - }, - $output - ); - $output = preg_replace_callback( - "/^(( ){20}.{41})([\d\s,\{\}]+)([ie\?])\n$/", - function ($matches) { - return $matches[1].link_as($matches[3]).$matches[4]."\n"; - }, - $output - ); - $output = preg_replace_callback( - "/(, remote AS )(\d+)(,)/", - function ($matches) { - return $matches[1].link_as($matches[2]).$matches[3]."\n"; - }, - $output - ); - - return $output; - } - - // JunOS - if (preg_match("/^show route receive-protocol bgp\s+[\d\.A-Fa-f:]+/i", $exec)) - { - $output = preg_replace_callback( - "/(Community: )([\d: ]+)/", - function ($matches) { - return $matches[1].link_community($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(Communities: )([\d: ]+)/", - function ($matches) { - return $matches[1].link_community($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(^\s+AS path: )([\d ]+)/", - function ($matches) { - return $matches[1].link_as($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/^([\d\.\s].{24})([\d\.]+)(\s+)/", - function ($matches) { - return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/^([\d\.\/]+)(\s+)/", - function ($matches) { - return link_command("bgp", $matches[1]).$matches[2]; - }, - $output - ); - $output = preg_replace_callback( - "/^([\d\.A-Fa-f:\/]+)(\s+)/", - function ($matches) { - return link_command("bgp", $matches[1]."+exact", $matches[1]).$matches[2]; - }, - $output - ); - $output = preg_replace_callback( - "/^([\d\.A-Fa-f:\/]+)\s*\n$/", - function ($matches) { - return link_command("bgp", $matches[1]."+exact", $matches[1]); - }, - $output - ); - $output = preg_replace_callback( - "/^([ \*] )([\d\.A-Fa-f:\/]+)(\s+)/", - function ($matches) { - return $matches[1].link_command("bgp", $matches[2]."+exact", $matches[2]).$matches[3]; - }, - $output - ); - - return $output; - } - - // JunOS - if (preg_match("/^show route advertising-protocol bgp\s+([\d\.A-Fa-f:]+)(?:\s+table\s+[\d\w\-\.]*)?$/i", $exec, $ip_exp)) - { - $ip = $ip_exp[1]; - - $output = preg_replace_callback( - "/^([\d\.\s].{64})([\d\s,\{\}]+)([I\?])\n$/", - function ($matches) { - return $matches[1].link_as($matches[2]).$matches[3]."\n"; - }, - $output - ); - $output = preg_replace_callback( - "/^([\d\.\s].{24})([\d\.]+)(\s+)/", - function ($matches) { - return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/^([\d\.\/]+)(\s+)/", - function ($matches) { - return link_command("bgp", $matches[1]).$matches[2]; - }, - $output - ); - $output = preg_replace_callback( - "/^([\d\.A-Fa-f:\/]+)(\s+)/", - function ($matches) { - return link_command("bgp", $matches[1]."+exact", $matches[1]).$matches[2]; - }, - $output - ); - $output = preg_replace_callback( - "/^([\d\.A-Fa-f:\/]+)\s*\n$/", - function ($matches) { - return link_command("bgp", $matches[1]."+exact", $matches[1])."\n"; - }, - $output - ); - $output = preg_replace_callback( - "/^([ \*] )([\d\.A-Fa-f:\/]+)(\s+)/", - function ($matches) use ($ip) { - return $matches[1].link_command("bgp", "neighbors+".$ip."+advertised-routes+".$matches[2], $matches[2]).$matches[3]; - }, - $output - ); - - return $output; - } - - if (preg_match("/^show ip bgp n\w*\s+([\d\.]+)/i", $exec) OR - preg_match("/^show ip bgp n\w*$/i", $exec)) - { - if (!isset($lastip) AND preg_match("/^BGP neighbor is ([\d\.]+)(,?)/", $output, $lastip_exp)) - { - $lastip = $lastip_exp[1]; - } - - $output = preg_replace_callback( - "/(Prefix )(advertised)( [1-9]\d*)/", - function ($matches) use ($lastip) { - return $matches[1].link_command("advertised-routes", $lastip, $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/( Prefixes Total: )(\d+)( )/", - function ($matches) use ($lastip) { - return $matches[1].link_command("advertised-routes", $lastip, $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/(prefixes )(received)( [1-9]\d*)/", - function ($matches) use ($lastip) { - return $matches[1].link_command("routes", $lastip, $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/( Prefixes Current: \s+)(\d+)(\s+)(\d+)/", - function ($matches) use ($lastip) { - return $matches[1]. - link_command("advertised-routes", $lastip, $matches[2]).$matches[3]. - link_command("routes", $lastip, $matches[4]); - }, - $output - ); - $output = preg_replace_callback( - "/(\s+)(Received)( prefixes:\s+[1-9]\d*)/", - function ($matches) use ($lastip) { - return $matches[1].link_command("routes", $lastip, $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/( Saved \(soft-reconfig\):\s+)(\d+|n\/a)(\s+)(\d+)/", - function ($matches) use ($lastip) { - return $matches[1].$matches[2].$matches[3].link_command("received-routes", $lastip, $matches[4]); - }, - $output - ); - $output = preg_replace_callback( - "/( [1-9]\d* )(accepted)( prefixes)/", - function ($matches) use ($lastip) { - return $matches[1].link_command("routes", $lastip, $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/^( [1-9]\d* )(accepted|denied but saved)( prefixes consume \d+ bytes)/", - function ($matches) use ($lastip) { - return $matches[1].link_command("received-routes", $lastip, $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/^(BGP neighbor is )(\d+\.\d+\.\d+\.\d+)(,?)/", - function ($matches) { - return $matches[1].link_whois($matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace("/^( Description: )(.*)$/", '\\1\\2', $output); - $output = preg_replace_callback( - "/(,\s+remote AS )(\d+)(,)/", - function ($matches) { - return $matches[1].link_as($matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/(, local AS )(\d+)(,)/", - function ($matches) { - return $matches[1].link_as($matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/( update prefix filter list is\s+:?\*?)(\S+)/", - function ($matches) { - return $matches[1].link_command("bgp", "prefix-list+".$matches[2], $matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(Route map for \S+ advertisements is\s+:?\*?)(\S+)/", - function ($matches) { - return $matches[1].link_command("bgp", "route-map+".$matches[2], $matches[2]); - }, - $output - ); - - return $output; - } - - if (preg_match("/^show (ipv6 bgp|bgp ipv6) n\w*\s+([\dA-Fa-f:]+)/i", $exec, $ip_exp)) - { - $ip = $ip_exp[1]; - - $output = preg_replace_callback( - "/(Prefix )(advertised)( [1-9]\d*)/", - function ($matches) use ($ip) { - return $matches[1].link_command("advertised-routes", "'.$ip.'", $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/( [1-9]\d* )(accepted)( prefixes)/", - function ($matches) use ($ip) { - return $matches[1].link_command("routes", $ip, $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace("/^( Description: )(.*)$/", '\\1\\2', $output); - $output = preg_replace_callback( - "/(\s+remote AS )(\d+)(,)/", - function ($matches) { - return $matches[1].link_as($matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/(\s+local AS )(\d+)(,)/", - function ($matches) { - return $matches[1].link_as($matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/( update prefix filter list is\s+:?\*?)(\S+)/", - function ($matches) { - return $matches[1].link_command("bgp", "prefix-list+".$matches[2], $matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(Route map for \S+ advertisements is\s+:?\*?)(\S+)/", - function ($matches) { - return $matches[1].link_command("bgp", "route-map+".$matches[2], $matches[2]); - }, - $output - ); - - return $output; - } - - if (preg_match("/^show bgp n\w*\s+(?:instance\s+[\d\w-]*)?\s*([\d\.A-Fa-f:]+)/i", $exec, $ip_exp)) - { - $ip = $ip_exp[1]; - - $output = preg_replace_callback( - "/(\s+AS )(\d+)/", - function ($matches) { - return $matches[1].link_as($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(\s+AS: )(\d+)/", - function ($matches) { - return $matches[1].link_as($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/^( Active prefixes:\s+)(\d+)/", - function ($matches) use ($ip) { - return $matches[1].link_command("routes", $ip, $matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/^( Received prefixes:\s+)(\d+)/", - function ($matches) use ($ip) { - return $matches[1].link_command("bgp", "neighbors+".$ip."+routes+all", $matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/^( Advertised prefixes:\s+)(\d+)/", - function ($matches) use ($ip) { - return $matches[1].link_command("advertised-routes", $ip, $matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/^( Suppressed due to damping:\s+)(\d+)/", - function ($matches) use ($ip) { - return $matches[1].link_command("bgp", "neighbors+".$ip."+routes+damping+suppressed", $matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/^( )(Export)(: )/", - function ($matches) use ($ip) { - return $matches[1].link_command("advertised-routes", $ip, $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/( )(Import)(: )/", - function ($matches) use ($ip) { - return $matches[1].link_command("bgp", "neighbors+".$ip."+routes+all", $matches[2]).$matches[3]; - }, - $output - ); - - return $output; - } - - // JunOS - if (preg_match("/^show route protocol bgp .* terse/i", $exec)) - { - if (preg_match("/^([\+\-\*]){1}/", $output, $exp) AND strpos($output, 'Active Route') === FALSE) - { - if ($exp[1] == '*' OR $exp[1] == '+') - { - $best = "#ff0000"; - } - else if ($exp[1] == '-') - { - $best = "#008800"; - } - else - { - $best = ''; - } - } - - if (isset($best) AND $best != '') - { - $output = ''.$output.''; - } - - $output = preg_replace_callback( - "/^([\* ] )([\d\.A-Fa-f:\/]+)(\s+)/", - function ($matches) { - return $matches[1].link_command("bgp", $matches[2]."+exact", $matches[2]).$matches[3]; - }, - $output - ); - - return $output; - } - - // JunOS - if (preg_match("/^show route protocol bgp /i", $exec) OR preg_match("/^show route aspath-regex /i", $exec)) - { - if (preg_match("/^ (.)BGP /", $output, $exp)) - { - if ($exp[1] == '*') - { - $best = '#FF0000'; - } - else - { - $best = ''; - } - } - else if (preg_match("/^:?\n?[\n\d\.A-Fa-f:\/\s]{19}([\*\+\- ]{1})\[BGP\//", $output, $exp)) - { - if ($exp[1] == '*' OR $exp[1] == '+') - { - $best = "#ff0000"; - } - else if ($exp[1] == '-') - { - $best = "#008800"; - } - else - { - $best = ''; - } - } - else if (preg_match("/^[\n]+$/", $output)) - { - $best = ''; - } - - $output = preg_replace_callback( - "/( from )([0-9\.A-Fa-f:]+)/", - function ($matches) { - return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/( Source: )([0-9\.A-Fa-f:]+)/", - function ($matches) { - return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(\s+AS: )([\d ]+)/", - function ($matches) { - return link_as($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(Community: )([\d: ]+)/", - function ($matches) { - return $matches[1].link_community($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(Communities: )([\d: ]+)/", - function ($matches) { - return $matches[1].link_community($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(^\s+AS path: )([\d ]+)/", - function ($matches) { - return $matches[1].link_as($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/^(:?\n?[\dA-Fa-f:]+[\d\.A-Fa-f:\/]+)(\s*)/", - function ($matches) { - return "".link_command("bgp", $matches[1]."+exact", $matches[1])."".$matches[2]; - }, - $output - ); - - if (isset($best) AND $best != '') - { - $output = ''.$output.''; - } - - return $output; - } - - if (preg_match("/bgp/", $exec)) - { - $output = preg_replace("|^(BGP routing table entry for) (\S+)|", "\\1 \\2", $output); - - if (preg_match("|^(Paths: .*) best #(\d+)|", $output, $best_exp)) - { - $best = $best_exp[2]; - - $output = preg_replace("|^(Paths: .*) best #(\d+)|", '\\1 best #\\2', $output); - } - - // Fix for IPv6 route output where there are no addional 3 spaces before addresses - if (preg_match("/ Advertised to non peer-group peers:/", $output) AND preg_match("/ ipv6 /", $exec)) - { - $count--; - } - - if (( preg_match("/^ (\d+.*)/", $output) AND - !preg_match("/^ \d+\./", $output) AND - !preg_match("/[a-z\:\.]+/", $output) - ) OR - preg_match("/^ Local/", $output) OR - preg_match("/, \([^)]+\)/", $output)) - { - $count++; - - $output = preg_replace_callback( - "/^([^\(A-z\n]+)/", - function ($matches) { - return link_as($matches[1]); - }, - $output - ); - $output = preg_replace_callback( - "/(, \(aggregated by )(\d+) ([^\)]+)/", - function ($matches) { - return $matches[1].link_as($matches[2])." ".link_whois($matches[3]); - }, - $output - ); - } - - if (isset($best) AND $best == $count) - { - $output = ''.$output.''; - } - - $output = preg_replace_callback( - "/( from )([0-9\.A-Fa-f:]+)( )/", - function ($matches) { - return $matches[1].link_command("bgp", "neighbors+".$matches[2], $matches[2]).$matches[3]; - }, - $output - ); - $output = preg_replace_callback( - "/(Community: )([\d: ]+)/", - function ($matches) { - return $matches[1].link_community($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(Communities: )([\d: ]+)/", - function ($matches) { - return $matches[1].link_community($matches[2]); - }, - $output - ); - $output = preg_replace_callback( - "/(^\s+AS path: )([\d ]+)/", - function ($matches) { - return link_as($matches[2]); - }, - $output - ); - - return $output; - } - - if (preg_match("/^trace/", $exec)) - { - $output = preg_replace("/\[AS0\]\s(.*)/", "\\1", $output); - - // IPv4 - $output = preg_replace_callback( - "/(\[AS)(\d+)(\])\s(.*)(\))(.*)/", - function ($matches) { - return $matches[4].$matches[5]." ".$matches[1]." ".link_as($matches[2]).$matches[3].$matches[6]; - }, - $output - ); - // IPv6 - $output = preg_replace_callback( - "/(\[AS)(\d+)(\])\s([^\s]+)\s(.*)/", - function ($matches) { - return $matches[4]." ".$matches[1]." ".link_as($matches[2]).$matches[3].$matches[5]; - }, - $output - ); - $output = preg_replace_callback( - "/\((.*)\) (\[AS\s+)(\d+)(\])/", - function ($matches) { - return get_as($matches[1], $matches[3]); - }, - $output - ); - - return $output; - } - - return $output; -} - -/** - * Parse `bgp' output contents and return AS pathes - */ -function parse_bgp_path($output) -{ - global $os, $exec, $query, $count; - - $best = FALSE; - $pathes = array(); - - $output = str_replace("\r\n", "\n", $output); - - // MikroTik - if (preg_match("/^\/(ip|ipv6) route print detail/i", $exec) AND $os == 'mikrotik') - { - $output_parts = explode("\n" , trim($output), 4); - - if (!isset($output_parts[3])) - { - return FALSE; - } - - $summary_parts = explode("\n\n" , $output_parts[3]); - - foreach ($summary_parts as $i => $summary_part) - { - $data_exp = explode(' ', trim($summary_part), 3); - - if (strpos($data_exp[1], 'A') !== FALSE) - { - $best = $i; - } - - if (preg_match("/bgp-as-path=\"([^\"]+)\"/", $summary_part, $exp)) - { - if ($path = parse_as_path($exp[1])) - { - $pathes[] = $path; - } - } - } - - return array - ( - 'best' => $best, - 'pathes' => $pathes - ); - } - - // JunOS - if (preg_match("/^show route protocol bgp .* terse/i", $exec)) - { - $lines = explode("\n", $output); - - foreach ($lines as $line) - { - if (preg_match("/^[\+\-\*]{1}/", $line, $exp) AND strpos($line, 'Active Route') === FALSE) - { - $line = preg_replace('/ {2,}/',' ',$line); - $line = explode(' ', $line); - - if ($line[0] == '*' OR $line[0] == '+') - { - if ($count == 0) - { - $count++; - } - } - - $line = array_slice($line, 6, -1); - $line = implode(' ', $line); - - if ($path = parse_as_path($line)) - { - $pathes[] = $path; - } - } - } - - $best = $count - 1; - - return array - ( - 'best' => $best, - 'pathes' => $pathes - ); - } - - // JunOS - if (preg_match("/^show route protocol bgp /i", $exec) OR preg_match("/^show route aspath-regex /i", $exec)) - { - $lines = explode("\n", $output); - - foreach ($lines as $line) - { - if (preg_match("/^ (.)BGP /", $line, $exp)) - { - if ($exp[1] == '*') - { - if ($count == 0) - { - $count++; - } - } - } - else if (preg_match("/^:?\n?[\n\d\.A-Fa-f:\/\s]{19}([\*\+\- ]{1})\[BGP\//", $line, $exp)) - { - if ($exp[1] == '*' OR $exp[1] == '+') - { - if ($count == 0) - { - $count++; - } - } - } - - if (preg_match("/^\s+AS path: ([\d ]+)/", $line, $exp)) - { - if ($path = parse_as_path($exp[1])) - { - $pathes[] = $path; - } - } - } - - $best = $count - 1; - - return array - ( - 'best' => $best, - 'pathes' => $pathes - ); - } - - // Other OS - if (preg_match("/bgp/", $exec)) - { - if (preg_match("|(Paths: .*) best #(\d+)|", $output, $best_exp)) - { - $best = $best_exp[2] - 1; - } - - $lines = explode("\n", $output); - - foreach ($lines as $line) - { - if (( preg_match("/^ (\d+.*)/", $line) AND - !preg_match("/^ \d+\./", $line) AND - !preg_match("/[a-z\:\.]+/", $line) - ) OR - preg_match("/^ Local/", $line) OR - preg_match("/, \([^)]+\)/", $line)) - { - $line = preg_replace("/^([^\(A-z\n]+).*/", "\\1", $line); - - if ($path = parse_as_path($line)) - { - $pathes[] = $path; - } - } - } - - return array - ( - 'best' => $best, - 'pathes' => $pathes - ); - } - - return FALSE; -} - -/** - * Parse list separated by spaces - */ -function parse_list($array) -{ - preg_match_all('/([^=]+)=([^\s]+)[\n\s]/is', $array, $out); - - $array = array(); - - if (isset($out[1])) - { - foreach ($out[1] as $key => $val) - { - $array[trim($out[1][$key])] = trim($out[2][$key], '"'); - } - } - - return $array; -} - -/** - * Parse AS path - */ -function parse_as_path($line) -{ - $path = preg_split('/([^\d]+)/', trim($line)); - - $return = array(); - - foreach ($path as $asn) - { - if ($asn != '') - { - $return[] = 'AS'.$asn; - } - } - - return array_unique($return); -} - -/** - * Build list table - */ -function build_table($table_array) -{ - $size_array = array(); - - foreach ($table_array as $priority => $data) - { - foreach ($data as $field => $value) - { - $size_array[$field][$priority] = $value; - } - } - - $size_max = array(); - - foreach ($size_array as $field => $value) - { - $size_max[$field] = array_map('mb_strlen', array_map('strip_tags', $value)); - } - - $max = array_map('max', $size_max); - - $return = ''; - - foreach ($table_array as $index => $data) - { - $line = array(); - - foreach ($data as $field => $value) - { - if (reset(array_keys($data)) == $field) - { - $line[] = $value.str_repeat(' ', $max[$field] - mb_strlen(strip_tags($value))); - } - else - { - $line[] = str_repeat(' ', $max[$field] - mb_strlen(strip_tags($value))).$value; - } - } - - $return .= implode(' ', $line)."\n"; - } - - return $return; -} - -/** - * Link to execute command - */ -function link_command($command, $query, $name = '', $return_uri = FALSE) -{ - global $router, $protocol; - - if ($name == '') - { - $name = $query; - } - - $uri = '?router='.$router. - '&protocol='.$protocol. - '&command='.$command. - '&query='.$query; - - if ($return_uri) - { - return $uri; - } - - return ''.$name.''; -} - -/** - * Link to AS community - */ -function link_community($line) -{ - global $_CONFIG; - - $communitylist = preg_split('/[^\d:]+/', $line); - - foreach ($communitylist as $i => $community) - { - $communitylist[$i] = ''.$community.''; - } - - return implode(' ', $communitylist); -} - -/** - * Link to AS whois - */ -function link_as($line, $word = FALSE) -{ - global $_CONFIG; - - //print_r($line); - - return preg_replace("/(?:AS)?([\d]+)/is", - "".($word ? 'AS' : '')."\\1", $line); -} - -function get_as($ip, $original_as) -{ - if ($original_as == 15835) - { - $as = 65535; - - if($conn = fsockopen ('whois.cymru.com', 43)) - { - fputs($conn, $ip."\r\n"); - - $output = ''; - - while(!feof($conn)) - { - $output .= fgets($conn,128); - } - - $output = explode("\n", $output); - if (isset($output[1])) - { - $_as = explode("|", $output[1]); - } - - if (isset($_as[0])) - { - $as = trim($_as[0]); - - if ($as == 'NA') - { - $as = 65535; - } - } - - fclose($conn); - } - - return '(' . $ip .') [AS ' . link_as($as) . ']'; - } - else - { - return '(' . $ip .') [AS ' . link_as($original_as) . ']'; - } -} - -/** - * Link to whois IP - */ -function link_whois($line, $name = '') -{ - global $_CONFIG; - - if ($name == '') - { - $name = $line; - } - - return ''.$name.''; -} - -/** - * Get and print BGP path graph - */ -function get_path_graph($router, $query, $as_pathes, $as_best_path, $format = 'svg') -{ - global $_CONFIG, $_REQUEST; - - $font_size = 9; // default font size - $graph = new Image_GraphViz(); - - $graph->addNode($router, array - ( - 'label' => $_CONFIG['routers'][$router]['description'], - 'shape' => 'box', - 'style' => 'filled', - 'fillcolor' => '#FFCCCC', - 'fontsize' => $font_size + 2 - )); - - $query_ptr = get_ptr($query); - - // Draw requested node - $graph->addNode($query, array - ( - 'URL' => link_command('trace', $query, '', TRUE), - 'target' => '_blank', - 'label' => $query_ptr ? $query."\n".$query_ptr : $query, - 'shape' => 'box', - 'style' => 'filled', - 'fillcolor' => '#FFFFCC', - 'fontsize' => $font_size - )); - - $as_pathes_best = $as_list = $as_peer_list = array(); - - // Draw path ASs - foreach ($as_pathes as $as_path_id => $as_path_array) - { - $peer_as = reset($as_path_array); - - if (!isset($as_peer_list[$peer_as]) OR $as_peer_list[$peer_as] !== TRUE) - { - $as_peer_list[$peer_as] = ($as_best_path === $as_path_id); - } - - $as_list = array_merge($as_list, $as_path_array); - } - - $as_list = array_unique($as_list); - - // Draw our AS - if (!in_array('AS'.$_CONFIG['asn'], $as_list)) - { - $group_as = 'AS'.$_CONFIG['asn']; - $group_label = $group_as."\n".$_CONFIG['company']; - - $node_array = array - ( - 'target' => '_blank', - 'label' => $group_label, - 'style' => 'filled', - 'fillcolor' => 'white', - 'fontsize' => $font_size - ); - - if (isset($_CONFIG['routers'][$router]['group']) AND - $_CONFIG['routers'][$router]['group'] !== $_CONFIG['asn'] AND - $_CONFIG['routers'][$router]['group'] !== $group_as) - { - $group_as = 'AS'.ltrim($_CONFIG['routers'][$router]['group'], 'AS'); - $group_label = $_CONFIG['routers'][$router]['group']; - - if ($group_asinfo = get_asinfo($group_as)) - { - $group_label = isset($group_asinfo['description']) ? $group_as."\n".$group_asinfo['description'] : $group_as; - $node_array['URL'] = $_CONFIG['aswhois'].$group_as; - } - - $node_array['label'] = $group_label; - } - else - { - $node_array['URL'] = $_CONFIG['aswhois'].$group_as; - } - - $graph->addNode($group_as, $node_array); - $graph->addEdge(array($group_as => $router), array - ( - 'color' => 'red' - )); - } - - foreach ($as_list as $as_id) - { - $color = isset($as_peer_list[$as_id]) ? ($as_peer_list[$as_id] ? '#CCFFCC' : '#CCCCFF') : 'white'; - - $asinfo = get_asinfo($as_id); - - $graph->addNode($as_id, array - ( - 'URL' => $_CONFIG['aswhois'].$as_id, - 'target' => '_blank', - 'label' => isset($asinfo['description']) ? $as_id."\n".$asinfo['description'] : $as_id, - 'style' => 'filled', - 'fillcolor' => $color, - 'fontsize' => $font_size - )); - } - - // Draw pathes - foreach ($as_pathes as $as_path_id => $as_path_array) - { - $first = $last = FALSE; - - foreach ($as_path_array as $as_path) - { - if (!$first) - { - $first = $last = $as_path; - - continue; - } - - if (!isset($as_pathes_best[$last.$as_path])) - { - $graph->addEdge(array($last => $as_path), array - ( - 'color' => ($as_best_path === $as_path_id) ? 'red' : 'black' - )); - - if ($as_best_path === $as_path_id) - { - $as_pathes_best[$last.$as_path] = TRUE; - } - } - - $last = $as_path; - } - - if (!isset($as_pathes_best[$router.$first])) - { - $graph->addEdge(array($router => $first), array - ( - 'color' => ($as_best_path === $as_path_id) ? 'red' : 'black' - )); - - if ($as_best_path === $as_path_id) - { - $as_pathes_best[$router.$first] = TRUE; - } - } - } - - // Draw last path - $graph->addEdge(array($as_path => $query), array - ( - 'color' => ($as_pathes_best) ? 'red' : 'black' - )); - - $graph->image($format); - - exit; -} - -/** - * Get and print blank image - */ -function get_blank_graph($string, $format = 'svg') -{ - $graph = new Image_GraphViz(); - - $graph->addNode('error', array - ( - 'label' => $string, - 'shape' => 'none', - 'fontcolor' => 'red', - 'fontsize' => 14 - )); - - $graph->image($format); - - exit; -} - -/** - * Get address information from RADb - */ -function get_radb($request) -{ - if (!$fp = @fsockopen('whois.radb.net', 43, $errnum, $errstr, 10)) - { - return FALSE; - } - - fputs($fp, $request."\r\n"); - - $data = ''; - - while (!feof($fp)) - { - $data .= fgets($fp, 128); - } - - fclose($fp); - - $exp = explode("\n\n", $data); - - if (!isset($exp[0]) OR empty($exp[0])) - { - return FALSE; - } - - $lines = explode("\n", $exp[0]); - - $return = array(); - - foreach ($lines as $line) - { - $line = explode(':', $line, 2); - - if (isset($line[0]) AND isset($line[1])) - { - $return[$line[0]] = trim($line[1]); - } - } - - if (sizeof($return) < 1) - { - return FALSE; - } - - return $return; -} - -/** - * Get AS name information from "Team Cymru - IP to ASN Mapping" - */ -function get_asinfo($request) -{ - if (!$dns = dns_get_record($request.'.asn.cymru.com', DNS_TXT) OR !isset($dns[0]['txt'])) - { - return FALSE; - } - - $segments = array_map('trim', explode('|', $dns[0]['txt'], 5)); - - if (sizeof($segments) != 5) - { - return FALSE; - } - - list($segments[4], $segments[5]) = explode(' ', $segments[4], 2); - - $segments[5] = str_replace('_', '"', $segments[5]); - - return array - ( - 'asn' => $segments[0], - 'country' => $segments[1], - 'registrar' => $segments[2], - 'regdate' => $segments[3], - 'asname' => $segments[4], - 'description' => $segments[5], - ); -} - -/** - * Get PTR record for IPv4/IPv6 address - */ -function get_host($hostname) -{ - global $protocol; - - if (filter_var($hostname, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) - { - return $hostname; - } - - $dns_a = (($dns_a = dns_get_record($hostname, DNS_A)) !== FALSE) ? $dns_a : array(); - $dns_aaaa = (($dns_aaaa = dns_get_record($hostname, DNS_AAAA)) !== FALSE) ? $dns_aaaa : array(); - - $ip_array = array(); - - foreach (array_merge($dns_aaaa, $dns_a) as $record) - { - switch ($record['type']) - { - case 'A': - $ip_array[] = $record['ip']; - break; - - case 'AAAA': - $ip_array[] = $record['ipv6']; - break; - - case 'CNAME': - $ip_array = array_push($ip_array, get_host($record['target'])); - break; - } - } - - $ipv4_array = $ipv6_array = array(); - - foreach ($ip_array as $ip) - { - if (strpos($ip, ':') !== FALSE) - { - $ipv6_array[] = $ip; - } - else - { - $ipv4_array[] = $ip; - } - } - - $ip_array = ($protocol == 'ipv6') - ? $ipv6_array - : $ipv4_array; - - if (!$ipv6_array OR !$ipv4_array) - { - $ip_array = array_merge($ipv6_array, $ipv4_array); - } - - if (!$ip_array OR (!$ipv4_array AND $protocol != 'ipv6')) - { - return FALSE; - } - - return end($ip_array); -} - -/** - * Get PTR record for IPv4/IPv6 address - */ -function get_ptr($ip) -{ - if (preg_match("/:/", $ip)) - { - $unpack = unpack('H*hex', @inet_pton($ip)); - $arpa = implode('.', array_reverse(str_split($unpack['hex']))).'.ip6.arpa'; - } - else - { - $arpa = implode('.', array_reverse(explode('.', $ip))).'.in-addr.arpa.'; - } - - if (!$arpa OR !$dns = dns_get_record($arpa, DNS_PTR)) - { - return $ip; - } - - $ptr = array(); - - foreach ($dns as $record) - { - if ($record['type'] == 'PTR') - { - $ptr[] = $record['target']; - } - } - - if (!$ptr) - { - return $ip; - } - - return reset($ptr); -} - -/** - * Group router list - */ -function group_routers($array) -{ - $return = array(); - - foreach ($array as $key => $value) - { - if (isset($value['group']) AND $value['group'] != '') - { - $return[$value['group']][$key] = $value; - - continue; - } - - $return[''][$key] = $value; - } - - return $return; -} - -// ------------------------------------------------------------------------ - -/** -* Telnet class -* -* Used to execute remote commands via telnet connection -* Usess sockets functions and fgetc() to process result -* -* All methods throw Exceptions on error -* -* Written by Dalibor Andzakovic"; +// var_dump($pathes); +// echo ""; return array ( 'best' => $best, @@ -2148,6 +2154,41 @@ function parse_bgp_path($output) ); } + // Huawei + if ($os == 'huawei') + { + $output_parts = explode("BGP local router ID :" , trim($output), 4); + $output_parts = explode("AS-path " , trim($output_parts[1]), 4); + if (!isset($output_parts[1])) + { + return FALSE; + } + $output_parts = explode("," , trim($output_parts[1]), 4)[0]; + + + $summary_parts[] = $output_parts; + + + foreach ($summary_parts as $i => $summary_part) + { + + $data_exp = str_replace(' ', ',',trim($summary_part)); + + + if ($path = parse_as_path($data_exp)) + { + $pathes[] = $path; + } + } + + return array + ( + 'best' => 0, + 'pathes' => $pathes + ); + } + + // JunOS if (preg_match("/^show route protocol bgp .* terse/i", $exec)) { @@ -2479,10 +2520,13 @@ function link_whois($line, $name = '') function get_path_graph($router, $query, $as_pathes, $as_best_path, $format = 'svg') { global $_CONFIG, $_REQUEST; + + // echo "Vai renderizar o Gráfico
Please wait...
"; -// var_dump($pathes); -// echo ""; return array ( 'best' => $best, @@ -2648,13 +2628,9 @@ function link_whois($line, $name = '') function get_path_graph($router, $query, $as_pathes, $as_best_path, $format = 'svg') { global $_CONFIG, $_REQUEST; - - // echo "Vai renderizar o Gráfico
W%KPD8LJ?LZ!8Tm;b+8 t`bf^b~|AS_|9dTSgq9
zRF5e3=|^vuq0o#jtWvnu%)JuOk$DwQ<3OFJC;sb6V@pL<%JKs_9ZgYB%-ykU7qlJ}
z &6lcN=a=W5}&E?_Z4#R5A^82XrpY(e`gY1V>&2+cb{AYhto*C
zR@{dva$WXryWWkI@)lSVKKShn{ml7;#F+{GnuG2Kp2c|sD^9$JjiRHkR{1%crH9r(
z9df3cA_!+~OQUp6p|38f98q>E+V;e;u&|}byiI{=4;GDMwdj}&zSq#)B=}l*V_?kQ
z3;A(Cl2fiFR{E}X><6ZhN`c(ToanHe&lB2FoY(vmKHXfgl~b7~aq^`Mk1Q~lpxd_c
znjW6N%)ypwoSwF0TQD@ZMAerick(3&MLP^}=T6T?!6+1j|qQFqac4CJN$14B$%
z+P1Ik{(N!!T}}";var_dump($data_exp);echo "
";
+
+
+ if ($path = parse_as_path($data_exp))
+ {
+ $pathes[] = $path;
+ }
+ }
+ $best = getBestHuawei();
+ }else
{
-
- $summary_part = explode("AS-path " , trim($summary_part));
-
- if(preg_match("/best/i", $summary_part[1])){
- // Se esta for a melhor rota, sinaliza ela
- $best = $i;
+ $output_parts = explode("BGP local router ID :" , trim($output), 4);
+ $output_parts = explode("Paths:" , trim($output_parts[1]), 2);
+ $output_parts = explode("BGP routing table entry information of " , trim($output_parts[1]),2);
+ // Aqui eu finalmente tenho os blocos de informações separados por path
+ $output_parts = explode("\n\n" , trim($output_parts[1]));
+ if (!isset($output_parts[0]))
+ {
+ return FALSE;
}
-
- $summary_part = explode("," , trim($summary_part[1]))[0];
- $data_exp = str_replace(' ', ',',trim($summary_part));
-
- if ($path = parse_as_path($data_exp))
+
+ $summary_parts = $output_parts;
+
+
+ foreach ($summary_parts as $i => $summary_part)
{
- $pathes[] = $path;
+
+ $summary_part = explode("AS-path " , trim($summary_part));
+
+ if(preg_match("/best/i", $summary_part[1])){
+ // Se esta for a melhor rota, sinaliza ela
+ $best = $i;
+ }
+
+ $summary_part = explode("," , trim($summary_part[1]))[0];
+ $data_exp = str_replace(' ', ',',trim($summary_part));
+
+
+ if ($path = parse_as_path($data_exp))
+ {
+ $pathes[] = $path;
+ }
}
}
@@ -3017,6 +3058,31 @@ function buscabarra($haystack, $needle, $nth) {
return $pos;
}
+function getBestHuawei() {
+ global $url,$os,$protocol, $queries,$query;
+ $exe = str_replace("%s",$query,$queries[$os][$protocol]['bgp']);
+ $test = process($url, $exe, TRUE);
+
+ $ddd = explode("BGP local router ID :" , trim($test), 4);
+ $ddd = explode("Paths:" , trim($ddd[1]), 2);
+ $ddd = explode("BGP routing table entry information of " , trim($ddd[1]),2);
+ // Aqui eu finalmente tenho os blocos de informações separados por path
+ $ddd = explode("\n\n" , trim($ddd[1]));
+
+ $eee = $ddd;
+ foreach ($eee as $i => $sp)
+ {
+
+ $sp = explode("AS-path " , trim($sp));
+
+ if(preg_match("/best/i", $sp[1])){
+ // Se esta for a melhor rota, sinaliza ela
+ $best = $i;
+ }
+ }
+ return $best;
+}
+
/**
* Group router list
From 50e12747bd11256d8cc2267e64eeeb114dd0fddd Mon Sep 17 00:00:00 2001
From: root ";var_dump($data_exp);echo "
";
-
+ if($data_exp == "Nil") $data_exp = $_CONFIG['asn'];
if ($path = parse_as_path($data_exp))
{
From 494f564c3f7f503ba92524346906677af12e96e1 Mon Sep 17 00:00:00 2001
From: root ";
+ $titles = "
";
+
+ }
+ }else if($os == "huawei" and $command == "advertised-routes" and $protocol == "ipv6"){
+ // echo "Pulei";
+ $fp = @popen('echo n | '.$ssh_path.' '.implode(' ', $params).' '.$exec, 'r');
+ if ($fp) {
+ $output = '';
+ while (!feof($fp)) {
+ $output .= fgets($fp, 1024);
+ }
+ pclose($fp);
+
+ // Remove tabulações, quebras de linha e excessos de espaços
+ $output = preg_replace('/[\t\r\n]+/', ' ', $output);
+ $output = preg_replace('/\s{2,}/', ' ', $output);
+
+
+
+ // Padrões de regex para capturar os campos relevantes
+ $patterns = [
+ 'Network' => '/Network\s*:\s*(.*?)\s*PrefixLen\s*:\s*(\d+)/',
+ 'NextHop' => '/NextHop\s*:\s*(.*?)\s*LocPrf\s*:\s*(\d*)/',
+ 'MED' => '/MED\s*:\s*(\d+)/',
+ 'PathOgn' => '/Ogn\s*:\s*(\d+[a-zA-Z]?)\b/'
+ ];
+
+ // Inicializa a tabela HTML
+ echo "
+ ";
+ echo $titles;
+
+ foreach($t as $linha){
+ if(strlen($linha) < 20) continue;
+ if(preg_match("/^Info: The max number of VTY /i",$linha)) continue;
+
+ $linha = preg_replace('/\\s\\s+/', ' ', $linha);
+ $linha = explode(' ', $linha);
+
+ $Network = $linha[2];
+ $NextHop = $linha[3];
+ $MED = $linha[4];
+ $LocPrf = $linha[5];
+ $Path = $linha[6];
+
+ $Network = preg_replace(['/\s\s+/', '/\//'], ' ', $Network);
+
+ $trupa = " Network
+ NextHop
+ MED
+ LocPrf
+ Path
+ Comunidades
+
+ ";
+ echo $trupa;
+ // var_dump($linha);
+ }
+
+ echo "$Network
+ $NextHop
+ $MED
+ $LocPrf
+ $Path
+ ".link_command2("advertised-communities", array($query, $Network ), $name = 'Comunidades', $return_uri = FALSE)."
+ ";
+ echo "
";
+ }
+ }else
if ($fp = @popen('echo n | '.$ssh_path.' '.implode(' ', $params).' '.$exec, 'r'))
{
// echo 'echo n | '.$ssh_path.' '.implode(' ', $params).' '.$exec;
@@ -980,9 +1141,54 @@ function process($url, $exec, $return_buffer = FALSE)
*/
function parse_out($output, $check = FALSE)
{
- global $_CONFIG, $router, $protocol, $os, $command, $exec, $query, $index, $lastip, $best, $count, $str_in, $ros;
+ global $anunciadas, $_CONFIG, $router, $protocol, $os, $command, $exec, $query, $index, $lastip, $best, $count, $str_in, $ros;
// Huawei
+// if(preg_match('/advertised-routes/i', $exec)){
+// $output = str_replace("\r\n", "", $output);
+// if (strlen($output) < 20) return;
+// if (preg_match("/^Info: The max number of VTY /i",$output)) return;
+// $output = preg_replace('/\\s\\s+/', ' ', $output);
+// if (preg_match("/^ The current login time /i",$output)) return;
+// if (preg_match("/^ Status codes: /i",$output)) return;
+// if (preg_match("/^ h - history, /i",$output)) return;
+// if (preg_match("/^ Network NextHop MED LocPrf PrefVal /i",$output)) return;
+// if (preg_match("/^ BGP Local router ID is /i",$output)) return;
+// if (preg_match("/^ Origin /i",$output)) return;
+// if (preg_match("/RPKI/i",$output)) return;
+// if (preg_match("/^ Total Number of Routes: /i",$output)) return;
+//
+// $item = explode(" ",$output);
+//
+// $titles = "
+ ";
+
+ // Encontra todas as correspondências usando os padrões de regex
+ foreach ($patterns as $key => $pattern) {
+ preg_match_all($pattern, $output, $matches[$key]);
+
+ }
+ // var_dump($matches);
+ // Itera sobre as correspondências encontradas
+ $numEntries = count($matches['Network'][1]); // Número de entradas de rota
+
+ for ($i = 0; $i < $numEntries; $i++) {
+ // Extrai os campos relevantes de cada entrada
+ $PrefixLen = isset($matches['Network'][2][$i]) ? $matches['Network'][2][$i] : '';
+ $Network = isset($matches['Network'][1][$i]) ? $matches['Network'][1][$i] . ' ' . $PrefixLen : '';
+ $NextHop = isset($matches['NextHop'][1][$i]) ? $matches['NextHop'][1][$i] : '';
+ $LocPrf = isset($matches['NextHop'][2][$i]) ? $matches['NextHop'][2][$i] : '';
+ $MED = isset($matches['MED'][1][$i]) ? $matches['MED'][1][$i] : '';
+ $Path = isset($matches['PathOgn'][1][$i]) ? $matches['PathOgn'][1][$i] : '';
+
+ // Ajusta o formato do Network (removendo espaços duplos e substituindo '/' por espaço)
+ $Network = preg_replace('/\s\s+/', ' ', $Network);
+ $Network = str_replace('/', ' ', $Network);
+
+ // Cria a linha da tabela HTML
+ $row = "Network
+ NextHop
+ MED
+ LocPrf
+ Path
+ Comunidades
+
+ ";
+
+ // Exibe a linha na tabela
+ echo $row;
+ }
+ // Fecha a tabela HTML
+ echo "$Network
+ $NextHop
+ $MED
+ $LocPrf
+ $Path
+ " . link_command2("advertised-communities", array($query, $Network), 'Comunidades', FALSE) . "
+
+// ";
+// // echo $titles;
+//
+// $Network = $item[2];
+// $NextHop = $item[3];
+// $Path = $item[6];
+// $Comunidades = "";
+//
+// $trupa .= " Network
+// NextHop
+// Path
+// Comunidades
+//
+// ";
+// // echo $trupa;
+//
+// $tabela .= "$Network
+// $NextHop
+// $Path
+// $Comunidades
+// ";
+// // $tabela .= $titles;
+// $tabela .= $trupa;
+// $tabela .= "
";
+// return $tabela;
+// }
+
+
if (preg_match('/^display bgp peer/i', $exec) or preg_match('/^display bgp ipv6 peer/i', $exec))
{
$output = str_replace("\r", "", $output);
@@ -2593,6 +2799,28 @@ function link_command($command, $query, $name = '', $return_uri = FALSE)
return ''.$name.'';
}
+function link_command2($command, $queries, $name = '', $return_uri = FALSE)
+{
+ global $router, $protocol;
+
+ if ($name == '') {
+ $name = implode(' ', $queries);
+ }
+
+ $queryString = implode('&query[]=', $queries);
+ $uri = '?router='.$router.
+ '&protocol='.$protocol.
+ '&command='.$command.
+ '&query[]='.$queryString;
+
+ if ($return_uri) {
+ return $uri;
+ }
+
+ return ''.$name.'';
+}
+
+
/**
* Link to AS community
*/
From 3ecf70685895cdcb062bd5f43e7886d359a79d36 Mon Sep 17 00:00:00 2001
From: root AS Looking Glass
@@ -280,7 +280,7 @@
'advertised-communities' => 'display bgp ipv6 routing-table peer %s advertised-routes %s community-list | no-more',
'received-routes' => 'display bgp ipv6 routing-table peer %s received-routes | no-more',
'routes' => 'display bgp ipv6 routing-table peer %s received-routes active | no-more',
- 'summary' => 'display bgp ipv6 peer',
+ 'summary' => 'display bgp ipv6 peer | no-more',
'ping' => 'ping ipv6 %s',
'trace' => 'tracert ipv6 %s',
)
@@ -636,6 +636,7 @@ function process($url, $exec, $return_buffer = FALSE)
switch ($url['scheme'])
{
case 'ssh':
+ $url['host'] = trim($url['host'], '[]'); //Ajusta IPV6
if(! empty($_CONFIG['routers'][$router]['sshauthtype']))
{
$sshauthtype = $_CONFIG['routers'][$router]['sshauthtype'];
diff --git a/htdocs/lg_logo.gif b/htdocs/lg_logo.gif
deleted file mode 100755
index cda0befdcee4a74f346722a5524bc50bf00979f8..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001
literal 4733
zcmZvce>{^5`^WF?zVB^j)@_vJM-z%tXDBtPMxw37SUJwB9VAKREJ~%0W>_q(nKi5k
zX@!y`Nu?Qy6=zmaPI+e4I?Z`}wWFQB>pXmW{{LQoUa#x*y5679>vLU^^oTXoL_bsn
z@V60&h>BCDbJ{vO%pJADl(@06vEdv}Qe2!sB({#}OjTJLjm9M6$`w^+vpFs{j+>UA
zmY$_ntA~e&E%LM(vq{;|AQnq4gB>Q5DJxrGlBdUVlUmx^as)y~9A{9ctEyF|XJuQh
z*3Qn($tE#}n_@5;$K+Wev9zhB#nRhkH5ts(l$5j#MQv@bM$^#JmXVd+(a~X&rj3q`
zH8eGe1!PLRR(A3mY&)AUD#>)6lo&f^!p8T-#
zcZcMK|3eu6!UJ#&-i|Xf_de6Wu1yACC%@{g0TJZ(hVXHFV_bOy!2n}m5L@Lkr))Hq
z&9UA%q+XC9$<-&lBFcRc3y0P3p5MVmF!;hb5Vs5uB{w-1L=_Wqfk4er|Mr<5DFaqC
zwKBdf!E=!t@2n2*0_FolD|?>H+?URZxEHWh>>LekOyN9u+&E`Xu!t~JmAPriF7gU+
z^YHNTa`WBbzo7{fZ~Q6>XmUUN3po!yAj>yySl=V_CwsgL*gyP?hk^Ur842XVl^-3_
zoykFW`++o_sTJK1DsByKA}nk`sZJ;c80-AE8weO1atnm{klLl0RRRCW24)NKUe;SD
z_n`H@6nf~uyIXw24_I(+@o=BNpDGZw6h6zjH4>MS4aVKT7!J_AFu+m4Ng5}(l;YLJ
z?e&TK0w4r65g2@miNOuxraddsW0G@|J|)(qH8<*ex%ghf=Sji^ckM>|`}YgEWwFk)
zVS&$`!v=#fCAeYS(4S>)upiHlyyv9H>F^yxPPnxtm!X+HT#w7fQQyK8?rf;f1%$4H
zqdk{3+}?(p`m8@jOj2&*pL`&gzwQ{mS6{*;#UyUiA>w4eB9LsUe^-|8_Bs8vNEGWg
zi&5jB$&LxZz<&BS`$N3JV34bcxI1*#dwN*Ayv(X1X^8MncMLJh9=0+^-@##8-wt$|
zU>RdZin1^Ki-``)$x4Supw9ZLB<`@wjM3t|Sn`E?=vUdH_PL=z-)R(b9XCjWN;&aeP9`~g3B
z