Script for list of connected devices via ARP not working

Hello, I have tried to create a script which creates a file with a list of assigned IP addresses and the MAC address it is assigned to by going through the ARP table:

{
:local buffer;
:local fileName "address-list"
{
	:local ip
	/interface bridge host 
	:foreach ID in=[find] do={
		:local mac [get $ID mac-address]
		:local idmac [/ip arp find mac-address="$mac"]
		:if ([:len $idmac] > 0) do={
			:set ip [/ip arp get $idmac address]
                                                :put "mac=$mac ip=$ip"
                                                :set $buffer ($buffer."IPV4=".$ip." MAC=".$mac."\n");
		}
	}
}
/file print file=$fileName where name="";
delay 1s;
/file set $fileName contents=$buffer;
}

Yet when i execute the code I get an error “invalid internal item number”. Does anyone know of a way to solve this problem or tell me what I am doing wrong in the script. I am using RouterOS Ver. 7.5. Thank you in advance.

First of all, is better to clean the script, before analyzing the problem.
(renamed ID to item, ID can be a reserved word sometime…)
{
:local buffer “”
:local fileName “address-list”

/interface bridge host
:foreach item in=[find] do={
:local mac [get $item mac-address]
:local idmac [/ip arp find where mac-address=$mac]
:if ([:len $idmac] > 0) do={
:local ip [/ip arp get $idmac address]
:put “IPv4=$ip MAC=$mac”
:set $buffer ($buffer.“IPv4=$ip MAC=$mac\r\n”)
}
}

/file print file=$fileName
:delay 1s
/file set $fileName contents=$buffer
}

Why you go trough the bridge/host when you have all already on ARP table?

If the entry on bridge/host or on ARP be cleaned for timeout, when “/ip arp get” is executed, it does not find that id and returns that error.


Just on one line:

/ip arp print file="address-list" terse where complete and !invalid

Hello, thank you for such a fast reply. The script you modified does look more cleaned up, yet when i run it, same error occurs (i guess its just cleaned up, not fixed yet). The code is what I used as a combination of 2 other scripts I have found earlier on this forum. When I was doing trial runs on my test Mikrotik Router (6.48.6), this problem hasn’t appeared. What part of the script should I edit for it to go through the whole ARP table and create a list of all MAC addresses with the IP assigned to them (not by interface/bridge)?

Now I see, thank you. It does seem to simplify everything by a lot. I will try to use this script and clean up the results received when i download the txt file for analysis. Cheers.