Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SSH PTY for NOKIA SROS (fix for #86) #107

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion auth/authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions auth/ssh.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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());
}
Expand Down
10 changes: 2 additions & 8 deletions auth/telnet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
45 changes: 39 additions & 6 deletions routers/nokia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -35,8 +66,6 @@ protected function build_bgp($parameter) {
$cmd->add('ipv4');
}

$cmd->add($parameter);

if ($this->config['bgp_detail']) {
$cmd->add('detail');
}
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down
23 changes: 22 additions & 1 deletion routers/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ abstract class Router {
protected $config;
protected $id;
protected $requester;
protected $vrf_cmd;
vitalisator marked this conversation as resolved.
Show resolved Hide resolved

public function __construct($global_config, $config, $id, $requester) {
$this->global_config = $global_config;
Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand Down