Tags

, ,

In JunOS you can export any output in XML. For example you can try:

show route protocol bgp | display xml

This will show you all the BGP routing table but in XML format. Which will look like:

<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.3R4/junos">
<route-information xmlns="http://xml.juniper.net/junos/12.3R4/junos-routing">
<!-- keepalive -->
<route-table>
<table-name>inet.0</table-name>
<destination-count>547112</destination-count>
<total-route-count>547137</total-route-count>
<active-route-count>547112</active-route-count>
<holddown-route-count>0</holddown-route-count>
<hidden-route-count>1</hidden-route-count>
<rt junos:style="brief">
<rt-destination>0.0.0.0/0</rt-destination>
<rt-entry>
<active-tag> </active-tag>
<protocol-name>BGP</protocol-name>
<preference>170</preference>
<age junos:seconds="106012">1d 05:26:52</age>
<local-preference>100</local-preference>
<as-path>10102 9498 I
</as-path>
<validation-state>unknown</validation-state>
<nh>
<selected-next-hop/>
<to>103.4.109.169</to>
<via>ge-1/0/0.0</via>
</nh>
</rt-entry>
</rt>

Copy this file to your webserver:

file copy bgp-data.xml ftp://fakrul@103.21.75.10/bgp-data.xml

My target was to get the RPKI validation status; to do that I write the following code; which will show the validation status of each route.

<html>
<head>
<title>RPKI ROA OUTPUT</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>

<?php
// Loading the XML file
$xml = new SimpleXMLElement("bgp-data-small.xml", 0, true);
?>

<div class="container">
<div class="page-header">
<h3><a href="testcode1.php">bdNOG</a> <small>Bangladesh Network Operators Group</small></h3>
</div>

<table class="table table-hover">
<thead>
<tr>
<th>Route</th>
<th>AS Path</th>
<th>Protocol</th>
<th>Validation</th>
</tr>
</thead>

<?php

$startPage = $_GET['page'];
$perPage = 500;
$currentRecord = 0;
foreach($xml->{'route-information'}->{'route-table'}->{'rt'} as $record){
$currentRecord += 1;
if($currentRecord > ($startPage * $perPage) && $currentRecord < ($startPage * $perPage + $perPage)){

$route = str_replace("\n", "", $record->{'rt-destination'});
$aspath = str_replace("\n", "", $record->{'rt-entry'}->{'as-path'});
$protocolname = str_replace("\n", "", $record->{'rt-entry'}->{'protocol-name'});
$validation = str_replace("\n", "", $record->{'rt-entry'}->{'validation-state'});
echo "<tbody>";
echo "<tr>";
echo "<td class=\"small\">{$route}</td>";
echo "<td class=\"small\">{$aspath}</td>";
echo "<td class=\"small\">{$protocolname}</td>";
if ($validation=="invalid")
{echo "<td class=\"small\"><span class=\"label label-danger\">$validation</span></td>";}
elseif ($validation=="valid")
{echo "<td class=\"small\"><span class=\"label label-success\">$validation</span></td>";}
else
{echo "<td class=\"small\"><span class=\"label label-default\">$validation</span></td>";}
echo "</tr>";
echo "</tbody>";
}//end of if loop
}//endo of foreach loop

//pagination
for ($i = 1; $i <= ($currentRecord / $perPage); $i++) {
echo("<a href='testcode1.php?page=".$i."'>\t".$i."</a>");
}
?>
</table>
</div>
</body>
</html>

The output will look like :