-2

mikrotik script automaticaly change dynamic IP to static IP with comment as Date

:local comment
:local address
:local mac-address
{
/ip dhcp-server lease
:foreach a in=[find where dynamic] do={
:set dhcpip [ get $i address ];
:set mac [ get $i mac-address ];

add address=$dhcpip mac-address=$mac comment="$date";
}}

not work where the problem ?

Mike
  • 7
  • 1
  • 5

1 Answers1

1

If you run the script from the terminal then you'd get useful error messages to help you correct your own script. To run the script go to /system script then get the number for the script by running print. The script can then be run with run <number>. You can also see errors syntax highlighted in your script from the output of print.

The first error received was:

expected end of command (line 3 column 11)

This is because your variable name includes a - which is not a valid character for variable names - see Manual:Scripting#Variables.

In correcting this error by making the variable simply mac I notice that the variables you have declared at the top, aren't the variables that you are using in your script and elsewhere you have already used mac. This is actually the cause of your next error:

syntax error (line 7 column 6)

After updating all variables to be consistent (dhcpip to address and a to i) the final problem is that $date is not defined anywhere. For this, you can add :local date [/system clock get date] somewhere in your script. With that, you should be able to fix your own script - and I highly recommend that you do so that you can learn from your mistakes.

One critique of the script, beyond its poor implementation, is that rather than storing into variables you can use the make-static command. The following is a script that does just this:

/ip dhcp-server lease
:foreach i in=[find dynamic] do={
  make-static $i
  set $i comment=[/system clock get date]
}

If you don't need the comment then making all dynamic leases static can be done with a single command:

/ip dhcp-server lease make-static [find dynamic]
flungo
  • 1,050
  • 1
  • 7
  • 21