Community discussions

MikroTik App
 
penwelldlamini
just joined
Topic Author
Posts: 7
Joined: Thu Jan 22, 2015 4:08 pm

Interface Printout PHP API

Tue Jan 10, 2017 4:51 pm

How can I echo out all interface names onto a table through API using PHP can someone please share a script
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Interface Printout PHP API

Tue Jan 10, 2017 10:19 pm

Using the API client from my signature:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));

$interfaces = $util->setMenu('/interface')->getAll();
?>
<table>
<?php foreach ($interfaces as $interface) { ?>
<tr><td><?php echo $interface('name'); ?></td></tr>
<?php } ?>
</table>
 
penwelldlamini
just joined
Topic Author
Posts: 7
Joined: Thu Jan 22, 2015 4:08 pm

Re: Interface Printout PHP API

Wed Jan 11, 2017 8:52 am

Using the API client from my signature:
<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));

$interfaces = $util->setMenu('/interface')->getAll();
?>
<table>
<?php foreach ($interfaces as $interface) { ?>
<tr><td><?php echo $interface('name'); ?></td></tr>
<?php } ?>
</table>
Hi I am just looking for a simple PHP script that can work with my project. I find it hard to use you application
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Interface Printout PHP API

Thu Jan 12, 2017 6:00 am

I find it hard to use you application
What part(s) do you find difficult? Disregarding this and any other pre-existing library, what would you find to be the easiest way for a library to do things like (while still allowing all protocol features)? Could you give me some pseudo code?

The code above is the entire thing you need to have in your file, with the PHAR being in the same folder (and of course, you can adjust it accordingly in your real application).

If all the "detailed" tutorials in the wiki look like too much, there's also a reference with full description of each method, clean from all the "story" elements.
 
mysz0n
Member Candidate
Member Candidate
Posts: 137
Joined: Tue Mar 03, 2009 2:14 am

Re: Interface Printout PHP API

Thu Sep 21, 2017 6:04 pm

@boen_robot
can you please tell me what i"m doing wronkg with your API.
This is working fine:
$trequest2 = new RouterOS\Request('/interface monitor-traffic interface=wlan1 once');
$rx = $client->sendSync($trequest2)->getProperty('rx-bits-per-second');
$tx = $client->sendSync($trequest2)->getProperty("tx-bits-per-second");
echo $rx;
echo $tx;
but this code gives me nothing
$trequest2 = new RouterOS\Request('/interface ethernet monitor ether1 once');
$status = $client->sendSync($trequest2)->getProperty('status');
$rate = $client->sendSync($trequest2)->getProperty("rate");
$auto_neg = $client->sendSync($trequest2)->getProperty("auto-negotiation");
echo 'status: '. $status;
echo 'rate: '. $rate;
echo 'auto_neg: '. $auto_neg;
By the way, Is it possible to see the LINK DOWN value, using your API?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Interface Printout PHP API

Thu Sep 21, 2017 7:12 pm

Commands in the request constructor must have argument names. Unnamed arguments are not supported by the API protocol.

The "/interface ethernet monitor" in particular has the interface name in an unnamed argument called "numbers" (as many other commands btw). You can find that out for yourself by typing "?" after the command from a terminal.

On a separate note though, each time you call sendSync(), that's a new call to the router. It's far more efficient to just make the call once, and inspect the results afterwards.

e.g.
$trequest2 = new RouterOS\Request('/interface monitor-traffic interface=wlan1 once');
$trequest2result = $client->sendSync($trequest2);
$rx = $trequest2result->getProperty('rx-bits-per-second');
$tx = $trequest2result->getProperty("tx-bits-per-second");
echo $rx;
echo $tx;
or (also addressing your "numbers" issue):
$trequest2 = new RouterOS\Request('/interface ethernet monitor numbers=ether1 once');
$trequest2result = $client->sendSync($trequest2);
$status = $trequest2result->getProperty('status');
$rate = $trequest2result->getProperty("rate");
$auto_neg = $trequest2result->getProperty("auto-negotiation");
echo 'status: '. $status;
echo 'rate: '. $rate;
echo 'auto_neg: '. $auto_neg;
By the way, Is it possible to see the LINK DOWN value, using your API?
Yes. Analogously to how you'd get it from CLI, which is at the "/interface" menu. For an even closer analog than with the Client class, you can use the Util class instead, e.g.
$util = new RouterOS\Util($client);
$linkDowns = $util->setMenu('/interface')->get('ether1', 'link-downs');
echo 'Link downs: ' . $linkDowns;
which would be analogous to the CLI:
:local linkDowns [/interface get ether1 link-downs];
:put ("Link downs: " . $linkDowns);
 
mysz0n
Member Candidate
Member Candidate
Posts: 137
Joined: Tue Mar 03, 2009 2:14 am

Re: Interface Printout PHP API

Wed Dec 13, 2017 9:19 pm

hello boen_robot, I have a question about your API (which is awesome)
I'm using your API with successes on the IIS server.
At the moment I started a new server on XAMPP. Everything works properly except for connections via api-ssl (regular api works fine)
at the moment of connection
$client = new RouterOS\Client(192.168.88.22, 'admin',"password", null, false, null, NetworkStream::CRYPTO_TLS));
i got an error
exception 'PEAR2\Net\Transmitter\SocketException' with message 'stream_socket_client(): Could not get peer certificate' in phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/Transmitter/TcpClient.php:205 Stack trace: #0 phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/Transmitter/Stream.php(130): PEAR2\Net\Transmitter\TcpClient->createException('stream_socket_c...', 0) #1 [internal function]: PEAR2\Net\Transmitter\Stream->handleError(2, 'stream_socket_c...', 'phar://C:/xampp...', 147, Array) #2 phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/Transmitter/TcpClient.php(147): stream_socket_client('tls://192.168.88.22...', 0, '', '60', 4, Resource id #15) #3 phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Communicator.php(148): PEAR2\Net\Transmitter\TcpClient->__construct('192.168.88.22', 8729, false, NULL, 'admin/pass', 'TLS', Resource id #15) #4 phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Client.php(146): PEAR2\Net\RouterOS\Communicator->__construct('192.168.88.22', NULL, false, NULL, 'admin/pass', 'TLS', NULL) #5 C:\xampp\htdocs\beam\new\ssl.php(50): PEAR2\Net\RouterOS\Client->__construct('192.168.88.22', 'admin', 'pass', NULL, false, NULL, 'TLS') #6 {main} Next exception 'PEAR2\Net\Transmitter\SocketException' with message 'Failed to initialize socket.' in phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/Transmitter/TcpClient.php:205 Stack trace: #0 phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/Transmitter/TcpClient.php(157): PEAR2\Net\Transmitter\TcpClient->createException('Failed to initi...', 7, Object(PEAR2\Net\Transmitter\SocketException)) #1 phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Communicator.php(148): PEAR2\Net\Transmitter\TcpClient->__construct('192.168.88.22', 8729, false, NULL, 'admin/pass', 'TLS', Resource id #15) #2 phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Client.php(146): PEAR2\Net\RouterOS\Communicator->__construct('192.168.88.22', NULL, false, NULL, 'admin/pass', 'TLS', NULL) #3 C:\xampp\htdocs\beam\new\ssl.php(50): PEAR2\Net\RouterOS\Client->__construct('192.168.88.22', 'admin', 'pass', NULL, false, NULL, 'TLS') #4 {main} Next exception 'PEAR2\Net\RouterOS\SocketException' with message 'Error connecting to RouterOS' in phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Communicator.php:150 Stack trace: #0 phar://C:/xampp/htdocs/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Client.php(146): PEAR2\Net\RouterOS\Communicator->__construct('192.168.88.22', NULL, false, NULL, 'admin/pass', 'TLS', NULL) #1 C:\xampp\htdocs\beam\new\ssl.php(50): PEAR2\Net\RouterOS\Client->__construct('192.168.88.22', 'admin', 'pass', NULL, false, NULL, 'TLS') #2 {main}

Do you have an idea what's going on and what should I change on server or php.ini?
I have enabled the php_openssl.dll library.
Last edited by mysz0n on Thu Dec 14, 2017 11:19 am, edited 1 time in total.
 
User avatar
LEA
newbie
Posts: 29
Joined: Tue Dec 12, 2017 5:02 pm
Location: Russian Federation, Novosibirsk
Contact:

Re: Interface Printout PHP API

Thu Dec 14, 2017 5:33 am

hello boen_robot, I have a question about your API (which is awesome)
I'm using your API with successes on the IIS server.
At the moment I started a new server on XAMPP. Everything works properly except for connections via api-ssl (regular api works fine)
at the moment of connection
Hello, have you created a certificate for api-ssl with subsequent binding to IP services?
If Yes, then check the port on which should be the connection between a php script and the server.
By default, api-ssl uses port 8729, and a non-secure connection, the api uses port 8728

You also need to check the settings of the firewall on the Windows server where you installed XAMPP.
You have configured the firewall needs to be opened 8729 port.
Last edited by LEA on Thu Dec 14, 2017 3:21 pm, edited 1 time in total.
 
mysz0n
Member Candidate
Member Candidate
Posts: 137
Joined: Tue Mar 03, 2009 2:14 am

Re: Interface Printout PHP API

Thu Dec 14, 2017 11:18 am

Firewall on the server is turned off, what's interesting in the logs I see that the XAMPP server wants to connect to Router
10:02:25 firewall,info input: in:ether1 out:(none), src-mac 02:ab:cd:ef:6b:a9, proto TCP (SYN), 192.168.88.6:56682->192.168.88.22:8729, len 52
I can connect to the router from IIS server so I exclude the problem with its firewall, it seems to me that this is a problem on XAMPP server, or something has to be changed in php.ini or somewhere else.
Eventually I will send my requests from the XAMPP server to IIS so that IIS will connect via API-SSL and send back results to XAMPP server, but I would prefer to do all this on the XAMPP server
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Interface Printout PHP API

Thu Dec 14, 2017 2:39 pm

Strange... I thought I specifically made it so that ADH is the default if no context is provided... Then again, I haven't thoroughly tested SSL due to problems with PHP, so this may also be a version specific thing...

You can manually add a context with all desired options. For ADH (i.e. without a certificate), you need
$client = new RouterOS\Client('192.168.88.22', 'admin', 'password', null, false, null, NetworkStream::CRYPTO_TLS,
    strem_context_create(array('ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'ciphers' => 'ADH'
    )))
);
Regardless though, due to the aforementioned PHP issues, if you want an encrypted channel, it's better to setup OpenVPN between the client and router, and make an unencrypted connection over the VPN.
 
mysz0n
Member Candidate
Member Candidate
Posts: 137
Joined: Tue Mar 03, 2009 2:14 am

Re: Interface Printout PHP API

Thu Dec 14, 2017 3:38 pm

Thanks again boen_robot. before posting my question i tried:
$context = stream_context_create(
    array(
        'ssl' => array(
            'verify_peer' => true
          )
    )
);
without success, but this code works fine:
$context = stream_context_create(array('ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'ciphers' => 'ADH'
    ))
	);

since I have your attention I have one more question:
I connect the clients SXT via api-ssl, I want to change the pppoe account. The account changes, but the IP address changes as well, and the script starts to hang.
After reaching the timeout, the page is displayed, can I not wait for the answer, just send the new setting and disconnect?

The second option is to send a scheduler that will change pppoe account data, api will disconnect and after a few seconds the scheduler will change the account.
 
User avatar
LEA
newbie
Posts: 29
Joined: Tue Dec 12, 2017 5:02 pm
Location: Russian Federation, Novosibirsk
Contact:

Re: Interface Printout PHP API

Thu Dec 14, 2017 4:13 pm

Firewall on the server is turned off, what's interesting in the logs I see that the XAMPP server wants to connect to Router
10:02:25 firewall,info input: in:ether1 out:(none), src-mac 02:ab:cd:ef:6b:a9, proto TCP (SYN), 192.168.88.6:56682->192.168.88.22:8729, len 52
I can connect to the router from IIS server so I exclude the problem with its firewall, it seems to me that this is a problem on XAMPP server, or something has to be changed in php.ini or somewhere else.
Eventually I will send my requests from the XAMPP server to IIS so that IIS will connect via API-SSL and send back results to XAMPP server, but I would prefer to do all this on the XAMPP server
View log file XAMPP, after you run your php script. If there is a problem with PHP, Apache - you will see these errors.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Interface Printout PHP API

Thu Dec 14, 2017 8:09 pm

since I have your attention I have one more question:
I connect the clients SXT via api-ssl, I want to change the pppoe account. The account changes, but the IP address changes as well, and the script starts to hang.
After reaching the timeout, the page is displayed, can I not wait for the answer, just send the new setting and disconnect?

The second option is to send a scheduler that will change pppoe account data, api will disconnect and after a few seconds the scheduler will change the account.
You mean you're changing the very IP you are connecting to? If that's the case, then yes, the only viable option is adding a self-deleting scheduler that will do the change.
 
mysz0n
Member Candidate
Member Candidate
Posts: 137
Joined: Tue Mar 03, 2009 2:14 am

Re: Interface Printout PHP API

Sat Apr 21, 2018 12:08 am

boen_robot quick question, how to do this in your API:
/tool bandwidth-server set enabled=yes 
I tried different approaches but nothing works
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Interface Printout PHP API

Sat Apr 21, 2018 11:10 pm

<?php
use PEAR2\Net\RouterOS;
require_once 'PEAR2_Net_RouterOS-1.0.0b6.phar';

$util = new RouterOS\Util($client = new RouterOS\Client('192.168.88.1', 'admin', 'password'));
$util->setMenu('/tool bandwidth-server')->set(null, array('enabled' => 'true'));
For boolean fields, the API protocol uses the strings "true" and "false" instead of CLI's "yes" and "no".
 
mysz0n
Member Candidate
Member Candidate
Posts: 137
Joined: Tue Mar 03, 2009 2:14 am

Re: Interface Printout PHP API

Sun Apr 22, 2018 12:29 pm

boen_robot: Thank you very much for your support!

Who is online

Users browsing this forum: salytwo and 29 guests