Community discussions

MikroTik App
 
ArtPiece55
just joined
Topic Author
Posts: 3
Joined: Thu Oct 20, 2022 11:21 pm

Script for list of connected devices via ARP not working

Thu Oct 20, 2022 11:34 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script for list of connected devices via ARP not working

Thu Oct 20, 2022 11:45 pm

First of all, is better to clean the script, before analyzing the problem.
(renamed ID to item, ID can be a reserved word sometime...)

cleaned code

{
: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
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script for list of connected devices via ARP not working

Thu Oct 20, 2022 11:54 pm

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
 
ArtPiece55
just joined
Topic Author
Posts: 3
Joined: Thu Oct 20, 2022 11:21 pm

Re: Script for list of connected devices via ARP not working

Fri Oct 21, 2022 12:05 am

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)?
 
ArtPiece55
just joined
Topic Author
Posts: 3
Joined: Thu Oct 20, 2022 11:21 pm

Re: Script for list of connected devices via ARP not working

Fri Oct 21, 2022 12:09 am

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.

Who is online

Users browsing this forum: No registered users and 28 guests