Community discussions

MikroTik App
 
saied
just joined
Topic Author
Posts: 5
Joined: Tue Mar 17, 2020 7:04 pm

Help with foreach script

Mon Jan 04, 2021 10:11 pm

I have a command that I can run from the terminal that can post an IP assignment and A MAC address to a specific site.
/tool fetch url="https://mysite.com/api/dhcp_assignments ... b20cf26f97" mode=https keep-result=no

However I would like to make it a automated so it grabs all the IP and mac addresses from the DHCP server and posts them to the above site. Can someone tell me how I would use a foreach loop in here? I am a newb in programming and I could not find a way to use the foreach loop.
Thanks
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Help with foreach script  [SOLVED]

Tue Jan 05, 2021 4:05 pm

Try something like this, adjust for yourself as needed:

/ip dhcp-server lease
:foreach i in=[find] do={
  :local add [get $i address]
  :local mac [get $i mac-address]

  /tool fetch url=("https://mysite.com/api/dhcp_assignments%5C?ip_address=" . $add . "&leased_mac_address=" . $mac . "&expired=1&api_key=some_api_key_here") mode=https keep-result=no
}
 
saied
just joined
Topic Author
Posts: 5
Joined: Tue Mar 17, 2020 7:04 pm

Re: Help with foreach script

Tue Jan 05, 2021 5:19 pm

Wow. Thank you. After messing around with it a bit I figured it out. I wanted to thank you so much for your help on this one.
Is there a course(paid or free) that would walk us noobs on programming languages on how to do these stuff. This is very cool.

Thanks again for your help!
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Help with foreach script

Tue Jan 05, 2021 11:45 pm

The Mikrotik Wiki article for scripting has very good information:
https://wiki.mikrotik.com/wiki/Manual:Scripting

I would suggest giving that a read, and playing around the CLI.
It fairly easy to understand and start scripting for yourself :)
 
austinNz
just joined
Posts: 6
Joined: Tue Sep 21, 2021 7:54 pm
Contact:

Re: Help with foreach script

Sat Oct 09, 2021 8:29 am


I have created a batch of hotspot users under /ip hotspot user, i filled every parameter except the limit-bytes-total. I would like to user a :foreach conditional loop script to set that total bytes limit. I've have tried several scripts to no avail, kindly help me there. The commom parameters are as follows: profile=3GiB7d uptime-limit=7d 00:00:00, I'd like to set the limit-bytes-total to 3G for all users under that "3GiB7d" profile

Who is online

Users browsing this forum: No registered users and 16 guests