Skip to main content

DHCP Leases to CSV

Random Mikrotik code snippets

CSV output of various things in RouterOS

Static DHCP leases to CSV output

{
    # process only static leases
    :local staticLeases [/ip dhcp-server lease find where dynamic=no]
    :foreach l in=$staticLeases do={
        :local lma [/ip dhcp-server lease get $l mac-address]
        :local la [/ip dhcp-server lease get $l address]
        :local lc [/ip dhcp-server lease get $l comment]
        :put "$lma,$la,\"$lc\""
    }
}

All DHCP leases to CSV output

{
    # process all leases
    :local staticLeases [/ip dhcp-server lease find]
    :foreach l in=$staticLeases do={
        :local lma [/ip dhcp-server lease get $l mac-address]
        :local la [/ip dhcp-server lease get $l address]
        :local lc [/ip dhcp-server lease get $l comment]
        :local ld [/ip dhcp-server lease get $l dynamic]
        :put "$lma,$la,$ld,\"$lc\""
    }
}

-end