script problem

Hello,

I have a scripting problem.
I have two routers, both running ROS version 7.16.1. I make the same settings on both routers, everything is perfect on the first, but I run into problems on the second.

So: There is only one DHCP client configured in the router. I want to switch the “Add default route” part of this dhcp client (from yes to no and vice versa).

There is only this in the script:

/ip dhcp-client set add-default-route=yes 0

If I run this command from the terminal, it does what it needs to do without any errors.
If it is run from a script, I get the following error message: “executing script failed, please check it manually”.

If I rewrite the end of the command from 0 to 1, so:

/ip dhcp-client set add-default-route=yes 1

Then it runs perfectly from the script, without errors, and also sets add-default-route to yes. However, from the terminal, the command gives an error (no such item), which is completely correct, since there is only one dhcp client, so there is no number 1.

If I set the end of the command to 0 in the script back, the script gives an error, but when started the script from the terminal, the script runs without error.

What could be causing this problem?

Thanks,
Szilard

Additional info:
Previously there are an inactive (disabled) dhcp client in the router too. At that time, even the script worked well, but it only touched the inactive client. I deleted this inactive client, then the numbering started to slip on this router. The /ip/dhcp-client print shows the client to be set as zero (there is only this one), but I can still only connect it with 1 from the script.

Bad idea to use such direct numbers in script.
use find construction.

Suppose, we have dhcp clients:

[admin@nufnuf] /ip/dhcp-client> print
Flags: X - DISABLED
Columns: INTERFACE, USE-PEER-DNS, ADD-DEFAULT-ROUTE, STATUS, ADDRESS
#   INTERFACE  USE-PEER-DNS  ADD-DEFAULT-ROUTE  STATUS  ADDRESS          
0 X wan1       yes           no                                          
1 X wan2       yes           no                                          
2   manage     no            no                 bound   192.168.56.105/24

to change somehing manually in terminal, you use numbers (0, 1, etc)
to change somethin in script use find.

try in terminal:

[admin@nufnuf] /ip/dhcp-client> print
Flags: X - DISABLED
Columns: INTERFACE, USE-PEER-DNS, ADD-DEFAULT-ROUTE, STATUS, ADDRESS
#   INTERFACE  USE-PEER-DNS  ADD-DEFAULT-ROUTE  STATUS  ADDRESS          
0 X wan1       yes           no                                          
1 X wan2       yes           no                                          
2   manage     no            no                 bound   192.168.56.105/24
[admin@nufnuf] /ip/dhcp-client> :put [find interface=wan1]
*1
[admin@nufnuf] /ip/dhcp-client> set add-default-route=yes [find interface=wan1]
[admin@nufnuf] /ip/dhcp-client> print
Flags: X - DISABLED
Columns: INTERFACE, USE-PEER-DNS, ADD-DEFAULT-ROUTE, STATUS, ADDRESS
#   INTERFACE  USE-PEER-DNS  ADD-DEFAULT-ROUTE  STATUS  ADDRESS          
0 X wan1       yes           yes                                         
1 X wan2       yes           no                                          
2   manage     no            no                 bound   192.168.56.105/24
[admin@nufnuf] /ip/dhcp-client> set add-default-route=no [find interface=wan1]   
[admin@nufnuf] /ip/dhcp-client> print                                         
Flags: X - DISABLED
Columns: INTERFACE, USE-PEER-DNS, ADD-DEFAULT-ROUTE, STATUS, ADDRESS
#   INTERFACE  USE-PEER-DNS  ADD-DEFAULT-ROUTE  STATUS  ADDRESS          
0 X wan1       yes           no                                          
1 X wan2       yes           no                                          
2   manage     no            no                 bound   192.168.56.105/24

In scripts “numbers” is internal numbers like *1 in second command. “find” counstructions locate them.

This is how you help others, explaining the why of things, so we all learn.

Thanks @tarasfilatov

The answer is simple: You.

You haven't read the basic scripting guides.
They aren't complete, but it's clearly stated not to use numbers in scripts.
Numbers are for "on the fly" use in the terminal, nothing fixed.



So, suffice just:

/ip dhcp-client set [find] add-default-route="yes"

# or

/ip dhcp-client set [find] add-default-route="no"

as already suggested, it is necessary to correctly identify the DHCP client based on the interface with which it run, could be sufficient (because it is not possible to have more than one on the same interface)

/ip dhcp-client set [find where interface=ether1] add-default-route="yes"

# or

/ip dhcp-client set [find where interface=ether1] add-default-route="no"



"invert" all... so, if is present only one.... (and if is "special-classless" ???)

/ip dhcp-client
:foreach item in=[find] do={
    :local st [get $item add-default-route]
    :if ($st = "yes") do={ set $item add-default-route="no" }
    :if ($st = "no")  do={ set $item add-default-route="yes" }
    # :if ($st = "special-classless") do={ [...] }
}



So, the ID 0 is already asigned to that, IDs are never reused. IDs and list numbers on print are different things.

Like up, IDs are never reused. IDs and list numbers on print are different things.

The new created dhcp client (since IDs are never reused) get the ID 1 (and again IDs and list numbers on print are different things.),
so is obvious that work only with the correct ID 1.
For example, if you do print on /ip dhcp-client and you digit :put [get 0], you can read .id=*1; etc. on terminal.
[rex@MATRIX] /ip dhcp-client> /ip dhcp-client
[rex@MATRIX] /ip dhcp-client> print
Flags: X - disabled, I - invalid, D - dynamic

INTERFACE USE-PEER-DNS ADD-DEFAULT-ROUTE STATUS ADDRESS

0 ether1 no no bound 127.0.0.101/24
[rex@MATRIX] /ip dhcp-client> :put [get 0]
.id=*1;add-default-route=no;address=127.0.0.101/24;dhcp-options=hostname;
clientid;dhcp-server=127.0.0.1;disabled=false;dynamic=false;expires-after=00:00:34;
gateway=127.0.0.1;interface=ether1;invalid=false;netmask=255.255.255.0;
primary-dns=127.0.0.1;primary-ntp=127.0.0.1;secondary-dns=127.0.0.2;
secondary-ntp=127.0.0.2;status=bound;use-peer-dns=false;use-peer-ntp=false