Community discussions

MikroTik App
 
User avatar
ocgltd
Member Candidate
Member Candidate
Topic Author
Posts: 112
Joined: Sun Sep 02, 2012 12:53 am
Location: Ontario, Canada

First script problem - just won't execute

Fri Nov 29, 2024 11:22 pm

I'm writing my first script (from examples found online). The first line SHOULD show a message in the system log, but it does not (when I manually start the script from the /SYSTEM/SCRIPTS window. What is wrong?
{
    :log info "DHCP client being executed for backup internet link"
    # Find route by matching comment, and count number of times route exists
    :local count [/ip route print count-only where comment="HOST-ON-WAN-BACKUP"]

    # If just got an IP address
    :if ($bound=1) do={

            # If no such route, just add a new one
            :if ($count = 0) do={
                /ip route add gateway=$"gateway-address" comment="HOST-ON-WAN-BACKUP"
            } 
            # Else at least one such route exists
            else={
                # If found exactly one match, update it
                :if ($count = 1) do={
                    :local test [/ip route find where comment="HOST-ON-WAN-BACKUP"]
                    :if ([/ip route get $test gateway] != $"gateway-address") do={
                        /ip route set $test gateway=$"gateway-address"
                    }
                # Else multiple routes found, unable to update
                } else={
                    :log error "DHCP Client Script: Multiple routes found for HOST-ON-WAN-BACKUP"
                }
            }
    } 
    # Else just lost the IP address
    else={
        /ip route remove [find comment="HOST-ON-WAN-BACKUP"]
    }
}
I'm not sure how the mikrotik script interpreter works, but will it execute lines UNTIL it finds a syntax error? Or will even a single error in the script cause it to not execute?
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4276
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: First script problem - just won't execute

Sat Nov 30, 2024 12:04 am

That is a script for /ip/dhcp-client. $bound does not exist in /system/script.
 
User avatar
ocgltd
Member Candidate
Member Candidate
Topic Author
Posts: 112
Joined: Sun Sep 02, 2012 12:53 am
Location: Ontario, Canada

Re: First script problem - just won't execute

Sat Nov 30, 2024 12:26 am

I know, but I kept having to renew/drop the interface to test the script. So I just copied into the generic /system/scripts area so I could easily launch it.

I realize some variables may not be populated when it runs from the new location, but I should at least see some log output. I cut and past the first few lines and they ran - but the whole script won't run.

Just trying to figure out what's going on
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4276
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: First script problem - just won't execute

Sat Nov 30, 2024 1:35 am

At least one problem is the comment in the :if - else= is an attribute and you cannot just insert a comment in-between (i.e. the "# Else at least one such route exists")

If you paste in into CLI, it show you where the problem is too. Or, even syntax checking in /system/script/edit which should colorize errors.
 
User avatar
ocgltd
Member Candidate
Member Candidate
Topic Author
Posts: 112
Joined: Sun Sep 02, 2012 12:53 am
Location: Ontario, Canada

Re: First script problem - just won't execute

Sat Nov 30, 2024 4:35 am

That was it. I asked MS Copilot and it said I could split them, but obviously not
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4276
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: First script problem - just won't execute

Sat Nov 30, 2024 5:58 am

It can be inside {} but NOT between attributes, which else={} is actually a property of the :if.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12534
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: First script problem - just won't execute

Sat Nov 30, 2024 11:52 am

Ignoring the Copilot crap, the script is useless, just use "Add Default Route" and choose rigtly "Default Route Distance", and default route distance
and everything works automagically without bothering with scripts.

At that point, when the mechanism (elsewhere) that automatically activates the DHCP-Client, the route with a lower weight is created, so that it is preferred,
and, when the DHCP-Client is deactivated, the emergency route is automatically removed making the default route preferable again.
 
jaclaz
Forum Guru
Forum Guru
Posts: 1919
Joined: Tue Oct 03, 2023 4:21 pm

Re: First script problem - just won't execute

Sat Nov 30, 2024 1:03 pm

Besides it being useful or not, I am failing to understand the logic of the if conditions (i am just trying to understand).

The base condition is whether $bound is 1 or not:
a. if $bound is not 1, then remove ALL routes with that comment
b. if $bound is 1 then:
b.1 if there is not a route with that comment, create one
b.2 if there is a single route with that comment, update it <- this - in my perverted mind - is the same as deleting it and creating a new one
b.3 if there are more than one route with that comment, do nothing <- what is the use of this doing nothing?

Woundn't the same (or even better) results be obtained by:

a. remove ALL routes with that comment
b. if $bound is 1 then:
b.1 add a new route with that comment
? :?:
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12534
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: First script problem - just won't execute

Sat Nov 30, 2024 1:51 pm

b.2 EXACT, if is wrong...

But the rute must not be uselessly removed.
The route (ALL THE ROUTES created with DHCP) must be removed from netwatch script that activate/deactivate DHCP-Client both on up and on down.
else connections are destroyed every time the lease are renewed.

and if is =1
delete only all wrong routes (probably everytime none present because removed from netwatch script that activate/deactivate DHCP)
if correct route number > 0, just ignore, useless remove all than one (or worst dellete all and add one), (probably at start none etc. etc. as up)
if correct route number < 0 add one


Another soluction is simply leave DHCP active with route weight > pppoe-client route weight (if is used ppppoe-client, etc. etc.),
and if pppoe-client go down, all is automatic without change anything or use any script.
 
User avatar
ocgltd
Member Candidate
Member Candidate
Topic Author
Posts: 112
Joined: Sun Sep 02, 2012 12:53 am
Location: Ontario, Canada

Re: First script problem - just won't execute

Sat Nov 30, 2024 3:10 pm

Wow lots of ways to do this.

I used a script from the wiki thinking that was "the right way".

I think I will tinker a bit to explore the ideas...
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4276
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: First script problem - just won't execute

Sat Nov 30, 2024 3:16 pm

Wow lots of ways to do this.
I was answering the scripting question, since I figure your trying to learn scripting.

But I too was not sure what you're trying to do.... If you have a "backup WAN"... typically you'd put a script on the "primary WAN" DHCP to change the default route to add a check-gateway=ping, and on the "backup WAN" you just set the default-route-distance=2.

Who is online

Users browsing this forum: No registered users and 9 guests