Community discussions

MikroTik App
 
cdman
newbie
Topic Author
Posts: 29
Joined: Sun Jan 01, 2006 11:47 pm
Location: Bulgaria/Sofia

API script problem with changing values

Thu Jan 05, 2012 10:38 am

Hello guys , trying all night to do some coding with no luck. I am making IP/MAC control for users on Mikrotik, so fo every user i create a rule in mangle chain with their IP / MAC values:

chain=prerouting action=mark-packet new-packet-mark=active passthrough=yes src-address=192.168.5.2
src-mac-address=11:11:11:11:11:12

I make it with PHP using routeros_api.class.php - which I found in the wiki - my code is:

$API->write('/ip/firewall/mangle/add',false);
$API->write('=chain=prerouting',false);
$API->write('=src-address='.$user_ip,false);
$API->write('=src-mac-address='.$user_mac,false);
$API->write('=action=mark-packet',false);
$API->write('=new-packet-mark=active',false);
$API->write('=comment='.$user_name,false);
$API->write('=disabled=false');
$res2=$API->read(true);

This creates my rule - But - for some users I want to change the MAC address later - for example when they buy new PC

I get $row_id first with other function - And with this code I can change the MAC

$API->write('/ip/firewall/mangle/set',false);
$API->write('=.id='.$row_id,false);
$API->write('=src-mac-address='.$user_mac);
$res=$API->read(true);

So far so good :) now my problem is when I want to remove src-mac-address restriction for some users - I tried to do this:

$API->write('/ip/firewall/mangle/set',false);
$API->write('=.id='.$row_id,false);
$API->write('=src-mac-address=');
$res=$API->read(true);

It does not work - I also tried this row and it does not work either:
$API->write('=src-mac-address=""');

So how to edit this value src-mac-address - to go default - like it is gray in Winbox ?

Te explain this again with CLI - my row is:

chain=prerouting action=mark-packet new-packet-mark=active passthrough=yes src-address=192.168.5.2
src-mac-address=11:11:11:11:11:12

And I want to make it this:

chain=prerouting action=mark-packet new-packet-mark=active passthrough=yes src-address=192.168.5.2

Any Ideas ?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: API script problem with changing values

Thu Jan 05, 2012 7:38 pm

There's the "unset" command.

If you open it in terminal, you'll see it takes two arguments - "numbers" (the entry to unset a property from) and "value-name" (the property to unset), so:
$API->write('/ip/firewall/mangle/unset',false);
$API->write('=numbers='.$row_id,false);
$API->write('=value-name=src-mac-address'); 
$res=$API->read(true); 
 
cdman
newbie
Topic Author
Posts: 29
Joined: Sun Jan 01, 2006 11:47 pm
Location: Bulgaria/Sofia

Re: API script problem with changing values

Thu Jan 05, 2012 11:58 pm

A :) that look very nice , I will try it right now. Thanks.


EDIT: I just tested it - and It works perfect!

Who is online

Users browsing this forum: No registered users and 24 guests