added json output

This commit is contained in:
Dennis Eisold 2017-12-29 23:45:10 +01:00
parent 11b59c7111
commit d25f3c2750
1 changed files with 158 additions and 127 deletions

285
index.php
View File

@ -1,139 +1,170 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Simple Babelweb</title>
<html>
<head>
<meta charset="UTF-8" />
<title>Simple Babelweb</title>
<style>
table {
font-family: monospace, sans-serif;
border-collapse: collapse;
}
<style>
table {
font-family: monospace, sans-serif;
border-collapse: collapse;
}
td, th {
border: 1px solid #fddddd;
text-align: left;
padding: 1px;
padding-right: 10px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
td, th {
border: 1px solid #fddddd;
text-align: left;
padding: 1px;
padding-right: 10px;
}
<body>
<?php
$addr = "::1";
$port = "33123";
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h1>Simple Babelweb </h1>
<?php
$msg = "dump\r\n";
$sock = socket_create(AF_INET6,SOCK_STREAM,0) or die("Cannot create socket");
socket_connect($sock,$addr,$port) or die("Cannot connect to socket");
$read = socket_read($sock,1024);
$data = explode(PHP_EOL, $read);
$addr = "::1";
$port = "33123";
$msg = "dump\r\n";
# dump anfordern
socket_write($sock,$msg,strlen($msg));
$sock = socket_create(AF_INET6,SOCK_STREAM,0) or die("Cannot create socket");
socket_connect($sock,$addr,$port) or die("Cannot connect to socket");
$read = socket_read($sock,1024);
# Daten einlesen
$interface = array();
$neighbour = array();
$xroute = array();
$data = explode(PHP_EOL, $read);
while (1) {
$read = socket_read($sock, 1024, PHP_NORMAL_READ);
if (preg_match("/interface\b/", $read)) {
$interface[] = $read;
}
if (preg_match("/neighbour\b/", $read)) {
$neighbour[] = $read;
}
if (preg_match("/xroute\b/", $read)) {
$xroute[] = $read;
}
if (preg_match("/\broute\b/", $read)){
break 1;
}
}
$json = json_encode(array(
'interface' => $interface,
'neighbour' => $neighbour,
'xroute' => $xroute,
));
socket_close($sock);
echo "<table>";
for($i = 0; $i < count($data); ++$i) {
if ($data[$i] == "ok"){
break;
}
echo "<tr><td>$data[$i]</td></tr>";
}
echo "</table>";
foreach ($interface as $temp) {
$tempdata = explode(" ", $temp);
$output['interfaces'][] = array(
'interface' => $tempdata[2],
'up' => $tempdata[4],
'ipv6' => $tempdata[6],
'ipv4' => $tempdata[8],
);
}
# dump anfordern
socket_write($sock,$msg,strlen($msg));
foreach ($neighbour as $temp) {
$tempdata = explode(" ", $temp);
$output['neighbours'][] = array(
'neighbour' => $tempdata[2],
'address' => $tempdata[4],
'interface' => $tempdata[6],
'reach' => $tempdata[8],
'rxcost' => $tempdata[10],
'txcost' => $tempdata[12],
'rtt' => $tempdata[14],
'rttcost' => $tempdata[16],
'cost' => $tempdata[18],
);
}
# Daten einlesen
$interface = array();
$neighbour = array();
$xroute = array();
foreach ($xroute as $temp) {
$tempdata = explode(" ", $temp);
$output['xroutes'][] = array(
'prefix' => $tempdata[2],
'from' => $tempdata[4],
'metric' => $tempdata[6],
);
}
while (1) {
$read = socket_read($sock, 1024, PHP_NORMAL_READ);
if (preg_match("/interface\b/", $read)){
$interface[] = $read;
}
if (preg_match("/neighbour\b/", $read)){
$neighbour[] = $read;
}
if (preg_match("/xroute\b/", $read)){
$xroute[] = $read;
}
if (preg_match("/\broute\b/", $read)){
break 1;
}
}
if($_REQUEST['format'] == 'json') {
echo json_encode($output);
}
else {
# Ausgabe
echo "<h1>Simple Babelweb</h1>";
echo "<table>";
for($i = 0; $i < count($data); ++$i) {
if ($data[$i] == "ok") {
break;
}
echo "<tr><td>$data[$i]</td></tr>";
}
echo "</table>";
socket_close($sock);
# Ausgabe
echo "<H2>Interfaces</H2>";
echo '<table>
<tr>
<th>Interface</th>
<th>up</th>
<th>ipv6</th>
<th>ipv4</th>
</tr>';
for($n = 0; $n < count($interface); ++$n){
$data = explode(" ", $interface[$n]);
echo "<tr>";
for($i = 2; $i < count($data); $i+=2) {
echo "<td>$data[$i]</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<H2>Neighbours</H2>";
echo '<table>
<tr>
<th>neighbour</th>
<th>address</th>
<th>interface</th>
<th>reach</th>
<th>rxcost</th>
<th>txcost</th>
<th>rtt</th>
<th>rttcost</th>
<th>cost</th>
</tr>';
for($n = 0; $n < count($neighbour); ++$n){
$data = explode(" ", $neighbour[$n]);
echo "<tr>";
for($i = 2; $i < count($data); $i+=2) {
echo "<td>$data[$i]</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<H2>Redistributed routes</H2>";
echo '<table>
<tr>
<th>prefix</th>
<th>from</th>
<th>metric</th>
</tr>';
for($n = 0; $n < count($xroute); ++$n){
$data = explode(" ", $xroute[$n]);
echo "<tr>";
for($i = 4; $i < count($data); $i+=2) {
echo "<td>$data[$i]</td>";
}
echo "</tr>";
}
echo "</table>";
?>
</body></html>
echo "<H2>Interfaces</H2>";
echo '<table>
<tr>
<th>Interface</th>
<th>up</th>
<th>ipv6</th>
<th>ipv4</th>
</tr>';
foreach($output['interfaces'] as $interface) {
echo "<tr><td>".$interface['interface']."</td>".
"<td>".$interface['up']."</td>".
"<td>".$interface['ipv6']."</td>".
"<td>".$interface['ipv4']."</td></tr>";
}
echo "</table>";
echo "<H2>Neighbours</H2>";
echo '<table>
<tr>
<th>neighbour</th>
<th>address</th>
<th>interface</th>
<th>reach</th>
<th>rxcost</th>
<th>txcost</th>
<th>rtt</th>
<th>rttcost</th>
<th>cost</th>
</tr>';
foreach($output['neighbours'] as $neighbour) {
echo "<tr><td>".$neighbour['neighbour']."</td>".
"<td>".$neighbour['address']."</td>".
"<td>".$neighbour['interface']."</td>".
"<td>".$neighbour['reach']."</td>".
"<td>".$neighbour['rxcost']."</td>".
"<td>".$neighbour['txcost']."</td>".
"<td>".$neighbour['rtt']."</td>".
"<td>".$neighbour['rttcost']."</td>".
"<td>".$neighbour['cost']."</td></tr>";
}
echo "</table>";
echo "<H2>Redistributed routes</H2>";
echo '<table>
<tr>
<th>prefix</th>
<th>from</th>
<th>metric</th>
</tr>';
foreach($output['xroutes'] as $xroute) {
echo "<tr><td>".$xroute['prefix']."</td>".
"<td>".$xroute['from']."</td>".
"<td>".$xroute['metric']."</td></tr>";
}
echo "</table>";
}
?>
</body>
</html>