Community discussions

MikroTik App
 
sirkike
just joined
Topic Author
Posts: 20
Joined: Mon Mar 22, 2010 10:34 pm
Location: Peru

synchronize two address-list certain time

Wed Oct 26, 2011 9:35 pm

I need that, sync two address-list every so often, some form? No idea how I ... Obviously they are two separate mikritok two different X86 ...
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: synchronize two address-list certain time

Thu Oct 27, 2011 9:59 am

you could give some details - like both lists are updated independently, and then has to be synced, or only one of them is updated, and other should contain the entries. So, how is it?
 
dimedrol
just joined
Posts: 7
Joined: Tue Nov 16, 2010 4:24 pm

Re: synchronize two address-list certain time

Tue Dec 13, 2011 9:47 am

Hi!

There is a similar task.
All changes happen on master ROS, on slave lists only are copied.
Prompt how probably to make?
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: synchronize two address-list certain time

Tue Dec 13, 2011 3:23 pm

you will have to make some external tool to do that. You can look at API or scripting. My choice would be to make API and database to synchronize and push only changes.
 
jurgenskrause
newbie
Posts: 30
Joined: Thu Feb 27, 2014 1:30 pm

Re: synchronize two address-list certain time

Thu Apr 02, 2015 1:00 pm

I know this is a late reply, but I had the same problem. The address lists that I need to sync are fairly small so I managed to do it by scripting on both routers.

Script available from:http://binaryheartbeat.blogspot.com/201 ... -list.html
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: synchronize two address-list certain time

Thu Apr 02, 2015 1:49 pm

If you use an API application, you don't even need to schedule a sync. You can keep the API connection on, and sync as soon as an item is added or removed to the address list.

e.g. with PHP (run from command line, not a web page!)
<?php
use PEAR2\Net\RouterOS;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

$master = new RouterOS\Client('master-router-ip', 'admin', 'password');
$slaveUtil = new RouterOS\Util($slave = new RouterOS\Client('slave-router-ip', 'admin', 'password'));
$slaveUtil->setMenu('/ip firewall address-list');

$addressLists = array('List1', 'List2');

$master->sendAsync(
    new RouterOS\Request('/ip firewall address-list listen'),
    function ($response) use ($addressLists, $slave, $slaveUtil) {
        if (in_array($response('list'), $addressLists, true)) {
            if (null !== $response('.dead')) {
                //Removed
                $slaveUtil->remove(
                    $slaveUtil->find(
                        RouterOS\Query::where('list', $response('list'))->andWhere('address', $response('address'))
                    )
                );
            } else {
                //Added
                $slaveUtil->add(
                    array(
                        'list' => $response('list'),
                        'address' => $response('address')
                    )
                );
            }
        }
    }
);
$master->loop(); 
Or if you insist on not using a 3rd device, you can use something similar to jurgenskrause's suggestion... Except that his can be implemented in a more simple and efficient fashion...

On the master, schedule:
#Semicolon separated list of Address Lists you want to sync
:local lists {"List1";"List2"}

#Filename of export (must match receiving router import filename)
:local exportFile "ExportAddressList.rsc"

:local fileContents "/ip firewall address-list\n"
:foreach i in=$lists do={
    :set $fileContents ($fileContents . "remove [find list=\"" . $i . "\"]\n")
    :foreach j in=[/ip firewall address-list print as-value where list=$i] do={
        :set $fileContents ($fileContents . "add list=\"" . ($j->"list") . "\" address=\"" . ($j->"address") . "\"\n")
    }
}

#Only the first time
:if ([/file print count-only where name=$exportFile] = 0) {
    /file print file=$exportFile
    :delay 2s
}

/file set $exportFile contents=$fileContents

On slave, schedule:
#Filename of export (must match receiving router import filename)
 :local importFile "ExportAddressList.rsc"

 #IP Address of router with existing address list(s)
 :local hostIP 1.2.3.4

 #FTP Username and Password
 :local ftpUser username
 :local ftpPassword userpassword

 /tool fetch address=$hostIP src-path=$importFile dst-path=$importFile mode=ftp user=$ftpUser password=$ftpPassword
 /import $importFile
Last edited by boen_robot on Thu Apr 02, 2015 3:45 pm, edited 2 times in total.
 
jurgenskrause
newbie
Posts: 30
Joined: Thu Feb 27, 2014 1:30 pm

Re: synchronize two address-list certain time

Thu Apr 02, 2015 2:56 pm

Clearly boen_robot has far more Mikrotik Scripting experience, that is a very neat solution indeed.

How would you go about (in the second example), making sure that entries are also removed?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: synchronize two address-list certain time

Thu Apr 02, 2015 3:41 pm

Oh. Yeah... I should've added a remove there... OK, I've fixed it above, so that the old address list is cleared before the items are (re)added.
 
operhunt
just joined
Posts: 2
Joined: Wed Jan 19, 2011 1:18 am

Re: synchronize two address-list certain time

Tue Mar 03, 2020 5:03 pm

Hi. I try to synchronize by example 1, and I get this error:
PHP Fatal error:  Uncaught exception 'PEAR2\Net\RouterOS\DataFlowException' with message 'Asynchonous commands must have a tag.' in phar:///home/kolan/sync/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Client.php:359
Stack trace:
#0 /home/kolan/sync/sync.php(34): PEAR2\Net\RouterOS\Client->sendAsync(Object(PEAR2\Net\RouterOS\Request), Object(Closure))
#1 {main}
  thrown in phar:///home/kolan/sync/PEAR2_Net_RouterOS-1.0.0b5.phar/PEAR2_Net_RouterOS-1.0.0b5/src/PEAR2/Net/RouterOS/Client.php on line 359
what I'm doing wrong?)

Who is online

Users browsing this forum: No registered users and 4 guests