How would you run this command and display the output for all ethernet ports using the API?
Just print all interface names, and loop over, executing the command for each one.
The exact code would be different for each API client. With the one from my signature for example, it would be something like:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
$cableTestRequest = new RouterOS\Request('/interface ethernet cable-test .proplist=status,cable-pairs');
$interfaces = $client->sendSync(new RouterOS\Request('/interface ethernet print .proplist=name'))
->getAllOfType(RouterOS\Response::TYPE_DATA);
foreach ($interfaces as $interface) {
$interfaceName = $interface->getArgument('name');
$testResult = $client->sendSync($cableTestRequest->setArgument('numbers', $interfaceName));
echo "Name: {$interfaceName}\n";
foreach ($testResult->getAllArguments() as $name => $value) {
echo "{$name}: {$value}\n";
}
echo "\n";
}
EDIT: Waaait a minute… I forgot you’re using the other PHP client… just a sec…
<?php
//include and connect...
$interfaces = $API->comm('/interface/ethernet/print', array('.proplist' => 'name'));
$interfaces = array_slice($interfaces, 0, count($interfaces) - 1);
foreach ($interfaces as $interface) {
$interfaceName = $interface['name'];
$testResult = $API->comm(
'/interface/ethernet/cable-test',
array('.proplist' => 'status,cable-pairs', 'numbers' => $interfaceName)
);
echo "Name: {$interfaceName}\n";
foreach ($testResult[0] as $name => $value) {
echo "{$name}: {$value}\n";
}
echo "\n";
}
Cant seem to get the PEAR2\Net\RouterOS running how would this look using the PHP API?
Thank you for your help!
What errors are you getting?
Do you have PHP 5.3.0 or later? (that’s typically the case when that client doesn’t work, while the older one does)
If you’re not sure, run the following file:
<?php phpinfo(); ?>
and see what it says at the top.
I’ve corrected my post above.
that above code doesn’t seem to work just get a timeout error?
the code that works using ssh is /interface ethernet cable-test ether3
Could be a RouterOS bug, given that cable-test is a very new command.
Just to make certain… when you type from terminal:
/interface ethernet cable-test ?
you do see
< numbers > -
right?
Or is it
< interface > -
?
Even if it’s the latter, you should at least get an error message.
< numbers > - … yea saw that it is quite a new addition.
no error is given.
I think the problem is that the command never terminates if detecting an active connection.
For example on ssh the return value is something like:
numbers: 1
name: ether2-master-local
status: link-ok
– [Q quit|D dump|C-z pause]
so wouldn’t the “-- [Q quit|D dump|C-z pause]” have to be bypassed per interface?
Oh.
So this command actually not only tells you the current state, it keeps telling it to you, so that you’d know if it suddenly changes. Nice.
Well, if that’s the case, you’ll really want to try using PEAR2_Net_RouterOS instead of the old client. It has the additional feature of sending requests and later terminating them (which is what you need to do here). Doing the same with the old client is so tricky, that you’d essentially be rewriting PEAR2_Net_RouterOS.
How it would look there is:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
$cableTestRequest = new RouterOS\Request('/interface ethernet cable-test .proplist=status,cable-pairs');
$interfaces = $client->sendSync(new RouterOS\Request('/interface ethernet print .proplist=name'))
->getAllOfType(RouterOS\Response::TYPE_DATA);
foreach ($interfaces as $interface) {
$interfaceName = $interface->getArgument('name');
$client->sendAsync(
$cableTestRequest->setArgument('numbers', $interfaceName)->setTag($interfaceName),
function ($testResult) {
echo "Name: {$testResult->getTag()}\n";
foreach ($testResult->getAllArguments() as $name => $value) {
echo "{$name}: {$value}\n";
}
echo "\n";
return true;//Cancel the request after getting the first results
}
);
}
$client->completeRequest();
This will tell you the current status of all interfaces, and terminate right away. If you remove the cancel command AND run this PHP file from the command line, you could instead essentially create a background program that reacts whenever the cable on an interface becomes bad.
You said you had some errors when trying to run PEAR2_Net_RouterOS… what errors?
Partially got it working, getting a Transmitter/TcpClient.php on line 140?
Fatal error: Uncaught exception ‘PEAR2\Net\Transmitter\SocketException’ with message 'Failed while receiving initial length byte
Are you able to do anything else? In particular, does the part up to
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b3.phar';
$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');
$cableTestRequest = new RouterOS\Request('/interface ethernet cable-test .proplist=status,cable-pairs');
$interfaces = $client->sendSync(new RouterOS\Request('/interface ethernet print .proplist=name'))
->getAllOfType(RouterOS\Response::TYPE_DATA);
work without errors?
If it does, then it again seems like this command might not be implemented in the API at all - the exception you see usually happens when RouterOS doesn’t return anything within the socket’s timeout.
@ MikroTik Technical Support
Is the new “/interface ethernet cable-test” implemented with the API? Seems buggy…