Why is this so hard???

I don’t understand (clearly) what I’m doing wrong. I’ve tried a million variations. Here are 2 ways that don’t work:

:local stringexec "/ip dhcp-server lease :foreach i in=[find] do={ :put ([get $i comment].",".[get $i address].",".[get $i mac-address].",".[get $i host-name]) }"

:execute $stringexec file=temp1.txt



:foreach i in=[/ip dhcp-server lease find] do={
:put ([get $i comment].",".[get $i address].",".[get $i mac-address].",".[get $i host-name])
}
file="test1.txt"

You have to put extra lines between the code parts so they are displayed separate.

Thank you, but I don’t understand.

Put extra lines so they display better here on the forum? Like this:

:foreach i in=[/ip dhcp-server lease find] do={


:put ([get $i comment].",".[get $i address].",".[get $i mac-address].",".[get $i host-name])


}


file="test1.txt"

You need to escape " and $ characters, also ; is missing after /ip dhcp-server lease to separate command.

:local stringexec "/ip dhcp-server lease; :foreach i in=[find] do={ :put ([get \$i comment].\",\".[get \$i address].\",\".[get \$i mac-address].\",\".[get \$i host-name]) }"
:execute $stringexec file=temp1.txt

That works – thank you.

But, I think it’s not giving me the desired results because there is no field named “comment”

You wrote script for your needs I just corrected it to work… Hard to guess what result you are expecting from it if not specified.

In the meantime, write the script in a decent way, then the rest comes.
:execute file=temp1.txt
“/ip dhcp-server lease; :foreach i in=[find] do={:put ("$[get $i mac-address],$[get $i address],$[get $i host-name],$[get $i comment]")}”
You can’t obtain host-name or comment if is not already present on lease, and dynamic leases do not have any comment…

Okay, thank you guys – making progress.

I have this version:

/file remove [find where name=temp2.txt]

/system

:local identitydate "$[identity get name]_$[clock get date]"

:local stringexec "/ip dhcp-server lease; :foreach i in=[find] do={ :put ([get \$i address].\",\".[get \$i comment].\",\",[get \$i mac-address].\",\".[get \$i host-name]  ) }"


:execute $stringexec file=temp2.txt

/tool fetch address=192.168.2.22 port=21 user=xxxxx password=xxxxsrc-path=temp2.txt mode=ftp dst-path="/mikrotik-backups/$identitydate-leases.txt" upload=yes ascii=yes

It works, sort of: The file “temp2.txt” is created and contains all the DHCP leases.

But, the FTP’d file only contains some of the records.


Specifically:

temp2.txt on the MT contains all 60 entries:

192.168.2.100,TV15,;78:6A:1F:8D:F9:C8,Theater
192.168.2.121,Ipad SRN,;DA:F3:68:BE:3F:0B,
192.168.2.102,STB,;78:6A:1F:8D:FC:B4,Master-Bedroom
192.168.2.101,STB,;78:6A:1F:8D:FC:0F,Living-Room
192.168.2.103,STB,;A0:68:7E:4D:D0:4B,Family-Room
192.168.2.138,MFCL3770CDW,;30:C9:AB:17:71:59,MFC-L3770
192.168.2.107,JRSLaptopASUS,;94:E7:0B:29:30:E7,
192.168.2.141,JRS iPhone,;C2:5D:7F:1F:4C:F5,
192.168.2.106,RB5009,;18:FD:74:CF:7F:5C,
192.168.2.109,Vizio on 15,;00:6B:9E:D1:24:F3,
192.168.2.147,TV,;3C:59:1E:F4:02:EF,212LR
192.168.2.122,Homepod,;D4:90:9C:D8:66:99,Living-Room
192.168.2.191,TV 15 SRN Office,;3C:59:1E:F4:3C:CB,SRNOffice
192.168.2.199,Playstation,;C8:63:F1:F1:9F:44,
192.168.2.119,ThomasMBP,;88:E9:FE:6E:97:9D,
192.168.2.128,MBR65TV,;34:51:80:C8:BB:2C,212MBR
192.168.2.200,HarmonyHub,;00:04:20:F9:31:D2,HarmonyHub
192.168.2.114,Thomas iPhone,;46:B4:96:5E:1A:1B,
192.168.2.176,;18:03:73:3A:63:19,
192.168.2.142,SRNAppleWatch,;4E:FE:92:A6:40:CD,
192.168.2.124,Printer,;2C:6F:C9:5F:BC:EB,BRW2C6FC95FBCEB
192.168.2.173,NC-LT-SN20,;24:EE:9A:54:9A:E8,NC-LT-SN20
192.168.2.117,Printer,;B4:22:00:95:59:8A,BRNB4220095598A
192.168.2.127,Debian LXC under Proxmox,;0A:25:A1:71:46:7D,Debian
192.168.2.110,JRS-Laptop-2023,;64:49:7D:61:AE:2C,JRS-Laptop-2023
192.168.2.166,15 TV,;B0:A7:37:75:B6:60,15Theatre
192.168.2.105,Susans-iPhone,;C4:17:FE:43:33:07,
192.168.2.104,;90:6E:BB:46:FA:09,
192.168.2.108,Denon AVR,;00:05:CD:19:3C:07,0005CD193C07
192.168.2.116,SRN iphone,;EA:C1:05:82:99:7C,
192.168.2.120,Thomas iPad large,;96:4E:A5:1A:A9:74,
192.168.2.123,Thomas Acer,;54:6C:EB:7B:A2:C3,TS-aceri7
192.168.2.113,hEX,;18:FD:74:38:81:2B,
192.168.2.112,JRS iPhone 2023,;FC:AA:81:2A:1F:B4,Jrs-iphone-2023
192.168.2.118,SRN Apple Watch,;36:41:EF:17:D0:C9,
192.168.2.115,Susan iPad,;16:31:50:11:6B:CF,
192.168.2.126,NOT any Thomas or Susan’s Device,;7A:49:88:57:E9:14,
192.168.2.130,Padan?,;3C:06:30:20:01:70,Jonnys-MBP-3
192.168.2.133,???,;F6:B9:88:DD:23:1A,
192.168.2.135,;58:E4:88:97:C9:D1,
192.168.2.136,;44:3D:54:A2:D1:14,
192.168.2.134,;BE:22:C3:46:12:33,
192.168.2.111,THR316D T BR,;C8:F0:9E:E8:8A:E4,espressif
192.168.2.131,SRN iwatch,;D6:A9:86:B1:C9:3E,
192.168.2.129,Thomas – ,;22:BC:D8:7F:66:FD,
192.168.2.132,;3C:A6:F6:1F:87:AC,Rachels-Air
192.168.2.139,Rachel phone,;1A:B9:14:B4:55:EA,
192.168.2.140,Mini-PC from aliexpress,;68:1D:EF:38:E5:9B,Mini-PC-N100-Aliexpress-1
192.168.2.125,Thomas iPhone,;2E:EF:FE:36:A1:05,
192.168.2.137,JRS 2024 Desktop,;C8:7F:54:5A:69:13,JRS-2024-Deskto
192.168.2.144,;3C:06:30:0C:EE:88,Sarahs-MBP-2
192.168.2.150,;84:57:33:9B:83:85,XBOXONE
192.168.2.154,;08:C3:B3:DF:26:62,65TCLRokuTV
192.168.2.153,;5A:E0:C5:BC:79:F4,
192.168.2.172,;E2:FB:B7:20:0D:6B,
192.168.2.174,;C0:49:EF:8C:63:98,shellyplus1pm-c049ef8c6398
192.168.2.171,;0C:62:A6:1E:8B:18,49TCLRokuTV
192.168.2.149,;68:1D:EF:3A:DA:E0,T8-Mini-PC-26NNQ3ARVB1
192.168.2.175,;CE:02:38:11:8C:3E,

But the FTP’d file contains only 19 entries:

192.168.2.100,TV15,;78:6A:1F:8D:F9:C8,Theater
192.168.2.121,Ipad SRN,;DA:F3:68:BE:3F:0B,
192.168.2.102,STB,;78:6A:1F:8D:FC:B4,Master-Bedroom
192.168.2.101,STB,;78:6A:1F:8D:FC:0F,Living-Room
192.168.2.103,STB,;A0:68:7E:4D:D0:4B,Family-Room
192.168.2.138,MFCL3770CDW,;30:C9:AB:17:71:59,MFC-L3770
192.168.2.107,JRSLaptopASUS,;94:E7:0B:29:30:E7,
192.168.2.141,JRS iPhone,;C2:5D:7F:1F:4C:F5,
192.168.2.106,RB5009,;18:FD:74:CF:7F:5C,
192.168.2.109,Vizio on 15,;00:6B:9E:D1:24:F3,
192.168.2.147,TV,;3C:59:1E:F4:02:EF,212LR
192.168.2.122,Homepod,;D4:90:9C:D8:66:99,Living-Room
192.168.2.191,TV 15 SRN Office,;3C:59:1E:F4:3C:CB,SRNOffice
192.168.2.199,Playstation,;C8:63:F1:F1:9F:44,
192.168.2.119,ThomasMBP,;88:E9:FE:6E:97:9D,
192.168.2.128,MBR65TV,;34:51:80:C8:BB:2C,212MBR
192.168.2.200,HarmonyHub,;00:04:20:F9:31:D2,HarmonyHub
192.168.2.114,Thomas iPhone,;46:B4:96:5E:1A:1B,
192.168.2.176,;18:03:73:3A:63:19,

Like the FTP upload session gets halted in the middle. The FTP server on my PC says it finished fine.

I tried using “ascii=no” and I tried using the same src and dst file name. Still the same.

Any ideas?

Okay, thank you guys – making progress.

I have this version:

/file remove [find where name=temp2.txt]

/system

:local identitydate "$[identity get name]_$[clock get date]"

:local stringexec "/ip dhcp-server lease; :foreach i in=[find] do={ :put ([get \$i address].\",\".[get \$i comment].\",\",[get \$i mac-address].\",\".[get \$i host-name]  ) }"


:execute $stringexec file=temp2.txt

/tool fetch address=192.168.2.22 port=21 user=xxxxx password=xxxxsrc-path=temp2.txt mode=ftp dst-path="/mikrotik-backups/$identitydate-leases.txt" upload=yes ascii=yes

It works, sort of: The file “temp2.txt” is created and contains all the DHCP leases.

But, the FTP’d file only contains some of the records.


Specifically:

temp2.txt on the MT contains all 60 entries:

192.168.2.100,TV15,;78:6A:1F:8D:F9:C8,Theater
192.168.2.121,Ipad SRN,;DA:F3:68:BE:3F:0B,
192.168.2.102,STB,;78:6A:1F:8D:FC:B4,Master-Bedroom
192.168.2.101,STB,;78:6A:1F:8D:FC:0F,Living-Room
192.168.2.103,STB,;A0:68:7E:4D:D0:4B,Family-Room
192.168.2.138,MFCL3770CDW,;30:C9:AB:17:71:59,MFC-L3770
192.168.2.107,JRSLaptopASUS,;94:E7:0B:29:30:E7,
192.168.2.141,JRS iPhone,;C2:5D:7F:1F:4C:F5,
192.168.2.106,RB5009,;18:FD:74:CF:7F:5C,
192.168.2.109,Vizio on 15,;00:6B:9E:D1:24:F3,
192.168.2.147,TV,;3C:59:1E:F4:02:EF,212LR
192.168.2.122,Homepod,;D4:90:9C:D8:66:99,Living-Room
192.168.2.191,TV 15 SRN Office,;3C:59:1E:F4:3C:CB,SRNOffice
192.168.2.199,Playstation,;C8:63:F1:F1:9F:44,
192.168.2.119,ThomasMBP,;88:E9:FE:6E:97:9D,
192.168.2.128,MBR65TV,;34:51:80:C8:BB:2C,212MBR
192.168.2.200,HarmonyHub,;00:04:20:F9:31:D2,HarmonyHub
192.168.2.114,Thomas iPhone,;46:B4:96:5E:1A:1B,
192.168.2.176,;18:03:73:3A:63:19,
192.168.2.142,SRNAppleWatch,;4E:FE:92:A6:40:CD,
192.168.2.124,Printer,;2C:6F:C9:5F:BC:EB,BRW2C6FC95FBCEB
192.168.2.173,NC-LT-SN20,;24:EE:9A:54:9A:E8,NC-LT-SN20
192.168.2.117,Printer,;B4:22:00:95:59:8A,BRNB4220095598A
192.168.2.127,Debian LXC under Proxmox,;0A:25:A1:71:46:7D,Debian
192.168.2.110,JRS-Laptop-2023,;64:49:7D:61:AE:2C,JRS-Laptop-2023
192.168.2.166,15 TV,;B0:A7:37:75:B6:60,15Theatre
192.168.2.105,Susans-iPhone,;C4:17:FE:43:33:07,
192.168.2.104,;90:6E:BB:46:FA:09,
192.168.2.108,Denon AVR,;00:05:CD:19:3C:07,0005CD193C07
192.168.2.116,SRN iphone,;EA:C1:05:82:99:7C,
192.168.2.120,Thomas iPad large,;96:4E:A5:1A:A9:74,
192.168.2.123,Thomas Acer,;54:6C:EB:7B:A2:C3,TS-aceri7
192.168.2.113,hEX,;18:FD:74:38:81:2B,
192.168.2.112,JRS iPhone 2023,;FC:AA:81:2A:1F:B4,Jrs-iphone-2023
192.168.2.118,SRN Apple Watch,;36:41:EF:17:D0:C9,
192.168.2.115,Susan iPad,;16:31:50:11:6B:CF,
192.168.2.126,NOT any Thomas or Susan’s Device,;7A:49:88:57:E9:14,
192.168.2.130,Padan?,;3C:06:30:20:01:70,Jonnys-MBP-3
192.168.2.133,???,;F6:B9:88:DD:23:1A,
192.168.2.135,;58:E4:88:97:C9:D1,
192.168.2.136,;44:3D:54:A2:D1:14,
192.168.2.134,;BE:22:C3:46:12:33,
192.168.2.111,THR316D T BR,;C8:F0:9E:E8:8A:E4,espressif
192.168.2.131,SRN iwatch,;D6:A9:86:B1:C9:3E,
192.168.2.129,Thomas – ,;22:BC:D8:7F:66:FD,
192.168.2.132,;3C:A6:F6:1F:87:AC,Rachels-Air
192.168.2.139,Rachel phone,;1A:B9:14:B4:55:EA,
192.168.2.140,Mini-PC from aliexpress,;68:1D:EF:38:E5:9B,Mini-PC-N100-Aliexpress-1
192.168.2.125,Thomas iPhone,;2E:EF:FE:36:A1:05,
192.168.2.137,JRS 2024 Desktop,;C8:7F:54:5A:69:13,JRS-2024-Deskto
192.168.2.144,;3C:06:30:0C:EE:88,Sarahs-MBP-2
192.168.2.150,;84:57:33:9B:83:85,XBOXONE
192.168.2.154,;08:C3:B3:DF:26:62,65TCLRokuTV
192.168.2.153,;5A:E0:C5:BC:79:F4,
192.168.2.172,;E2:FB:B7:20:0D:6B,
192.168.2.174,;C0:49:EF:8C:63:98,shellyplus1pm-c049ef8c6398
192.168.2.171,;0C:62:A6:1E:8B:18,49TCLRokuTV
192.168.2.149,;68:1D:EF:3A:DA:E0,T8-Mini-PC-26NNQ3ARVB1
192.168.2.175,;CE:02:38:11:8C:3E,

But the FTP’d file contains only 19 entries:

192.168.2.100,TV15,;78:6A:1F:8D:F9:C8,Theater
192.168.2.121,Ipad SRN,;DA:F3:68:BE:3F:0B,
192.168.2.102,STB,;78:6A:1F:8D:FC:B4,Master-Bedroom
192.168.2.101,STB,;78:6A:1F:8D:FC:0F,Living-Room
192.168.2.103,STB,;A0:68:7E:4D:D0:4B,Family-Room
192.168.2.138,MFCL3770CDW,;30:C9:AB:17:71:59,MFC-L3770
192.168.2.107,JRSLaptopASUS,;94:E7:0B:29:30:E7,
192.168.2.141,JRS iPhone,;C2:5D:7F:1F:4C:F5,
192.168.2.106,RB5009,;18:FD:74:CF:7F:5C,
192.168.2.109,Vizio on 15,;00:6B:9E:D1:24:F3,
192.168.2.147,TV,;3C:59:1E:F4:02:EF,212LR
192.168.2.122,Homepod,;D4:90:9C:D8:66:99,Living-Room
192.168.2.191,TV 15 SRN Office,;3C:59:1E:F4:3C:CB,SRNOffice
192.168.2.199,Playstation,;C8:63:F1:F1:9F:44,
192.168.2.119,ThomasMBP,;88:E9:FE:6E:97:9D,
192.168.2.128,MBR65TV,;34:51:80:C8:BB:2C,212MBR
192.168.2.200,HarmonyHub,;00:04:20:F9:31:D2,HarmonyHub
192.168.2.114,Thomas iPhone,;46:B4:96:5E:1A:1B,
192.168.2.176,;18:03:73:3A:63:19,

Like the FTP upload session gets halted in the middle. The FTP server on my PC says it finished fine.

I tried using “ascii=no” and I tried using the same src and dst file name. Still the same.

Any ideas?

nevermind…

Just needed a “:delay 30” before the ftp command.

Execute is async command.

is there no way in ROS Script to wait for the async process? callback function? promise? anything but :delay?

edit: using the “as-string” argument. better than nothing

Yes, read my snippets...

{
:local jobID [:execute file=temp1.txt
"/ip dhcp-server lease; :foreach i in=[find] do={:put ("$[get $i mac-address],$[get $i address],$[get $i host-name],$[get $i comment]")}"]
:while ([:len [/system script job find where .id=$jobID]] > 0) do={
:delay 1s
}
}

what????

see docs for “:execute” command - https://help.mikrotik.com/docs/display/ROS/Scripting

Execute the script in the background. The result can be written in the file by setting a “file” parameter or printed to the CLI by setting “as-string”.
When using the “as-string” parameter executed script is blocked (not executed in the background).

Why not just perform commands without :execute then?

I’m not a programmer but (or, more precisely, even) I know that this language is incomprehensible. My intention is not to criticize the docs. It is only to point out that I (and I am certain, many others) would very much like to learn this and cannot do so in part because of language like this.

That "snippet" of code is so incredibly full of skill! The 'density' of knowledge reflected in it is incredible.

Do you do this professionally?

@infabo

Often when I write something I check first…
The grammatical error in the text is another thing, but I read what others write…

Why is not present until “” is present??? I’m expecting the full list…
[admin@] > :execute
file script
[admin@] > :execute “”
as-string file
[admin@] > :execute
file script
But the purpose of the first version of the script is to write the file, not to have a variable.

Once upon a time, I got annoyed at how idiotic customers are, so I opened a WISP business and stopped programming for work.

It seems that the final goal is to have that file, not to print something on screen…