Determine the interface to which the device is connected

This is a subtopic of my attempts to monitor the connection of devices to the network. Now I would like to receive a notification when a device connects and receives an IP. For this, I linked the script to lease-script. I do not use CAPsMAN yet. And it would be enough for me to receive a notification like: “DHCP Event on " . $interface . “: MAC=” . $macAddress . “, IP=” . $ipAddress . “, Comment=” . $comment” But I can’t determine the interface to which the device connects. I have several interfaces combined into a bridge. Including wireless. And at the moment I can either get the name of the bridge, or to which wireless the device is connected. Neither arp nor dhcp-server lease give me the name of a specific interface yet. Although there is such information in the Leases tab of Winbox. Here is one of the options that gives partial information.

:global sendTelegramMessage
:local macAddress $leaseActMAC
:local ipAddress $leaseActIP
:local eventTime [/system clock get time]
:local interface "unknown"
:local serverName [/ip dhcp-server get [find where lease-script=dhcp_lease_event] name]
:if ([:len $serverName] > 0) do={
    :local dhcpServer [/ip dhcp-server find where name=$serverName]
    :if ([:len $dhcpServer] > 0) do={
        :set interface [/ip dhcp-server get $dhcpServer interface]
    }
}
:local comment ""
:if ($ipAddress != "") do={
    :set comment [/ip dhcp-server lease get [find where address=$ipAddress] comment]
}
:local message ("DHCP Event on " . $interface . ": MAC=" . $macAddress . ", IP=" . $ipAddress)
:if ([:len $comment] > 0) do={
    :set message ($message . ", Comment=" . $comment)
}
:set message ($message . ", Time=" . $eventTime)
:log info $message
$sendTelegramMessage $message

I tried to define wirelesses this way:

    :local leaseMac [/ip dhcp-server lease get [find where address=$ipAddress] mac-address]
    :if ([:len $leaseMac] > 0) do={
        :local regInterface [/interface wireless registration-table get [find where mac-address=$leaseMac] interface]
        :if ([:len $regInterface] > 0) do={
            :set interface $regInterface
        } else={
            :set interface "Ethernet порт"
        }
    }

But then for some reason only the notification about connecting to wireless works.

I read about such a team:

/interface bridge host get [find where mac-address=$macAddress] on-interface

It works great if you use it in the terminal. But in the script, for some reason, it only responds to a Wi-Fi connection. I do it like this:

:if ([:len $macAddress] > 0) do={
#    /ping count=10 $ipAddress
#    :delay 10
#    :local hostIndex [/interface bridge host find where mac-address=$macAddress]
    :local hostIndex [/interface bridge host get [find where mac-address=$macAddress] on-interface]
    :if ([:len $hostIndex] > 0) do={
        :set interface $hostIndex
    }
}

But even with delays and ping to the device, the script does not respond to the connection via Ethernet.

Thanks for sharing your script — I’ve been working on something similar, and I ran into the same issue with identifying the exact interface when devices connect via a bridge. It’s a bit frustrating that the DHCP lease info doesn’t directly show the physical interface, even though Winbox displays it clearly.

Your approach to check the wireless registration-table is smart. I’ve also used that in the past to distinguish between wireless and wired connections. For wired devices, I ended up relying on /interface bridge host to get the bridge port associated with a MAC address — maybe that could help refine your interface detection?

Would be great if MikroTik exposed this more cleanly in lease scripts, but for now, combining DHCP, registration-table, and bridge host data seems like the way to go. Let me know if you find a cleaner workaround!

I’ve faced similar challenges while setting up network tracking for a facility management advisor platform — clear interface mapping can make a big difference in diagnostics and reporting.