EvilFreelancer / routeros-api-php

Mikrotik RouterOS API PHP client for your applications
https://mikrotik.com/software
MIT License
397 stars 145 forks source link

Printing IP address #66

Closed genysta closed 2 years ago

genysta commented 3 years ago

Hello, im trying to get ip addresses from my router but when i run the query it returns nothing. On the router log it just logged in from api log, did i miss something? Here's my code, im trying the same code from the doc but it's the same

$response = $client->query('/ip/address/print')->read();

genysta commented 3 years ago

I got gateway error after waiting a few minutes

EvilFreelancer commented 3 years ago

Hello! Please read this for details about similar issues: https://github.com/EvilFreelancer/routeros-api-php#unable-to-establish-socket-session-operation-timed-out

And also need to check if IP of router is available and port is open from server where installed the library: https://en.wikipedia.org/wiki/Nmap

nmap ip_address -p api_port
genysta commented 3 years ago

it's available and open and i got log logged in from api. And i've tried adding count-only parameter and get a response

genysta commented 3 years ago

Currently im trying to print ip address from router with just 3 ip address and it works perfectly

EvilFreelancer commented 3 years ago

Hm, interesting, can you try to print response from command like this:

$response = $client->query('/ip/address/print')->read(false);
var_dump($response);

->read(false) will turn off parsing of response, in some cases in will contain response with details about error from router side.

Ah, by the way, which version of RouterOS you use?

genysta commented 3 years ago

I'll let you know later, i've tried it on 6.44.6 and 6.47.9

genysta commented 3 years ago

i've tried ->read(false) and it's still the same, return nothing. My RouterOS version is 6.47.9

EvilFreelancer commented 2 years ago

@genysta hello! Can you please provide sample of code which you use for connecting to RouterOS API?

genysta commented 2 years ago

@EvilFreelancer of course, here's my code

$client = new Client([
            'host' => '10.10.10.1',
            'user' => 'someuser',
            'pass' => 'somepass'
        ]);

        $response = $client->query('/ip/address/print')->read();

        var_dump($response);
EvilFreelancer commented 2 years ago

It looks like the most basic example, but in any way I tried different ways of working with the library, I think you need to enable advanced debugging mode and check the logs.

Need to add error_reporting(E_ALL); into to top of your script then re-run it again.

I guess reproduced your issue, for example, I have prepared everything for deployment on my local PC, then added these sources to Docker image without one of the required extensions, like ext-sockets. And also see nothing in response, but in logs i have also see error message about missed extension.

genysta commented 2 years ago

Im using Laravel command to test it and added error_reporting(E_ALL); on top of my script and it's still return nothing. It's possible if there's something i missunderstand because im new to Laravel and php.

EvilFreelancer commented 2 years ago

Check Laravel logs in storage folder from root of project. Log file will looks like laravel_2021-09-27.log or just laravel.log. After running command need to look at the errors which occurs on script execution stage, use tail -f laravel.log for logs obtaining reading process.

EvilFreelancer commented 2 years ago

Ah, plus need to check for extension, try to run php -m in response must be sockets line, if you don't see it then need to install.

genysta commented 2 years ago

on laravel.log there's nothing i see when i run my command, and for php -m there's sockets line

EvilFreelancer commented 2 years ago

Okay, then need to check simple solution, need to create a simple script in root of project, eg test.php with following content:

<?php
error_reporting(E_ALL);

require_once __DIR__ . '/vendor/autoload.php';

$client = new \RouterOS\Client([
    'host' => '10.10.10.1',
    'user' => 'someuser',
    'pass' => 'somepass'
]);

$response = $client->query('/ip/address/print')->read();

var_dump($response);

Then run it php -f test.php and check response.

If you will receive response from router then issue somewhere in your Laravel installation.

genysta commented 2 years ago

Tried test.php and it still give me nothing. And i've tried it on other project and still give me nothing.

EvilFreelancer commented 2 years ago

@genysta please write to me via email (it will visible in my profile), i'll send you instruction of how to communicate with me via Telegram.

genysta commented 2 years ago

@EvilFreelancer sent email to email address on your profile

genysta commented 2 years ago

So i've tried it on another machine and it works properly so i guess there's something wrong with my PHP

genysta commented 2 years ago

The required extension is enabled, is there any way to check what's missing from my php installation?

genysta commented 2 years ago

@EvilFreelancer After downgrading to php 7.4 i got Stream timed out on router with 10 ip addresses but it works on router with less than 10 ip addresses. Did i miss something?

genysta commented 2 years ago

The problem is on my php installation

fikurimax commented 2 years ago

Hi @genysta can u mention the problem on ur PHP installation? seems I got the same issue

genysta commented 1 year ago

Hi @genysta can u mention the problem on ur PHP installation? seems I got the same issue

Hi, sorry but i start with fresh installation without further investigation on my PHP installation. Sorry but i dont know the problem.