Community discussions

MikroTik App
 
saied
just joined
Topic Author
Posts: 2
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: 1146
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
}
Unimus - configuration management, automation and backup solution
Mass Config Push, network-wide RouterOS upgrades, and more!
 
saied
just joined
Topic Author
Posts: 2
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: 1146
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 :)
Unimus - configuration management, automation and backup solution
Mass Config Push, network-wide RouterOS upgrades, and more!

Who is online

Users browsing this forum: Google [Bot] and 45 guests