Community discussions

MikroTik App
 
User avatar
adrianTNT
Member Candidate
Member Candidate
Topic Author
Posts: 113
Joined: Sun Mar 10, 2019 4:27 am
Location: The Internet
Contact:

error [Expected end of command] when trying [on-error]

Sun Feb 04, 2024 6:30 am

I have this below script.
If it matters, it updates value of ip address from the hostname mentioned in the comment.
The problem is, this loop breaks if it reaches an inexistent host name "failure: dns name does not exist"
How can I suppress that error so that it continues in case of error ?

I tried adding on-error={}; (it is near the end of the script), but I am getting "expected end of command" when adding that.

Maybe I just don't understand the format of the if / do in these scripts.
# define variables
:local list
:local comment
:local newip
:local oldip

# Loop through each entry in the address list.
:foreach i in=[/ip firewall address-list find] do={

    # Get the first five characters of the list name
    #:set list [:pick [/ip firewall address-list get $i list] 0 5]
    :set list [/ip firewall address-list get $i list]

    # If they're 'adr_ip_', then we've got a match - process it
    :if ($list = "adr_ip_list") do={
    
        # Get the comment for this address list item (this is the host name to use)
        :set comment [/ip firewall address-list get $i comment]
        :set oldip [/ip firewall address-list get $i address]
    
        # Resolve it and set the address list entry accordingly.
        : if ($newip != $oldip) do={
            :set newip [:resolve $comment]
            /ip firewall address-list set $i address=$newip
        } 
        # if I enable this here, I get error "expected end of command"  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< HERE <<<<<<<<<<<<
        # on-error={};

        :put $comment;
        :put $newip;
    }
}
 
optio
Long time Member
Long time Member
Posts: 675
Joined: Mon Dec 26, 2022 2:57 pm

Re: error [Expected end of command] when trying [on-error]  [SOLVED]

Mon Feb 05, 2024 11:16 pm

 
User avatar
adrianTNT
Member Candidate
Member Candidate
Topic Author
Posts: 113
Joined: Sun Mar 10, 2019 4:27 am
Location: The Internet
Contact:

Re: error [Expected end of command] when trying [on-error]

Tue Feb 06, 2024 12:03 am

@optio that worked, thank you.
I am writing the final code for me or others that might run into this ...

So this on-error location would NOT work:
:if ($newip != $oldip) do={
	:set newip [:resolve $comment]
	/ip firewall address-list set $i address=$newip
} on-error={ put "error resolving host"}

Because on-error needs it's own separate :do{} part, besides the existent one, already inside if condition. This one works:
:if ($newip != $oldip) do={
	:do {
		:set newip [:resolve $comment]
		/ip firewall address-list set $i address=$newip
	} on-error={ put "error resolving host"}
}

Who is online

Users browsing this forum: tikworx and 9 guests