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