Skip to content
Permalink
Browse files

Closes #74: Fix command builder behaviour.

  • Loading branch information...
respawner committed Jun 7, 2019
1 parent e613ee0 commit 1735bf3e990ebebe78ec1a407bfbb47348285414
@@ -27,11 +27,6 @@ public function __construct($separator = ' ') {
$this->separator = $separator;
// Use an array instead of a string to help in element manipulations
$this->elements = array();
// Add elements given in constructor
for ($i = 1; $i < func_num_args(); $i++) {
$this->add(func_get_arg($i));
}
}
/**
@@ -52,8 +47,8 @@ private function breakdown_element($element) {
*
* @return CommandBuilder the instance used when calling this function.
*/
public function add() {
$this->elements = array_merge($this->elements, func_get_args());
public function add(...$elements) {
$this->elements = array_merge($this->elements, $elements);
return $this;
}
@@ -49,10 +49,6 @@ private function get_aspath($parameter) {
}
protected function build_bgp($parameter) {
if (!is_valid_ip_address($parameter)) {
throw new Exception('The parameter is not an IP address.');
}
$cmd = new CommandBuilder();
$cmd->add($this->get_bird_binary(match_ipv6($parameter, false)));
@@ -67,16 +63,10 @@ protected function build_bgp($parameter) {
}
protected function build_aspath_regexp($parameter) {
if (!match_aspath_regexp($parameter)) {
throw new Exception('The parameter is not an AS-Path regular expression.');
}
return $this->get_aspath($parameter);
}
protected function build_as($parameter) {
if (!match_as($parameter)) {
throw new Exception('The parameter is not an AS number.');
}
return $this->get_aspath($parameter);
}
}
@@ -25,11 +25,9 @@
class Cisco extends Router {
protected function build_bgp($parameter) {
if (!is_valid_ip_address($parameter)) {
throw new Exception('The parameter is not an IP address.');
}
$cmd = new CommandBuilder();
$cmd->add('show bgp');
$cmd = new CommandBuilder('show bgp');
if (match_ipv6($parameter, false)) {
$cmd->add('ipv6');
}
@@ -42,13 +40,10 @@ protected function build_bgp($parameter) {
}
protected function build_aspath_regexp($parameter) {
if (!match_aspath_regexp($parameter)) {
throw new Exception('The parameter is not an AS-Path regular expression.');
}
$parameter = quote($parameter);
$commands = array();
$cmd = new CommandBuilder('show bgp');
$cmd = new CommandBuilder();
$cmd->add('show bgp');
if (!$this->config['disable_ipv6']) {
$commands[] = (clone $cmd)->add('ipv6 unicast quote-regexp', $parameter);
@@ -59,10 +54,6 @@ protected function build_aspath_regexp($parameter) {
}
protected function build_as($parameter) {
if (!match_as($parameter)) {
throw new Exception('The parameter is not an AS number.');
}
$parameter = '^'.$parameter.'_';
return $this->build_aspath_regexp($parameter);
}
@@ -72,7 +63,9 @@ protected function build_ping($parameter) {
throw new Exception('The parameter is not an IP address or a hostname.');
}
$cmd = new CommandBuilder('ping', $parameter, 'repeat 10');
$cmd = new CommandBuilder();
$cmd->add('ping', $parameter, 'repeat 10');
if ($this->has_source_interface_id()) {
$cmd->add('source', $this->get_source_interface_id());
}
@@ -85,7 +78,9 @@ protected function build_traceroute($parameter) {
throw new Exception('The parameter is not an IP address or a hostname.');
}
$cmd = new CommandBuilder('traceroute');
$cmd = new CommandBuilder();
$cmd->add('traceroute');
if (match_ipv6($parameter) || match_ipv4($parameter) ||
!$this->has_source_interface_id()) {
$cmd->add($parameter);
@@ -29,7 +29,9 @@ protected function build_ping($parameter) {
throw new Exception('The parameter is not an IP address or a hostname.');
}
$cmd = new CommandBuilder('ping');
$cmd = new CommandBuilder();
$cmd->add('ping');
if (match_ipv6($parameter) || match_ipv4($parameter) ||
!$this->has_source_interface_id()) {
$cmd->add($parameter);
@@ -72,7 +74,9 @@ protected function build_traceroute($parameter) {
throw new Exception('The parameter is not an IP address or a hostname.');
}
$cmd = new CommandBuilder('traceroute');
$cmd = new CommandBuilder();
$cmd->add('traceroute');
if (match_ipv6($parameter) || match_ipv4($parameter) ||
!$this->has_source_interface_id()) {
$cmd->add($parameter);
@@ -27,11 +27,9 @@ final class ExtremeNetIron extends Router {
private static $wrapper = "skip-page-display\r\n";
protected function build_bgp($parameter) {
if (!is_valid_ip_address($parameter)) {
throw new Exception('The parameter is not an IP address.');
}
$cmd = new CommandBuilder();
$cmd->add(self::$wrapper.'show');
$cmd = new CommandBuilder(self::$wrapper.'show');
if (match_ipv6($parameter, false)) {
$cmd->add('ipv6');
}
@@ -50,12 +48,9 @@ protected function build_bgp($parameter) {
}
protected function build_aspath_regexp($parameter) {
if (!match_aspath_regexp($parameter)) {
throw new Exception('The parameter is not an AS-Path regular expression.');
}
$commands = array();
$cmd = new CommandBuilder(self::$wrapper.'show');
$cmd = new CommandBuilder();
$cmd->add(self::$wrapper.'show');
if (!$this->config['disable_ipv6']) {
$cmd6 = clone $cmd;
@@ -82,10 +77,6 @@ protected function build_aspath_regexp($parameter) {
}
protected function build_as($parameter) {
if (!match_as($parameter)) {
throw new Exception('The parameter is not an AS number.');
}
$parameter = '^'.$parameter.'_';
return $this->build_aspath_regexp($parameter);
}
@@ -95,7 +86,9 @@ protected function build_ping($parameter) {
throw new Exception('The parameter is not an IP address or a hostname.');
}
$cmd = new CommandBuilder('ping');
$cmd = new CommandBuilder();
$cmd->add('ping');
if (match_hostname($parameter)) {
$hostname = $parameter;
$parameter = hostname_to_ip_address($hostname, $this->config);
@@ -134,7 +127,9 @@ protected function build_traceroute($parameter) {
throw new Exception('The parameter is not an IP address or a hostname.');
}
$cmd = new CommandBuilder('traceroute');
$cmd = new CommandBuilder();
$cmd->add('traceroute');
if (match_hostname($parameter)) {
$hostname = $parameter;
$parameter = hostname_to_ip_address($hostname, $this->config);
@@ -25,13 +25,10 @@
final class FRR extends Quagga {
protected function build_aspath_regexp($parameter) {
if (match_aspath_regexp($parameter)) {
throw new Exception('The parameter is not an AS-Path regular expression.');
}
$commands = array();
$cmd = new CommandBuilder();
// vytsh commands need to be quoted
$cmd = new CommandBuilder(self::$wrapper, '"', 'show');
$cmd->add(self::$wrapper, '"', 'show');
if (!$this->config['disable_ipv6']) {
$commands[] = (clone $cmd)->add('bgp ipv6 regexp', $parameter, '"');
@@ -25,11 +25,9 @@
final class Juniper extends Router {
protected function build_bgp($parameter) {
if (!is_valid_ip_address($parameter)) {
throw new Exception('The parameter is not an IP address.');
}
$cmd = new CommandBuilder();
$cmd->add('show route', $parameter, 'protocol bgp table');
$cmd = new CommandBuilder('show route', $parameter, 'protocol bgp table');
if (match_ipv6($parameter, false)) {
$cmd->add('inet6.0');
}
@@ -44,14 +42,11 @@ protected function build_bgp($parameter) {
}
protected function build_aspath_regexp($parameter) {
if (!match_aspath_regexp($parameter)) {
throw new Exception('The parameter is not an AS-Path regular expression.');
}
$parameter = quote($parameter);
$commands = array();
$cmd = new CommandBuilder('show route aspath-regex', $parameter,
'protocol bgp table');
$cmd = new CommandBuilder();
$cmd->add('show route aspath-regex', $parameter, 'protocol bgp table');
if (!$this->config['disable_ipv6']) {
$cmd6 = clone $cmd;
$cmd6->add('inet6.0');
@@ -73,10 +68,6 @@ protected function build_aspath_regexp($parameter) {
}
protected function build_as($parameter) {
if (!match_as($parameter)) {
throw new Exception('The parameter is not an AS number.');
}
$parameter = '^'.$parameter.' .*';
return $this->build_aspath_regexp($parameter);
}
@@ -86,7 +77,9 @@ protected function build_ping($parameter) {
throw new Exception('The parameter is not an IP address or a hostname.');
}
$cmd = new CommandBuilder('ping count 10 rapid', $parameter);
$cmd = new CommandBuilder();
$cmd->add('ping count 10 rapid', $parameter);
if ($this->has_source_interface_id()) {
$cmd->add('interface', $this->get_source_interface_id());
}
@@ -99,7 +92,9 @@ protected function build_traceroute($parameter) {
throw new Exception('The parameter is not an IP address or a hostname.');
}
$cmd = new CommandBuilder('traceroute');
$cmd = new CommandBuilder();
$cmd->add('traceroute');
if (match_ipv4($parameter)) {
$cmd->add('as-number-lookup');
}
@@ -30,6 +30,7 @@ protected function build_bgp($parameter) {
}
$cmd = new CommandBuilder();
if (match_ipv6($parameter, false)) {
$cmd->add('ipv6');
} else if (match_ipv4($parameter, false)) {
@@ -47,21 +48,18 @@ protected function build_bgp($parameter) {
}
protected function build_aspath_regexp($parameter) {
if (!match_aspath_regexp($parameter)) {
throw new Exception('The parameter is not an AS-Path regular expression.');
}
$commands = array();
$cmd = new CommandBuilder();
if (!$this->config['disable_ipv6']) {
$cmd6 = new CommandBuilder('ipv6 route print');
$cmd6 = (clone $cmd)->add('ipv6 route print');
if ($this->config['bgp_detail']) {
$cmd6->add('detail');
}
$commands[] = $cmd6->add('where', 'bgp-as-path='.quote($parameter));
}
if (!$this->config['disable_ipv4']) {
$cmd4 = new CommandBuilder('ip route print');
$cmd4 = (clone $cmd)->add('ip route print');
if ($this->config['bgp_detail']) {
$cmd4->add('detail');
}
@@ -72,21 +70,18 @@ protected function build_aspath_regexp($parameter) {
}
protected function build_as($parameter) {
if (!match_as($parameter)) {
throw new Exception('The parameter is not an AS number.');
}
$commands = array();
$cmd = new CommandBuilder();
if (!$this->config['disable_ipv6']) {
$cmd6 = new CommandBuilder('ipv6 route print');
$cmd6 = (clone $cmd)->add('ipv6 route print');
if ($this->config['bgp_detail']) {
$cmd6->add('detail');
}
$commands[] = $cmd6->add('where', 'bgp-as-path~'.quote($parameter.'\$'));
}
if (!$this->config['disable_ipv4']) {
$cmd4 = new CommandBuilder('ip route print');
$cmd4 = (clone $cmd)->add('ip route print');
if ($this->config['bgp_detail']) {
$cmd4->add('detail');
}
@@ -101,7 +96,9 @@ protected function build_ping($parameter) {
throw new Exception('The parameter is not an IP address or a hostname.');
}
$cmd = new CommandBuilder('ping count=10');
$cmd = new CommandBuilder();
$cmd->add('ping count=10');
if (match_hostname($parameter)) {
$cmd->add('address=[:resolv '.quote($parameter).']');
} else {
@@ -119,7 +116,9 @@ protected function build_traceroute($parameter) {
throw new Exception('The parameter is not an IP address or a hostname.');
}
$cmd = new CommandBuilder('tool traceroute count=1 use-dns=yes');
$cmd = new CommandBuilder();
$cmd->add('tool traceroute count=1 use-dns=yes');
if (match_hostname($parameter)) {
$cmd->add('address=[:resolv '.quote($parameter).']');
} else {

0 comments on commit 1735bf3

Please sign in to comment.
You can’t perform that action at this time.