Mikrotik Script Creates DHCP Comment with VendorID

PUBLISHED ON NOV 1, 2018 / 1 MIN READ

It has annoyed me for a long time that whenever I connect a new device to my network, I have to go hunting for its IP address.

You can search for the most recent lease, but it would be much more helpful to know which manufacturer the device (or at least its network card) comes from.

The following script, added as a lease script to the MikroTik DHCP server, does exactly that:

# mikrotik mac2vendor


:foreach i in=[/ip dhcp-server lease find mac-address=$leaseActMAC] do={
   :if ([:len [/ip dhcp-server lease get $i comment]] = 0) do={
   
      :local mac [:pick $leaseActMAC 0 8];
      :local mvurl "https://api.macvendors.com/$mac";
      :local result [/tool fetch url=$mvurl as-value output=user ];
      :local vendor ($result->"data");
      :log info "MAC: $mac / VENDOR: $vendor";
      /ip dhcp-server lease set comment="VENDOR: $vendor" $i

   }
}

How it works:

The global variable $leaseActMAC automatically contains the MAC address of the most recently assigned lease, which is then used to search all existing leases.

If no comment is present (length = 0), the MAC address is truncated to its first 8 characters and looked up via the macvendors.com API.

The result is written to the log and to the comment field of the corresponding lease.

TAGS: MIKROTIK