summaryrefslogtreecommitdiff
path: root/router.php
diff options
context:
space:
mode:
authorGuillaume Mazoyer <respawneral@gmail.com>2014-05-29 14:28:48 +0200
committerGuillaume Mazoyer <respawneral@gmail.com>2014-05-29 14:28:48 +0200
commit1a3e5a5bb861f59cbcdede94a82f0984e97ea002 (patch)
tree519d53ff3981c161b18d00ef0ca3caf85387f5c1 /router.php
parentf8b05a1fed71b89b1cd7524e4f64b317af1ab7a4 (diff)
Apache 2.4 configuration to avoid to access the config.php file.
First import of the whole source code (quite monolithic for now). Update configuration example.
Diffstat (limited to 'router.php')
-rw-r--r--router.php139
1 files changed, 139 insertions, 0 deletions
diff --git a/router.php b/router.php
new file mode 100644
index 0000000..c1fb3c4
--- /dev/null
+++ b/router.php
@@ -0,0 +1,139 @@
+<?php
+
+require_once 'utils.php';
+
+class Router {
+ private $id;
+ private $host;
+ private $type;
+ private $auth;
+ private $connection;
+ private $requester;
+
+ public function __construct($id, $requester) {
+ include 'config.php';
+
+ $this->id = $id;
+ $this->host = $config['routers'][$id]['host'];
+ $this->type = $config['routers'][$id]['type'];
+ $this->auth = $config['routers'][$id]['auth'];
+ $this->requester = $requester;
+ }
+
+ private function log_command($command) {
+ include 'config.php';
+
+ file_put_contents($config['misc']['logs'], $command,
+ FILE_APPEND | LOCK_EX);
+ }
+
+ public function connect() {
+ include 'config.php';
+
+ switch ($this->auth) {
+ case 'ssh-password':
+ $this->connection = ssh2_connect($this->host, 22);
+ if (!$this->connection) {
+ throw new Exception('Cannot connect to router');
+ }
+
+ $user = $config['routers'][$this->id]['user'];
+ $pass = $config['routers'][$this->id]['pass'];
+
+ if (!ssh2_auth_password($this->connection, $user, $pass)) {
+ throw new Exception('Bad login/password.');
+ }
+
+ break;
+
+ default:
+ echo 'Unknown protocol for connection.';
+ }
+ }
+
+ public function execute_command($command) {
+ if ($this->connection == null) {
+ $this->connect();
+ }
+
+ if (!($stream = ssh2_exec($this->connection, $command))) {
+ throw new Exception('SSH command failed');
+ }
+
+ stream_set_blocking($stream, true);
+
+ $data = '';
+ while ($buf = fread($stream, 4096)) {
+ $data .= $buf;
+ }
+ fclose($stream);
+
+ return $data;
+ }
+
+ public function send_command($command, $parameters) {
+ switch ($command) {
+ case 'bgp':
+ if (($parameters != null) && (strlen($parameters) > 0)) {
+ $complete_command = 'show route '.$parameters.' | no-more';
+ } else {
+ return 'An IP address (and only one) is required as destination.';
+ }
+ break;
+
+ case 'as-path-regex':
+ if (($parameters != null) && (strlen($parameters) > 0)) {
+ $complete_command = 'show route aspath-regex '.$parameters.' | no-more';
+ } else {
+ return 'An AS-Path regex is required like ".*XXXX YYYY.*".';
+ }
+ break;
+
+ case 'as':
+ if (($parameters != null) && (strlen($parameters) > 0)) {
+ $complete_command = 'show route aspath-regex .*'.$parameters.'.* | no-more';
+ } else {
+ return 'An AS number is required like XXXX.';
+ }
+ break;
+
+ case 'ping':
+ if (($parameters != null) && (strlen($parameters) > 0)) {
+ $complete_command = 'ping count 10 '.$parameters.' rapid';
+ } else {
+ return 'An IP address (and only one) is required to ping a host.';
+ }
+ break;
+
+ case 'traceroute':
+ if (($parameters != null) && (strlen($parameters) > 0)) {
+ if (match_ipv4($parameters)) {
+ $complete_command = 'traceroute '.$parameters.' as-number-lookup';
+ } else {
+ $complete_command = 'traceroute '.$parameters;
+ }
+ } else {
+ return 'An IP address is required to traceroute a host.';
+ }
+ break;
+
+ default:
+ return 'Command not supported.';
+ }
+
+ $this->log_command('['.date("Y-m-d H:i:s").'] [client: '.
+ $this->requester.'] '.$this->host.'> '.$complete_command."\n");
+ return $this->execute_command($complete_command);
+ }
+
+ public function disconnect() {
+ $this->execute_command('exit');
+ $this->connection = null;
+ }
+
+ public function __destruct() {
+ $this->disconnect();
+ }
+}
+
+// End of router.php