cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Go to solution
Highlighted

Mikrotik PHP API Problem

Hello,

 

I've developed a web based system with PHP API for Mikrotik Devices. 

The system runs normaly in a local based server and another cloud hosted server that I've tested.

When I try to use goDaddy's Linux cPanel server this system cannot connect to a remote device.

 

I need to open a websocket to stream data from device.

The api shows:

public 'socket' => boolean false
  public 'error_no' => int 111
  public 'error_str' => string 'Connection refused' (length=18)

I know this is a problem with a godaddy because the software is a copy of another that is running perfectly.

 

i.e. this another server: http://seb-ecologia.org.br/teste/

  public 'socket' => resource(4, Unknown)
  public 'error_no' => int 0
  public 'error_str' => string '' (length=0)

 

What should I do?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Manager
Community Manager
Solution

Re: Mikrotik PHP API Problem

@wfuzatto. That's correct. The majority of ports are blocked on our shared hosting plans. You should be able to use that port on a VPS or Dedicated Server plan though. Glad you were able to figure something out!

 

JesseW - GoDaddy | Community Manager | 24/7 support available at x.co/247support | Remember to choose a solution and give kudos.

View solution in original post

4 REPLIES 4
Highlighted
Community Manager
Community Manager

Re: Mikrotik PHP API Problem

Hi @wfuzatto. Thanks for being part of GoDaddy Community! 

 

It seems likely that the configuration of your local server is different from that of the cPanel account you're using. It's difficult to say exactly what part of the configuration is causing the error. Shared hosting plans have increased security and some features are not available. However, I was able to upload a test websocket script to a test cPanel account and found it was able to connect. My suggestion would be to provide the full script you're using. Someone else in the community might be able to offer suggestions on what's causing the problem if they can review your script. 

 

JesseW - GoDaddy | Community Manager | 24/7 support available at x.co/247support | Remember to choose a solution and give kudos.
Highlighted

Re: Mikrotik PHP API Problem

@JesseW, thanks for your reply. I'm trying to find out a solution..

So if anyone know how to solve this problem..

 

Here is some information about this API:

https://wiki.mikrotik.com/wiki/API_PHP_class

https://wiki.mikrotik.com/wiki/Manual:API

 

and here is the function that open the sockets:

 

 

public function connect($ip, $login, $password)
    {
        for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
            $this->connected = false;
            $PROTOCOL = ($this->ssl ? 'ssl://' : '' );
            $context = stream_context_create(array('ssl' => array('ciphers' => 'ADH:ALL', 'verify_peer' => false, 'verify_peer_name' => false)));
            $this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $PROTOCOL . $ip . ':' . $this->port . '...');
            $this->socket = @stream_socket_client($PROTOCOL . $ip.':'. $this->port, $this->error_no, $this->error_str, $this->timeout, STREAM_CLIENT_CONNECT,$context);
            if ($this->socket) {
                socket_set_timeout($this->socket, $this->timeout);
                $this->write('/login');
                $RESPONSE = $this->read(false);
                if (isset($RESPONSE[0]) && $RESPONSE[0] == '!done') {
                    $MATCHES = array();
                    if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES)) {
                        if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {
                            $this->write('/login', false);
                            $this->write('=name=' . $login, false);
                            $this->write('=response=00' . md5(chr(0) . $password . pack('H*', $MATCHES[0][1])));
                            $RESPONSE = $this->read(false);
                            if (isset($RESPONSE[0]) && $RESPONSE[0] == '!done') {
                                $this->connected = true;
                                break;
                            }
                        }
                    }
                }
                fclose($this->socket);
            }
            sleep($this->delay);
        }

        if ($this->connected) {
            $this->debug('Connected...');
        } else {
            $this->debug('Error...');
        }
        return $this->connected;
    }

You can view the full file ( routeros_api.class.php )  or here: 

 

https://github.com/BenMenking/routeros-api

 

Regards.

Highlighted

Re: Mikrotik PHP API Problem

Edit:

 

I've got some help in Mikrotik's Forum.


Now I'm able to connect using websocket on port 80.

So, GD's firewall is blocking private ports?

i.e. port 58728? Is it possible to have this port open?

 

Regards.

Highlighted
Community Manager
Community Manager
Solution

Re: Mikrotik PHP API Problem

@wfuzatto. That's correct. The majority of ports are blocked on our shared hosting plans. You should be able to use that port on a VPS or Dedicated Server plan though. Glad you were able to figure something out!

 

JesseW - GoDaddy | Community Manager | 24/7 support available at x.co/247support | Remember to choose a solution and give kudos.

View solution in original post