[ask] Dhcp Server Setup via php API

I’m trying and learn to create php API …
im add setting menu interfaces, ip address , dns , gateaway , userman , NAT , n maybe other menu …
but i’m having trouble at setting DHCP Server(include pool and dhcp network) …
anyone have idea or php script about dhcp-server ??

thanks in advance
n sorry my bad english :slight_smile:

What exactly are you having problems with? What have you tried?

It should be almost as easy as in terminal. In other words, if you need to add a pool and network, you need to add them separately, but that’s about the only “complication”.

Here’s for example adding the whole thing:

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

$client = new RouterOS\Client('192.168.0.1', 'admin', 'password');

$client(new RouterOS\Request(
    '/ip pool add name=pool1 ranges=192.168.0.100-192.168.0.254'
));
$client(new RouterOS\Request(
    '/ip dchp-server network add address=192.168.0.0/24 gateway=192.168.0.1 dns-server=192.168.0.1'
));
$client(new RouterOS\Request(
    '/ip dhcp-server add name=server1 interface=ether1 address-pool=pool1'
)); 

i try n learn to create web controller with input method sir … n this my script for submit button … is it possible to execute 3 API command in one submit button ??

<?php
{
require "routeros_api.class.php";

$API = new routeros_api();

$API->debug = false;

if ($API->connect('192.168.88.1', 'admin', 'admin'))

{if ($_POST ['apply'])

$API->comm ("/ip/pool/add", array(
	"name"	=> "pool-$_POST[name]",
	"ranges" => "$_POST[range]",
	"interface" => "$_POST[ether]"));
$API->comm ("/ip/dhcp-server/network/add",array(
	"address" => "$_POST[address]",
	"gateaway" => "$_POST[gateaway]",
	"netmask" => "255.255.255.0"));
$API->comm ("/ip/dhcp-server/add", array (
	"name" => "$_POST[name]",
	"interface" => "$_POST[iface]",
	"address-pool" => "pool-$_POST[name]",);
};
$API->disconnect();
}	
?>

Of course it is.

In this case, the problem you’re having is a PHP syntax error. You need to surround your inner “if” with brackets. And there’s also a missing “)” in the last comm() call.

Also… I don’t think there’s an “interface” argument in the pool creation.

Although not required, I’d also recommend you fix your quoting and indenting style…

So something like:

<?php
require "routeros_api.class.php";

$API = new routeros_api();

$API->debug = false;

if ($API->connect('192.168.88.1', 'admin', 'admin')) {
    if ($_POST['apply']) {
        $API->comm ("/ip/pool/add", array(
            "name"   => "pool-{$_POST['name']}",
            "ranges" => $_POST['range']
        ));
        $API->comm("/ip/dhcp-server/network/add", array(
            "address" => $_POST['address'],
            "gateaway" => $_POST['gateaway'],
            "netmask" => "255.255.255.0"
        ));
        $API->comm("/ip/dhcp-server/add", array(
            "name" => $_POST['name'],
            "interface" => $_POST['iface'],
            "address-pool" => "pool-{$_POST['name']}"
        ));
    }
    $API->disconnect();
}
?>

sorry too late for reply sir .. my ISP have problem …
thanks u very much sir .. for correct my mistake .. u r very2 help me :slight_smile:
My problem is fixed now .. my web setting 90% finished .. wanna made portable so can be taken anywhere :slight_smile: ..

There are many ways to make the system more portable, but the easiest is to abstract away the settings.

Create a new PHP file, and in it, create constants and/or variables that contain the settings, such as the MikroTik hostname, username and password, and anything else your app may be using (database credentials, etc.). Include said file every time you use one of its settings.

When a different system uses it, it can just alter the file to contain the new system’s settings.

yep sir… for me the hardest part is sync user & pass on login .. Therefore, in every part of the PHP , im write manual login … I actually recently learned PHP (1 month) and I only understand in mikrotik system… because this is the final project of my college .. then inevitably I had to try it ..

very2 hard for me .. learn programming language in a short time and in a hurry .. because the field that I take is network engineering not programming

Well what do ya’ know :smiley: . My API client (the one in my very first post that is) also ended up as my final (university, but still) project… though my degree is in programming, but yeah…

Fine. Here you go…

Save the following file as “config.php”:

<?php
define('ROS_HOSTNAME', '192.168.88.1');
define('ROS_USERNAME', 'admin');
define('ROS_PASSWORD', 'admin'); 

Then turn your existing file into something like:

<?php
require "config.php";
require "routeros_api.class.php";

$API = new routeros_api();

$API->debug = false;

if ($API->connect(ROS_HOSTNAME, ROS_USERNAME, ROS_PASSWORD)) {
    if ($_POST['apply']) {
        $API->comm ("/ip/pool/add", array(
            "name"   => "pool-{$_POST['name']}",
            "ranges" => $_POST['range']
        ));
        $API->comm("/ip/dhcp-server/network/add", array(
            "address" => $_POST['address'],
            "gateaway" => $_POST['gateaway'],
            "netmask" => "255.255.255.0"
        ));
        $API->comm("/ip/dhcp-server/add", array(
            "name" => $_POST['name'],
            "interface" => $_POST['iface'],
            "address-pool" => "pool-{$_POST['name']}"
        ));
    }
    $API->disconnect();
}
?>

This is what I was previously talking about.