Web Hosting/Website & Pear2RouterOS API PHP | add,edit,delete user

Hello guys,

I have a problem with my MikroTik configuration.

I would like to connect the Pear2RouterOS API PHP via website to access the local routerOS(IP: 192.168.0.15)

I just want to connect and create a user, edit, delete using a website platform to send or sync data to webfig/winbox control.

Here’s my sample code that I used:

<?php use PEAR2\Net\RouterOS; require_once 'PEAR2_Net_RouterOS-1.0.0b5.php'; try { $client = new RouterOS\Client('192.168.0.15', 'admin', ''); echo 'OK'; } catch (Exception $e) { die($e); } When I try to run this script from my localhost. The code is working properly. Then I got an error after I run it to my web hosting. Here's the error: exception 'PEAR2\Net\Transmitter\SocketException' with message 'stream_socket_client(): unable to connect to tcp://192.168.0.15:8728/admin%2F (Connection timed out)' in phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/Transmitter/TcpClient.php:205 Stack trace: #0 phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/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:///var/www...', 147, Array) #2 phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/Transmitter/TcpClient.php(147): stream_socket_client('tcp://192.168.0...', 110, 'Connection time...', '60', 4, Resource id #2) #3 phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Communicator.php(148): PEAR2\Net\Transmitter\TcpClient->__construct('192.168.0.15', 8728, false, NULL, 'admin/', '', NULL) #4 phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Client.php(146): PEAR2\Net\RouterOS\Communicator->__construct('192.168.0.15', 8728, false, NULL, 'admin/', '', NULL) #5 /var/www/html/routeros.php(6): PEAR2\Net\RouterOS\Client->__construct('192.168.0.15', 'admin', '') #6 {main} Next exception 'PEAR2\Net\Transmitter\SocketException' with message 'Failed to connect with socket.' in phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/Transmitter/TcpClient.php:205 Stack trace: #0 phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/Transmitter/TcpClient.php(163): PEAR2\Net\Transmitter\TcpClient->createException('Failed to conne...', 8, Object(PEAR2\Net\Transmitter\SocketException)) #1 phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Communicator.php(148): PEAR2\Net\Transmitter\TcpClient->__construct('192.168.0.15', 8728, false, NULL, 'admin/', '', NULL) #2 phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Client.php(146): PEAR2\Net\RouterOS\Communicator->__construct('192.168.0.15', 8728, false, NULL, 'admin/', '', NULL) #3 /var/www/html/routeros.php(6): PEAR2\Net\RouterOS\Client->__construct('192.168.0.15', 'admin', '') #4 {main} Next exception 'PEAR2\Net\RouterOS\SocketException' with message 'Error connecting to RouterOS' in phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Communicator.php:150 Stack trace: #0 phar:///var/www/html/PEAR2_Net_RouterOS-1.0.0b5.php/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Client.php(146): PEAR2\Net\RouterOS\Communicator->__construct('192.168.0.15', 8728, false, NULL, 'admin/', '', NULL) #1 /var/www/html/routeros.php(6): PEAR2\Net\RouterOS\Client->__construct('192.168.0.15', 'admin', '') #2 {main} Please I need help for this. Thank you, Sapphire

When I try to run this script from my localhost. The code is working
properly. Then I got an error after I run it my web hosting.

Do you keep this IP even on the web host? You can’t.

The IP 192.168.0.15 is a private IP, i.e. one visible only within your router’s network. You can’t reach the router from the internet with it. That’s just how the IP protocol works…

If you want to do this as a way to be independent of internet settings and public IP, you need to create a VPN server at your web host, and connect the router(s) to this VPN. Once that is in place, the web server will be able to reach the router through the IP address assigned by the VPN.

To set up a VPN, you’ll need to have a VPS or dedicated host (more specifically: one with sudo SSH access), as chances are you’ll need to “manually” install and configure the VPN server. Shared hosting won’t work for this.

Hi boen_robot,

Thanks for the reply. Actually, I have a VPS Hosting. Do you have some links or tutorial if how to setup a VPN server and my VPS hosting? Where did I setup VPN?

Thank you,

Sapphire

It’s not exactly trivial, but since you have such access…

Here’s one tutorial for Ubuntu. You might want to google it if your server OS is a different one. OpenVPN is one of the more modern protocols supported by both Linux and RouterOS, so it’s a good choice, though there are other VPN protocols that could do the same.

Hi boen_robot,

What are the difference between VPS Hosting and VPN? If I have VPS Hosting do I need VPN? Sorry I’m newbie.

Thanks,

Sapphire

VPS means “Virtual Private Server” - it’s a marketing term, used to describe a virtual machine (hence “Virtual”), to which you are granted full exclusive control (hence “Private”), as if it was a real full blown computer, with the intent of it being used to host server applications, typically web servers (hence “Server”), though not necessarily.

VPN means “Virtual Private Network” - it’s an umbrella technical term used to describe a networking approach (hence “Network”). Your “real” network sends and receives special packets that emulate a real network (hence “Virtual”), usually to a dedicated device accessible “for real”, from which those packets are forwarded to other participants in this virtual network. All participants need special settings and/or credentials to be in the network, and the network is non existent to anyone outside of it (hence “Private”).

OpenVPN is just one protocol for VPN. Like in most VPN protocols, you have an OpenVPN server, to which OpenVPN clients connect. Once the OpenVPN client is connected, the OpenVPN server gives it a private IP within the network (ala 192.168.0.15, but instead of it being accessible only in the router network, it’s accessible only in the VPN network). Other OpenVPN clients can access this one with that IP, by going through the OpenVPN server. As long as the OpenVPN server is accessible to all parties involved, they can communicate with one another regardless of their individual internet settings (f.e. NAT).

Hello Sapphire ,
I am currently working on a simler but more complex project. Here is what you can do.
Change your private ip to public ip.
If you have dynamic ip which changes often you have to use some other methods, like script, ddns, or vpn tunnle.
For my case its better to use VPN, because its add another security level.
You can use softether vpn for vpn protocol (I am using ipsec/L2TP). There are lots of tutorial on internet and you can use any of them .You can make it work with in 10 min.
I will personally tell you to use laravel framework for your php programming. Its simple and fun, and rapid.

Best of luck.