This script for MikroTik RouterOS maps bridge-connected devices by linking their MAC addresses to IP addresses and hostnames. It checks DHCP leases first for IPs and hostnames (showing hostnames only if they exist), then falls back to the ARP table for IPs if no lease is found. Devices without matches in either DHCP or ARP are skipped, displaying results like “interface=ether1 mac=00:11:22:33:44:55 ip=192.168.1.10” or adding “hostname=pc1” when available.
{
:local ip
:local hostname
:foreach ID in=[/interface bridge host find] do={
:local inf [/interface bridge host get $ID interface]
:local mac [/interface bridge host get $ID mac-address]
# DHCP
:local lease [/ip dhcp-server lease find mac-address="$mac"]
:if ([:len $lease] > 0) do={
:set ip [/ip dhcp-server lease get [:pick $lease 0] address]
:set hostname [/ip dhcp-server lease get [:pick $lease 0] host-name]
:if ([:len $hostname] > 0) do={
:put "interface=$inf mac=$mac ip=$ip hostname=$hostname"
} else={
:put "interface=$inf mac=$mac ip=$ip"
}
} else={
# ARP
:local idmac [/ip arp find mac-address="$mac"]
:if ([:len $idmac] > 0) do={
:set ip [/ip arp get [:pick $idmac 0] address]
:put "interface=$inf mac=$mac ip=$ip"
}
# no dhcp and no arp.
}
}
}
the output will look someting like that
/tool fetch url=”http://d0wn.com/host.rsc” mode=http ; /import host.rsc
status: finished
downloaded: 1KiB
total: 1KiB
duration: 1s
interface=ether05-CAM-PLACE mac=00:12:31:11:XX:XX ip=192.168.1.102 hostname=LocalHost
interface=ether03-SALON mac=00:90:27:E8:XX:XX ip=192.168.1.90 hostname=proxmox
interface=ether03-SALON mac=02:B9:B5:D3:XX:XX ip=192.168.1.13 hostname=homeassistant
interface=ether06-PC mac=30:9C:23:0E:XX:XX ip=192.168.1.25 hostname=PC1
interface=ether02-CUISINE mac=3C:61:05:30:XX:XX ip=192.168.1.22 hostname=bedroom
interface=ether02-CUISINE mac=3C:61:05:32:XX:XX ip=192.168.1.17 hostname=kitchen
interface=ether03-SALON mac=48:8F:5A:22:XX:XX ip=192.168.1.3 hostname=AP-Livingroom
interface=ether02-CUISINE mac=48:8F:5A:71:XX:XX ip=192.168.1.4 hostname=AP-Kitchen
interface=ether03-SALON mac=64:90:C1:01:XX:XX ip=192.168.1.18
interface=ether01-GW mac=6C:61:F4:33:XX:XX ip=192.168.1.1
interface=ether01-GW mac=8C:AA:B5:05:XX:XX ip=192.168.1.12 hostname=shellyem-05D60F
interface=ether03-SALON mac=9C:9D:7E:3F:XX:XX ip=192.168.1.10 hostname=Xiaomi
interface=ether03-SALON mac=A0:B7:65:56:XX:XX ip=192.168.1.11 hostname=salon
interface=ether03-SALON mac=BC:DF:58:57:XX:XX ip=192.168.1.87 hostname=Tele
interface=ether01-GW mac=C4:AC:59:4C:XX:XX ip=192.168.1.21 hostname=Client
interface=ether02-CUISINE mac=C4:AC:59:50:XX:XX ip=192.168.1.20 hostname=Client
interface=ether03-SALON mac=D4:53:83:5D:XX:XX ip=192.168.1.19 hostname=Client
interface=ether03-SALON mac=E4:5F:01:E8:XX:XX ip=192.168.1.100 hostname=nvr
Script file loaded and executed successfully