Browse Source

To Client class was added ->export() method, it use divineomega/php-ssh-connection library for interact with remote hosts, example file updated

pull/40/head
Paul Rock 6 years ago
parent
commit
be80ff9bcf
  1. 9
      examples/export.php
  2. 28
      src/Client.php
  3. 31
      src/Interfaces/ClientInterface.php

9
examples/export.php

@ -12,14 +12,13 @@ $config =
(new Config())
->set('host', '127.0.0.1')
->set('pass', 'admin')
->set('user', 'admin');
->set('user', 'admin')
->set('ssh_port', 22222);
// Initiate client with config object
$client = new Client($config);
// Build query
$query = new Query('/export');
// Execute export command via ssh
$response = $client->export();
// Send query and read answer from RouterOS
$response = $client->write($query)->read(false);
print_r($response);

28
src/Client.php

@ -2,6 +2,7 @@
namespace RouterOS;
use DivineOmega\SSHConnection\SSHConnection;
use RouterOS\Exceptions\ClientException;
use RouterOS\Exceptions\ConfigException;
use RouterOS\Exceptions\QueryException;
@ -517,4 +518,31 @@ class Client implements Interfaces\ClientInterface
// Return status of connection
return $connected;
}
/**
* Execute export command on remote host
*
* @return string
* @throws \RouterOS\Exceptions\ConfigException
* @throws \RuntimeException
*
* @since 1.3.0
*/
public function export(): string
{
// Connect to remote host
$connection =
(new SSHConnection())
->to($this->config('host'))
->onPort($this->config('ssh_port'))
->as($this->config('user') . '+etc')
->withPassword($this->config('pass'))
->connect();
// Run export command
$command = $connection->run('/export');
// Return the output
return $command->getOutput();
}
}

31
src/Interfaces/ClientInterface.php

@ -49,6 +49,11 @@ interface ClientInterface
public const ATTEMPTS_DELAY = 1;
/**
* Delay between attempts in seconds
*/
public const SSH_PORT = 22;
/**
* Return socket resource if is exist
*
* @return resource
@ -58,7 +63,14 @@ interface ClientInterface
/**
* Read answer from server after query was executed
*
* A Mikrotik reply is formed of blocks
* Each block starts with a word, one of ('!re', '!trap', '!done', '!fatal')
* Each block end with an zero byte (empty line)
* Reply ends with a complete !done or !fatal block (ended with 'empty line')
* A !fatal block precedes TCP connexion close
*
* @param bool $parse
*
* @return mixed
*/
public function read(bool $parse);
@ -67,7 +79,10 @@ interface ClientInterface
* Send write query to RouterOS
*
* @param string|array|\RouterOS\Query $query
*
* @return \RouterOS\Client
* @throws \RouterOS\Exceptions\QueryException
* @deprecated
*/
public function write($query): Client;
@ -78,9 +93,21 @@ interface ClientInterface
* @param array|null $where List of where filters
* @param string|null $operations Some operations which need make on response
* @param string|null $tag Mark query with tag
* @return \RouterOS\Client
*
* @return \RouterOS\Interfaces\ClientInterface
* @throws \RouterOS\Exceptions\QueryException
* @since 1.0.0
*/
public function query($endpoint, array $where, string $operations, string $tag): Client;
public function query($endpoint, array $where, string $operations, string $tag): ClientInterface;
/**
* Execute export command on remote host
*
* @return string
* @throws \RouterOS\Exceptions\ConfigException
* @throws \RuntimeException
*
* @since 1.3.0
*/
public function export(): string;
}
Loading…
Cancel
Save