Community discussions

MikroTik App
 
hazartilirot
newbie
Topic Author
Posts: 31
Joined: Thu Sep 17, 2020 11:48 pm
Location: Lviv

script importing MAC table

Sun Sep 27, 2020 9:04 pm

Well, I've just started using one of Mikrotik devices and decided to import a table of MACs from my previous device. The thing is that I'm a newcomer and have by no means dealt with Mikrotik before.

There is a comma separated values in file MAC,ADDRESS,SERVER,COMMENT
00:00:00:00:00:00,XXX.XXX.XXX.XXX,DEFCONF,HOSTNAME1
00:00:00:00:00:00,XXX.XXX.XXX.XXX,DEFCONF,HOSTNAME2
00:00:00:00:00:00,XXX.XXX.XXX.XXX,DEFCONF,HOSTNAME3
00:00:00:00:00:00,XXX.XXX.XXX.XXX,DEFCONF,HOSTNAME4
The logic is simple is that we open a file, read content line by line chunking it by breaklines "\r\n".
Comma separated values is considered a values in an array, so the string I get I split into an array.
Once it's split it should get each value corresponding with its index and add the record to the mac table.
:local filename "mac_table.txt";
:local content [/file get [/file find name=$filename] contents];

:foreach line in=[:find $content "\n" ] do={
	:local array [:toarray $line];
    :local mac-address = array[1]
    :local address = array[2]
	:local server = array[3]
	:local comment = array[4]
    :if ([/ip dhcp-server lease find=$mac-address] = false) do {
		/ip dhcp-server lease add mac-address=$mac-address address=$address server=$server comment=$comment
	} 
}

Actually, the script doesn't work. The problem I'm facing I cannot understand how to debug it?
 
hazartilirot
newbie
Topic Author
Posts: 31
Joined: Thu Sep 17, 2020 11:48 pm
Location: Lviv

Re: script importing MAC table

Mon Sep 28, 2020 12:08 am

I've changed my script, but it doesn't work either.... I don't know why....
:local filename "mac_table.txt";
:local content [/file get [/file find name=$filename] contents];
:delay 1;
:foreach line in=[:find "\r" $content] do={
	:local array [:toarray $line];
    :local mac-address [:pick $array 0];
    :local address [:pick $array 1];
	:local server [:pick $array 2];
	:local comment [:pick $array 3];
	
    :if ([/ip dhcp-server lease get [/ip dhcp-server lease find mac-address=$mac-address] mac-address] != $mac-address) do {
		/ip dhcp-server lease add mac-address=$mac-address address=$address server=$server comment=$comment
	} 
}
Last edited by hazartilirot on Mon Sep 28, 2020 12:25 am, edited 1 time in total.