Community discussions

MikroTik App
 
necker
just joined
Topic Author
Posts: 1
Joined: Fri Nov 03, 2023 10:55 pm

Script for CSV DHCP export

Fri Nov 03, 2023 11:09 pm

Hello, New to scripting and been trying to work it out on my own but need a hand.
I have put together the below script and tried other variants but I can only get 1 of the variables to pull at a time and export. doesnt matter if its address, mac-address, server etc. The min I add more variables it creates a file that shows the current file structure and not the output.
If I try multiple :local address :local mac etc it will not even create a file.
Open for my noob ideas to be fixed. 2 versions below I have played with.

:local output ""
:local filename "dhcp-leases.txt"
:local entry
/ip dhcp-server lease
:foreach i in=[find where server=fttx-public-dhcp] do={
    :set entry ([get $i address].",".[get $i mac-address].",".[get $i client-id].",".[get $i server])

    :set output ($output . "$entry\r\n")
}
/file print file=$filename
:delay 2s
/file set $filename contents=$output
:local filename "dhcp-leases.txt"
/file set $filename contents=""

/ip dhcp-server lease
:foreach i in=[find where server=fttx-public-dhcp] do={
    :local address [get $i address]
    :local mac [get $i mac-address]
    :local client_id [get $i client-id]
    :local server [get $i server]
    :local entry "$address,$mac,$client_id,$server"
    
    :put ("$entry")
    /file print file=$filename
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script for CSV DHCP export

Sat Nov 04, 2023 9:53 am

file is limited to 4KB, if you go over that limit 1st script do not work.

do not use the var name like the named item in the context..

the second script is ALSO full of logical errors...

working code

:execute script="/ip dhcp-server lease\r\
    \n:foreach i in=[find where server=\"fttx-public-dhcp\"] do={\r\
    \n    :local add [get \$i address]\r\
    \n    :local mac [get \$i mac-address]\r\
    \n    :local cid [get \$i client-id]\r\
    \n    :local svr [get \$i server]\r\
    \n    :local line \"\\\"\$add\\\",\\\"\$mac\\\",\\\"\$cid\\\",\\\"\$svr\\\"\"\r\
    \n    :put \$line\r\
    \n}\r\
    \n" file="dhcp-leases.txt"

Who is online

Users browsing this forum: sutrus and 3 guests