* http://www.hsdn.org/
*
* Copyright (C) 2000-2002 Cougar 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
flush();
}
$queries = array
(
'ios' => array
(
'ipv4' => array
(
'bgp' => 'show ip bgp %s',
'advertised-routes' => 'show ip bgp neighbors %s advertised-routes',
'received-routes' => 'show ip bgp neighbors %s received-routes',
'routes' => 'show ip bgp neighbors %s routes',
'summary' => 'show ip bgp summary',
'ping' => 'ping ip %s',
'trace' => 'traceroute ip %s'
),
'ipv6' => array
(
'bgp' => 'show bgp ipv6 unicast %s',
'advertised-routes' => 'show bgp ipv6 neighbors %s advertised-routes',
'received-routes' => 'show bgp ipv6 neighbors %s received-routes',
'routes' => 'show bgp ipv6 neighbors %s routes',
'summary' => 'show bgp ipv6 unicast summary',
'ping' => 'ping ipv6 %s',
'trace' => 'traceroute ipv6 %s'
)
),
'quagga' => array
(
'ipv4' => array
(
'bgp' => 'show ip bgp %s',
'advertised-routes' => 'show ip bgp neighbors %s advertised-routes',
'received-routes' => 'show ip bgp neighbors %s received-routes',
'routes' => 'show ip bgp neighbors %s routes',
'summary' => 'show ip bgp summary',
'ping' => 'ping -t 5 -c 5 %s',
'trace' => 'traceroute -Iaw 1 %s',
),
'ipv6' => array
(
'bgp' => 'show ipv6 bgp %s',
'advertised-routes' => 'show ipv6 bgp neighbors %s advertised-routes',
'received-routes' => 'show ipv6 bgp neighbors %s received-routes',
'routes' => 'show ipv6 bgp neighbors %s routes',
'summary' => 'show ipv6 bgp summary',
'ping' => 'ping6 -c 5 %s',
'trace' => 'traceroute6 -Ialw 1 %s',
)
),
'mikrotik' => array
(
'ipv4' => array
(
'bgp' => '/ip route print detail where dst-address=%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 dst-address=%s',
'advertised-routes' => '/routing bgp advertisements print peer=%s',
'routes' => '/ipv6 route print where gateway=%s',
'summary' => '/routing bgp peer print status where address-families=ipv6',
'ping' => '/ping count=5 size=56 %s',
'trace' => '/tool traceroute %s size=60 count=1',
)
),
'junos' => array
(
'ipv4' => array
(
'bgp' => 'show bgp %s',
'advertised-routes' => 'show route advertising-protocol bgp %s',
'routes' => 'show route protocol bgp %s terse',
'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 protocol bgp %s terse',
'summary' => 'show bgp summary',
'ping' => 'ping count 5 %s',
'trace' => 'traceroute %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);
$os = $_CONFIG['routers'][$router]['os'];
if ($command == 'graph' AND isset($queries[$os][$protocol]['bgp']))
{
$exec = $queries[$os][$protocol]['bgp'];
}
else
{
$exec = $queries[$os][$protocol][$command];
}
if (strpos($exec, '%s') !== FALSE)
{
if (preg_match('/^.[.a-z0-9_-]+\.[a-z]+$/i', $query))
{
if ($command != 'advertised-routes')
{
$query = get_host($query);
}
}
if ($query AND ($command == 'bgp' OR $command == 'graph') AND ($os == 'mikrotik' OR ($protocol == 'ipv6' AND $os == 'ios')))
{
if (strpos($query, '/') === FALSE AND $radb = get_radb($query))
{
$route = FALSE;
if (strpos($query, ':') AND isset($radb['route6']))
{
$route = $radb['route6'];
}
else if (isset($radb['route']))
{
$route = $radb['route'];
}
if ($route)
{
if ($command != 'graph')
{
print '
';
}
if ($exec)
{
if ($os == 'junos')
{
// @see JunOS Routing Table Names (http://www.net-gyver.com/?p=602)
$table = ($protocol == 'ipv6') ? 'inet6.0' : 'inet.0';
if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+)$/", $exec, $exec_exp))
{
$exec = 'show bgp neighbor '.$exec_exp[1];
}
else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) ro\w*$/", $exec, $exec_exp))
{
$exec = 'show route receive-protocol bgp '.$exec_exp[1];
}
else if (preg_match("/^show bgp neighbors ([\d\.A-Fa-f:]+) routes all$/", $exec, $exec_exp))
{
$exec = 'show route receive-protocol bgp '.$exec_exp[1].' all';
}
else if (preg_match("/^show bgp neighbors ([\d\.A-Fa-f:]+) routes damping suppressed$/", $exec, $exec_exp))
{
$exec = 'show route receive-protocol bgp '.$exec_exp[1].' damping suppressed';
}
else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) advertised-routes ([\d\.A-Fa-f:\/]+)$/", $exec, $exec_exp))
{
$exec = 'show route advertising-protocol bgp '.$exec_exp[1].' '.$exec_exp[2].' exact detail';
}
else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) receive-protocol ([\d\.A-Fa-f:\/]+)$/", $exec, $exec_exp))
{
$exec = 'show route receive-protocol bgp '.$exec_exp[1].' '.$exec_exp[2].' exact detail';
}
else if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+) a[\w\-]*$/", $exec, $exec_exp))
{
$exec = 'show route advertising-protocol bgp '.$exec_exp[1];
}
else if (preg_match("/^show bgp\s+([\d\.A-Fa-f:]+\/\d+)$/", $exec, $exec_exp))
{
$exec = 'show route protocol bgp '.$exec_exp[1].' terse exact';
}
else if (preg_match("/^show bgp\s+([\d\.A-Fa-f:]+)$/", $exec, $exec_exp))
{
$exec = 'show route protocol bgp '.$exec_exp[1].' table '.$table; // terse
}
else if (preg_match("/^show bgp\s+([\d\.A-Fa-f:\/]+) exact$/", $exec, $exec_exp))
{
$exec = 'show route protocol bgp '.$exec_exp[1].' exact detail all';
}
else if (preg_match("/^show bgp re\s+(.*)$/", $exec, $exec_exp))
{
$re = $exec_exp[1];
if (!preg_match('/^\^/', $re))
{
$re = "^.*".$re;
}
if (!preg_match('/\$$/', $re))
{
$re = $re.".*\$";
}
$exec = 'show route aspath-regex "'.str_replace('_', ' ', $re).'" all';
}
}
if ($command == 'graph')
{
@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
?>
}
// HTML footer
?>
Information: RIPEstat he.net robtex.com Peering Map PeeringDB
Copyright ©
Command aborted.
'; } break; } if ($line === FALSE OR $return_buffer === TRUE) { $lines .= $output; continue; } print $line; flush(); if ($line === NULL) { $line = $output; } } pclose($fp); } 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}$/'; } $telnet = new Telnet($url['host'], $url['port']); $telnet->connect(); $telnet->login($url['user'], $url['pass']); $telnet->write(($os == 'junos') ? $exec.' | no-more' : $exec); $buffer = ''; $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 //$c = preg_replace('/[\x80-\xFF]/x', ' ', $c); // Strip Ext ASCII //$c = preg_replace('/[\x00-\x09\x0B-\x1F]/x', ' ', $c); // Strip Low ASCII $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 = ''; } $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); } catch (Exception $exception) { if (!$return_buffer) { print 'Telnet error: '.$exception->getMessage().'
'; } } break; } if ($return_buffer) { return $lines; } if ($lines AND ($line = parse_out($lines))) { print $line; } if ($line == '' AND !isset($exception)) { print 'Command failed.
'; } 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[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("/bgp-as-path=\"([^\"]+)\"/ex", "stripslashes('bgp-as-path=\"'.link_as('\\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("/^(.{8}\s)([\d\.A-Fa-f:\/]+)(\s+)/e", '"\\1".link_command("bgp", "\\2")."\\3"', $output); } // MikroTik if (preg_match("/^\/(ip|ipv6) route print/i", $exec) AND $os == 'mikrotik') { return preg_replace("/(^[\s\d]+)(\s+[A-z]+\s+)([\d\.A-Fa-f:\/]+)(\s+)/e", '"\\1\\2".link_command("bgp", "\\3")."\\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("/( local AS number )(\d+)/e", '"\\1".link_as("\\2")', $output); // JunOS if ($os == 'junos') { $output = preg_replace("/^([\dA-Fa-f:\.]+\s+)(\d+)/e", '"\\1".link_as("\\2")', $output); } else { $output = preg_replace("/^([\d\.]+\s+\d+\s+)(\d+)/e", '"\\1".link_as("\\2")', $output); } $output = preg_replace("/^(\d+\.\d+\.\d+\.\d+)(\s+.*\s+)([1-9]\d*)\n$/e", '"\\1\\2".link_command("routes", "\\1", "\\3")."\n"', $output); $output = preg_replace("/^(\d+\.\d+\.\d+\.\d+)(\s+)/e", 'link_command("bgp", "neighbors+\\1", "\\1")."\\2"', $output); // IPv6 neighbours $output = preg_replace("/^(.{15} 4\s+)(\d+)/e", '"\\1".link_as("\\2")', $output); $output = preg_replace("/^([\dA-Fa-f]*:[\dA-Fa-f:]*)(\s+)/e", 'link_command("bgp", "neighbors+\\1", "\\1")."\\2"', $output); $output = preg_replace("/^([\dA-Fa-f]*:[\dA-Fa-f:]*)\n$/e", 'link_command("bgp", "neighbors+\\1", "\\1")."\n"', $output); return $output; } if ($exec == 'show ipv6 bgp summary' OR $exec == 'show bgp ipv6 unicast summary') { $output = preg_replace("/^(.{15} 4\s+)(\d+)/e", '"\\1".link_as("\\2")', $output); if (preg_match("/^([\dA-Fa-f]*:[\dA-Fa-f:]*)\s+4\s+/", $output, $lastip_exp)) { $lastip = $lastip_exp[1]; $output = preg_replace("/^(.{15} 4\s+)(\d+)/e", '"\\1".link_as("\\2")', $output); $output = preg_replace("/^([\dA-Fa-f:]+)(\s+.*\s+)([1-9]\d*)$/e", 'link_command("routes", "'.$lastip.'", "\\3")', $output); $output = preg_replace("/^([\dA-Fa-f:]+)(\s+)/e", 'link_command("routes", "\\1")."\\2"', $output); unset($lastip); } if (preg_match("/^([\dA-Fa-f:]+)\n$/", $output, $lastip_exp)) { $lastip = $lastip_exp[1]; $output = preg_replace("/^([\dA-Fa-f:]+)/e", 'link_command("routes", "\\1")', $output); } if (isset($lastip) AND preg_match("/^(\s+.*\s+)([1-9]\d*)\n$/", $output)) { $output = preg_replace("/^(\s+.*\s+)([1-9]\d*)\n$/e", '"\\1".link_command("routes", "'.$lastip.'", "\\2")."\n"', $output); unset($lastip); } return $output; } if ($exec == 'show bgp summary') { // JunOS if (preg_match("/^([\dA-Fa-f:][\d\.A-Fa-f:]+)\s+/", $output, $lastip_exp)) { $lastip = $lastip_exp[1]; // IPv4 $output = preg_replace("/^(\d+\.\d+\.\d+\.\d+)(\s+.*\s+)([1-9]\d*)(\s+\d+\s+\d+\s+\d+\s+\d+\s+[\d:]+\s+)(\d+)\/(\d+)\/(\d+)(\s+)/e", '"\\1\\2".link_command("routes", "\\1", "\\3")."\\4".link_command("routes", "\\1", "\\5")."/".link_command("routes", "\\1", "\\6")."/".link_command("routes", "\\1", "\\7")."\\8"', $output); // IPv4/IPv6 $output = preg_replace("/^([\dA-Fa-f:][\d\.A-Fa-f:]+\s+)(\d+)(\s+)/e", '"\\1".link_as("\\2")."\\3"', $output); $output = preg_replace("/^([\dA-Fa-f:][\d\.A-Fa-f:]+)(\s+)/e", 'link_command("bgp", "neighbors+\\1", "\\1")."\\2"', $output); } if (isset($lastip) AND preg_match("/( [^:]+: )(\d+)\/(\d+)\/(\d+)$/", $output)) { $output = preg_replace("/^( [^:]+: )(\d+)\/(\d+)\/(\d+)\n$/e", '"\\1".link_command("routes", "'.$lastip.'", "\\2")."/".link_command("bgp", "neighbors+'.$lastip.'+routes+all", "\\3")."/".link_command("bgp", "neighbors+'.$lastip.'+routes+damping+suppressed", "\\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("/^([\*r ](>|d|h| ).{59})([\d\s,\{\}]+)([ie\?])\n$/e", '"\\1".link_as("\\3")."\\4\n"', $output); $output = preg_replace("/^([\*r ](>|d|h| )[i ])([\d\.A-Fa-f:\/]+)(\s+)/e", '"\\1".link_command("bgp", "\\3")."\\4"', $output); $output = preg_replace("/^([\*r ](>|d|h| )[i ])([\d\.A-Fa-f:\/]+)\n$/e", '"\\1".link_command("bgp", "\\3")."\n"', $output); $output = preg_replace("/^(( ){20}.{41})([\d\s,\{\}]+)([ie\?])\n$/e", '"\\1".link_as("\\3")."\\4\n"', $output); $output = preg_replace("/(, remote AS )(\d+)(,)/e", '"\\1".link_as("\\2")."\\3\n"', $output); return $output; } // JunOS if (preg_match("/^show route receive-protocol bgp\s+[\d\.A-Fa-f:]+/i", $exec)) { $output = preg_replace("/(Community: )([\d: ]+)/e", '"\\1".link_community("\\2")', $output); $output = preg_replace("/(Communities: )([\d: ]+)/e", '"\\1".link_community("\\2")', $output); $output = preg_replace("/(^\s+AS path: )([\d ]+)/e", '"\\1".link_as("\\2")', $output); $output = preg_replace("/^([\d\.\s].{24})([\d\.]+)(\s+)/e", '"\\1".link_command("bgp", "neighbors+\\2", "\\2")', $output); $output = preg_replace("/^([\d\.\/]+)(\s+)/e", 'link_command("bgp", "\\1")."\\2"', $output); $output = preg_replace("/^([\d\.A-Fa-f:\/]+)(\s+)/e", 'link_command("bgp", "\\1+exact", "\\1")."\\2"', $output); $output = preg_replace("/^([\d\.A-Fa-f:\/]+)\s*\n$/e", 'link_command("bgp", "\\1+exact", "\\1")', $output); $output = preg_replace("/^([ \*] )([\d\.A-Fa-f:\/]+)(\s+)/e", '"\\1".link_command("bgp", "\\2+exact", "\\2")."\\3"', $output); return $output; } // JunOS if (preg_match("/^show route advertising-protocol bgp\s+([\d\.A-Fa-f:]+)$/i", $exec, $ip_exp)) { $ip = $ip_exp[1]; $output = preg_replace("/^([\d\.\s].{64})([\d\s,\{\}]+)([I\?])\n$/e", '"\\1".link_as("\\2")."\\3\n"', $output); $output = preg_replace("/^([\d\.\s].{24})([\d\.]+)(\s+)/e", '"\\1".link_command("bgp", "neighbors+\\2", "\\2")."\\3"', $output); $output = preg_replace("/^([\d\.\/]+)(\s+)/e", 'link_command("bgp", "\\1")."\\2"', $output); $output = preg_replace("/^([\d\.A-Fa-f:\/]+)(\s+)/e", 'link_command("bgp", "\\1+exact", "\\1")."\\2"', $output); $output = preg_replace("/^([\d\.A-Fa-f:\/]+)\s*\n$/e", 'link_command("bgp", "\\1+exact", "\\1")."\n"', $output); $output = preg_replace("/^([ \*] )([\d\.A-Fa-f:\/]+)(\s+)/e", '"\\1".link_command("bgp", "neighbors+"'.$ip.'"+advertised-routes+\\2", "\\2")."\\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("/(Prefix )(advertised)( [1-9]\d*)/e", '"\\1".link_command("advertised-routes", "'.$lastip.'", "\\2")."\\3"', $output); $output = preg_replace("/( Prefixes Total: )(\d+)( )/e", '"\\1".link_command("advertised-routes", "'.$lastip.'", "\\2")."\\3"', $output); $output = preg_replace("/(prefixes )(received)( [1-9]\d*)/e", '"\\1".link_command("routes", "'.$lastip.'", "\\2")."\\3"', $output); $output = preg_replace("/( Prefixes Current: \s+)(\d+)(\s+)(\d+)/e", '"\\1".link_command("advertised-routes", "'.$lastip.'", "\\2")."\\3".link_command("routes", "'.$lastip.'", "\\4")', $output); $output = preg_replace("/(\s+)(Received)( prefixes:\s+[1-9]\d*)/e", '"\\1".link_command("routes", "'.$lastip.'", "\\2")."\\3"', $output); $output = preg_replace("/( Saved \(soft-reconfig\):\s+)(\d+|n\/a)(\s+)(\d+)/e", '"\\1\\2\\3".link_command("received-routes", "'.$lastip.'", "\\4")', $output); $output = preg_replace("/( [1-9]\d* )(accepted)( prefixes)/e", '"\\1".link_command("routes", "'.$lastip.'", "\\2")."\\3"', $output); $output = preg_replace("/^( [1-9]\d* )(accepted|denied but saved)( prefixes consume \d+ bytes)/e", '"\\1".link_command("received-routes", "'.$lastip.'", "\\2")."\\3"', $output); $output = preg_replace("/^(BGP neighbor is )(\d+\.\d+\.\d+\.\d+)(,)/e", '"\\1".link_whois("\\2")."\\3"', $output); $output = preg_replace("/^( Description: )(.*)$/", '\\1\\2', $output); $output = preg_replace("/(,\s+remote AS )(\d+)(,)/e", '"\\1".link_as("\\2")."\3"', $output); $output = preg_replace("/(, local AS )(\d+)(,)/e", '"\\1".link_as("\\2")."\3"', $output); $output = preg_replace("/( update prefix filter list is )(\S+)/e", '"\\1".link_command("bgp", "prefix-list+\\2", "\\2")', $output); $output = preg_replace("/(Route map for \S+ advertisements is\s+:?\*?)(\S+)/e", '"\\1".link_command("bgp", "route-map+\\2", "\\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("/(Prefix )(advertised)( [1-9]\d*)/e", '"\\1".link_command("advertised-routes", "'.$ip.'", "\\2")."\\3"', $output); $output = preg_replace("/( [1-9]\d* )(accepted)( prefixes)/e", '"\\1".link_command("routes", "'.$ip.'", "\\2")."\\3"', $output); $output = preg_replace("/^( Description: )(.*)$/", '\\1\\2', $output); $output = preg_replace("/(\s+remote AS )(\d+)(,)/e", '"\\1".link_as("\\2")."\3"', $output); $output = preg_replace("/(\s+local AS )(\d+)(,)/e", '"\\1".link_as("\\2")."\3"', $output); $output = preg_replace("/( update prefix filter list is )(\S+)/e", '"\\1".link_command("bgp", "prefix-list+\\2", "\\2")', $output); $output = preg_replace("/(Route map for \S+ advertisements is\s+:?\*?)(\S+)/e", '"\\1".link_command("bgp", "route-map+\\2", "\\2")', $output); return $output; } if (preg_match("/^show bgp n\w*\s+([\d\.A-Fa-f:]+)/i", $exec, $ip_exp)) { $ip = $ip_exp[1]; $output = preg_replace("/(\s+AS )(\d+)/e", '"\\1".link_as("\\2")', $output); $output = preg_replace("/(\s+AS: )(\d+)/e", '"\\1".link_as("\\2")', $output); $output = preg_replace("/^( Active prefixes:\s+)(\d+)/e", '"\\1".link_command("routes", "'.$ip.'", "\\2")', $output); $output = preg_replace("/^( Received prefixes:\s+)(\d+)/e", '"\\1".link_command("bgp", "neighbors+"'.$ip.'"+routes+all", "\\2")', $output); $output = preg_replace("/^( Suppressed due to damping:\s+)(\d+)/e", '"\\1".link_command("bgp", "neighbors+"'.$ip.'"+routes+damping+suppressed", "\\2")', $output); $output = preg_replace("/^( )(Export)(: )/e", '"\\1".link_command("advertised-routes", "'.$ip.'", "\\2")."\\3"', $output); $output = preg_replace("/( )(Import)(: )/e", '"\\1".link_command("bgp", "neighbors+"'.$ip.'"+routes+all", "\\2")."\\3"', $output); return $output; } // JunOS if (preg_match("/^show route protocol bgp .* terse/i", $exec)) { //$output = preg_replace("/^(.{20} B .{25} \>.{15}[^ ]*)( [\d\s,\{\}]+)(.*)$/e", '"\\1".link_command("bgp", "\\2+exact", "\\2")."\\3"', $output); $output = preg_replace("/^([\* ] )([\d\.A-Fa-f:\/]+)(\s+)/e", '"\\1".link_command("bgp", "\\2+exact", "\\2")."\\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("/( from )([0-9\.A-Fa-f:]+)/e", '"\\1".link_command("bgp", "neighbors+\\2", "\\2")', $output); $output = preg_replace("/( Source: )([0-9\.A-Fa-f:]+)/e", '"\\1".link_command("bgp", "neighbors+\\2", "\\2")', $output); $output = preg_replace("/(\s+AS: )([\d ]+)/e", 'link_as("\\2")', $output); $output = preg_replace("/(Community: )([\d: ]+)/e", '"\\1".link_community("\\2")', $output); $output = preg_replace("/(Communities: )([\d: ]+)/e", '"\\1".link_community("\\2")', $output); $output = preg_replace("/(^\s+AS path: )([\d ]+)/e", '"\\1".link_as("\\2")', $output); $output = preg_replace("/^(:?\n?[\dA-Fa-f:]+[\d\.A-Fa-f:\/]+)(\s*)/e", '"".link_command("bgp", "\\1+exact", "\\1")."\\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("/, \(aggregated by /", $output)) { $count++; $output = preg_replace("/^([^\(A-z\n]+)/e", 'link_as("\\1")', $output); $output = preg_replace("/(, \(aggregated by )(\d+) ([^\)]+)/e", '"\\1".link_as("\\2")." ".link_whois("\\3")', $output); } if (isset($best) AND $best == $count) { $output = ''.$output.''; } $output = preg_replace("/( from )([0-9\.A-Fa-f:]+)( )/e", '"\\1".link_command("bgp", "neighbors+\\2", "\\2")."\\3"', $output); $output = preg_replace("/(Community: )([\d: ]+)/e", '"\\1".link_community("\\2")', $output); $output = preg_replace("/(Communities: )([\d: ]+)/e", '"\\1".link_community("\\2")', $output); $output = preg_replace("/(^\s+AS path: )([\d ]+)/e", 'link_as("\\2")', $output); return $output; } if (preg_match("/^trace/", $exec)) { $output = preg_replace("/\[AS0\]\s(.*)/", "\\1", $output); $output = preg_replace("/(\[AS)(\d+)(\])\s(.*)(\))(.*)/e", '"\\4\\5 \\1 ".link_as("\\2")."\\3\\6"', $output); // IPv4 $output = preg_replace("/(\[AS)(\d+)(\])\s([^\s]+)\s(.*)/e", '"\\4 \\1 ".link_as("\\2")."\\3\\5"', $output); // IPv6 $output = preg_replace("/(\[AS\s+)(\d+)(\])/e", '"\\1".link_as("\\2")."\\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=\"([^\"]+)\"/ex", $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 /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("/, \(aggregated by /", $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; return preg_replace("/(?:AS)?([\d]+)/is", "".($word ? 'AS' : '')."\\1", $line); } /** * 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)) { $graph->addNode('AS'.$_CONFIG['asn'], array ( 'URL' => $_CONFIG['aswhois'].'AS'.$_CONFIG['asn'], 'target' => '_blank', 'label' => 'AS'.$_CONFIG['asn']."\n".$_CONFIG['company'], 'style' => 'filled', 'fillcolor' => 'white', 'fontsize' => $font_size )); $graph->addEdge(array('AS'.$_CONFIG['asn'] => $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