diff --git a/auth/authentication.php b/auth/authentication.php index ef226c5d..1ed6f967 100644 --- a/auth/authentication.php +++ b/auth/authentication.php @@ -80,7 +80,7 @@ public abstract function disconnect(); * * @param string $command the command to be sent to the host. */ - public abstract function send_command($command); + public abstract function send_command($command, $router); /** * Method called when the object is destructed. diff --git a/auth/ssh.php b/auth/ssh.php index ea4f5ebc..5bdddbe0 100644 --- a/auth/ssh.php +++ b/auth/ssh.php @@ -40,7 +40,7 @@ public function __construct($config, $debug) { $this->port = isset($this->config['port']) ? (int) $this->config['port'] : 22; if ($this->debug) { - define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX); + define('NET_SSH2_LOGGING', SSH2::LOG_COMPLEX); } } @@ -91,10 +91,11 @@ public function connect() { } } - public function send_command($command) { + public function send_command($command, $router) { $this->connect(); - $data = $this->connection->exec($command); + $data = $router->send_ssh_command($command, $this->connection); + if ($this->debug) { log_to_file($this->connection->getLog()); } diff --git a/auth/telnet.php b/auth/telnet.php index 29ac6e48..20117d7f 100644 --- a/auth/telnet.php +++ b/auth/telnet.php @@ -48,16 +48,10 @@ public function connect() { fputs($this->connection, $this->config['pass']."\r\n"); } - public function send_command($command) { + public function send_command($command, $router) { $this->connect(); - fputs($this->connection, $command."\r\n"); - - $data = ''; - while (substr($data, -1) != '#' && substr($data, -1) != '>') { - $data .= fread($this->connection, 4096); - $data = rtrim($data, " "); - } + $data = $router->send_telnet_command($command, $this->connection); $this->disconnect(); diff --git a/routers/nokia.php b/routers/nokia.php index aa9fad14..b29b4eb0 100644 --- a/routers/nokia.php +++ b/routers/nokia.php @@ -19,14 +19,45 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +use phpseclib\Net\SSH2; + require_once('router.php'); require_once('includes/command_builder.php'); require_once('includes/utils.php'); final class Nokia extends Router { + public function __construct($global_config, $config, $id, $requester) { + parent::__construct($global_config, $config, $id, $requester); + if (isset($this->config['vrf'])) { + $this->vrf_cmd = 'router ' . $this->config['vrf']; + } + } + + // overwrite Router::send_ssh_command with vendor specific + public function send_ssh_command($command, $connection) { + $connection->enablePTY(); + // read and ignore the first response prompt to get clear output if the router is sending MOTD + $connection->read('/.*:.*# /', SSH2::READ_REGEX); + + // disable paging + $connection->write("environment no more\n"); + $connection->read('/.*:.*# /', SSH2::READ_REGEX); + + $connection->write($command . "\n"); + $data = $connection->read('/.*:.*# /', SSH2::READ_REGEX); + + return $data; + } + protected function build_bgp($parameter) { $cmd = new CommandBuilder(); - $cmd->add('show router bgp routes'); + if (isset($this->vrf_cmd)) { + $cmd->add('show', $this->vrf_cmd, 'bgp routes'); + } else { + $cmd->add('show router bgp routes'); + } + + $cmd->add($parameter); if (match_ipv6($parameter, false)) { $cmd->add('ipv6'); @@ -35,8 +66,6 @@ protected function build_bgp($parameter) { $cmd->add('ipv4'); } - $cmd->add($parameter); - if ($this->config['bgp_detail']) { $cmd->add('detail'); } @@ -48,7 +77,11 @@ protected function build_aspath_regexp($parameter) { $parameter = quote($parameter); $commands = array(); $cmd = new CommandBuilder(); - $cmd->add('show router bgp routes'); + if (isset($this->vrf_cmd)) { + $cmd->add('show', $this->vrf_cmd, 'bgp routes'); + } else { + $cmd->add('show router bgp routes'); + } if (!$this->config['disable_ipv6']) { $cmd6 = clone $cmd; @@ -83,7 +116,7 @@ protected function build_ping($parameter) { } $cmd = new CommandBuilder(); - $cmd->add('ping count 10', $parameter); + $cmd->add('ping count 10', $parameter, $this->vrf_cmd); if ($this->has_source_interface_id()) { $cmd->add('source', $this->get_source_interface_id()); @@ -98,7 +131,7 @@ protected function build_traceroute($parameter) { } $cmd = new CommandBuilder(); - $cmd->add('traceroute', $parameter); + $cmd->add('traceroute no-dns', $parameter, $this->vrf_cmd); if ($this->has_source_interface_id()) { $cmd->add('source', $this->get_source_interface_id()); diff --git a/routers/router.php b/routers/router.php index f6a02730..ebbd1952 100644 --- a/routers/router.php +++ b/routers/router.php @@ -41,6 +41,7 @@ abstract class Router { protected $config; protected $id; protected $requester; + protected $vrf_cmd; public function __construct($global_config, $config, $id, $requester) { $this->global_config = $global_config; @@ -205,7 +206,7 @@ public function send_command($command, $parameter) { '[BEGIN] '.$selected), $this->global_config['logs']['format']); log_to_file($log); - $output = $auth->send_command((string) $selected); + $output = $auth->send_command((string) $selected, $this); $output = $this->sanitize_output($output); $data .= $this->format_output($selected, $output); @@ -219,6 +220,26 @@ public function send_command($command, $parameter) { return $data; } + public function send_ssh_command($command, $connection) { + + $data = $connection->exec($command); + + return $data; + } + + public function send_telnet_command($command, $connection) { + + fputs($connection, $command."\r\n"); + + $data = ''; + while (substr($data, -1) != '#' && substr($data, -1) != '>') { + $data .= fread($connection, 4096); + $data = rtrim($data, " "); + } + + return $data; + } + public static final function instance($id, $requester) { global $config;