Help with script?

Hello guys !
Im using latest MT v3.1 on x86 … i try to make script which will be work like backup route in case that my ADSL connection is down.
This is script … but it is not work … so please can somebody help me?

:local pppoe1-status ([/interface pppoe-client get [/interface find comment="ADSL"] running])
:if ($pppoe1-status = false) do {/ip route enable [/ip route find comment="myRoute"])
:if ($pppoe1-status = true) do {/ip route disable [/ip route find comment="myRoute"])
:if ($pppoe1-status = "false") do={/ip route enable [/ip route find comment="myRoute"])
:if ($pppoe1-status = "true") do={/ip route disable [/ip route find comment="myRoute"])

Hm I was for testin disable my ADSL interface and run script manualy and nothing is hapening … MyRoute is still disabled …
Does anybody have some other idea?
I dont see errors in my log …

Hey bax,

microtiks are very fussy about spacing and the way you type stuff into the script.

:local pppoe1-status ([/interface pppoe-client get [/interface find comment="ADSL"] running])
:if ($pppoe1-status=false) do {/ip route enable [/ip route find comment="myRoute"]}
:if ($pppoe1-status=true) do {/ip route disable [/ip route find comment="myRoute"]}

What I do usually for the script because there is no compiler I take each line and paste it into a test router I have on my desk and check each line indivitually so I can narrow the line with the error.

check here http://wiki.mikrotik.com/wiki/Scripts
and here http://www.mikrotik.com/testdocs/ros/2.9/system/scripting.php

Try it now with removing the spaces in the if check condition.


Sotiris

Hm I just try this also … without succes … Is there any way to turn on logging ? I dont see anything in log …

{
:local pppoe1-status ([/interface pppoe-client get [/interface find comment="ADSL"] running])
:put "Status=$pppoe1-status";
:put ($pppoe1-status=false);
}

Paste this code in console and look for output.

{… :local pppoe1-status ([/interface pppoe-client get [/interface find comment=“ADSL”] run
ning])
expected end of command (line 2 column 14)
:put “Status=$pppoe1-status”;
expected end of command (line 1 column 14)
:put ($pppoe1-status=false);
syntax error (line 1 column :sunglasses:

Hm here is output … some syntax errors

oh.. completely forgot.. If you have variables that contain characters that could be interpreted as operators then put variable in quotes.

pppoe1-status should be written as “pppoe1-status” or change the variable name without minus sign.

Ok here is the change :

{ :local "pppoe1-status" ([/interface pppoe-client get [/interface find comment="ADSL"] running]) :put "Status=$pppoe1-status"; :put ($pppoe1-status=false); }

And here is output:
expected end of command (line 1 column 99)

put variable in quotes in every place where it appears not just when defining it :slight_smile:)

But I would suggest to change the variable name. It will be a lot easier for you.

Hm… I was really dont understand this scripting language … can you give me example ?
Please :smiley:

:local "pppoe-status" "lalala";
:if ($"pppoe1-status" = "lalala") do={ :put $"pppoe1-status"; }

or

:local pppoeStatus "lalala";
:if ($pppoeStatus = "lalala") do={ :put $pppoeStatus; }

it is quite simple.
You can also read scripting manual:
http://wiki.mikrotik.com/wiki/Scripting

OK here is changes:

:local pppoeStatus ([/interface pppoe-client get [/interface find comment="ADSL"] running]);
:if ($pppoeStatus=false) do={/ip route enable [/ip route find comment="myRoute"]);
:if ($pppoeStatus=true) do={/ip route disable [/ip route find comment="myRoute"]);

But when i put this first part in terminal I have no errors , so I think that this is OK
:local pppoeStatus ([/interface pppoe-client get [/interface find comment=“ADSL”] running]);
But when i put second line in terminal:

:if ($pppoeStatus=false) do={/ip route enable [/ip route find comment="myRoute"]);

I have error : syntax error (line 1 column 7)

Of course it will give you syntax error, because when you paste directly in console each line is like separate script

and :local variable is accessible only within current scope ( in this case within that one line ).

Hm… you are right, but now my script look OK … but still is not work … when I disable my ADSL interface and run script it will not enable my backup route …
Do you have some idea what is wrong? or somebody other?

:local pppoeStatus ([/interface pppoe-client get [/interface find comment="ADSL"] running]);
:if ($pppoeStatus=false) do={/ip route enable [/ip route find comment="myRoute"]);
:if ($pppoeStatus=true) do={/ip route disable [/ip route find comment="myRoute"]);

Hi Bax

I have kind of got it working.

{
:local pppoe ([/interface pppoe-client get [/interface find comment=“adsl”] running]);
:if ($pppoe=false) do={ :put [/ip route enable [/ip route find comment=“myRoute”]};
:if ($pppoe=true) do={ :put [/ip route disable [/ip route find comment=“myRoute”]};
}

I have no problems running the script from the terminal window but the script does not run once i add it to /system scripts.
Any Ideas ?

I have just tested the script on version 3.15 and it works but still have a issue with v3.18

Take the :put out. That only works on a terminal. And I think you have running in the wrong place.

:local pppoe [/interface pppoe-client get [/interface find comment="adsl" running]]

:if ([:len $pppoe] = 0) do={
   [/ip route enable [/ip route find comment="myRoute"]]
} else={
   [/ip route disable [/ip route find comment="myRoute"]]
}

ADD: I am not certain that first line will work. What is returned with the ‘find’ parameter is an array of unique “pointers” to the data. I don’t think that pointer will be in the “/ip pppoe-client” section also. I would change the variable pppoe to global so you can see what it is returning. It may not be what you expect. If all you need to check is the interface up and running, then:

:global pppoe [/interface find comment="adsl" running]

:if ([:len $pppoe] = 0) do={
   [/ip route enable [/ip route find comment="myRoute"]]
} else={
   [/ip route disable [/ip route find comment="myRoute"]]
}

If you need to check the pppoe status instead, then change the first line to:
:global pppoe [/interface pppoe-client find name=“adsl” running]

Or maybe this:
:global adsl [/interface get [/interface find comment=“adsl” running] name]
:global pppoe [/interface pppoe-client find interface=$adsl running]
Both must be running.

Hi SurferTim

I was going to use the script to change my pppoe accounts once they where capped
As i am very new to scripting this is what i have come up with so far

:local pppoe ([/interface pppoe-client get [/interface find comment="adsl"] running])
:if ($pppoe = false) do={:put [/interface pppoe-client set adsl user=xx password=xx]}  
:delay 60s;
:if ($pppoe = false) do={ :put [/interface pppoe-client set adsl user=xx password=xx]} 
:delay 60s;
:if ($pppoe = false) do={ :put [/interface pppoe-client set adsl user=xx password=xx]} 
:delay 60s;
:if ($pppoe = false) do={ :put [/interface pppoe-client set adsl user=xx password=xx]} 
:delay 60s;
:if ($pppoe = false) do={ :put [/tool e-mail send to=xx subject="ADSL IS NOT CONNECTED"]}

The script detectors that the interface is not running and changes the username and password but then even if the account is running is will change the username and password to the next one.

Any ideas ?

Your first line is the problem. You cannot cross-link these pointers. It won’t work like that. If you use find and get, they MUST BE in the same place.
This is best. Includes error checking on the interfaces:

:global adsl [/interface get [/interface find comment=adsl running] name]

:if ([:len $adsl] > 0) do={
   :global pppoe [/interface pppoe-client find interface=$adsl running]
} else={
   :global pppoe ""
}

:if ([:len $pppoe] > 0) do={
   :log info "interface running"
} else={
   :log info "interface down"
}

Notice how the get and find in the first line are preceded by the same location “/interface”. You cannot use ‘find’ in one directory and then use ‘get’ in another. Use them both in the same place, then use the result of the ‘get’ in that location to ‘find’ the interface in ‘/interface pppoe-client’.

If you want to recheck the status of the connections, the routines must be run from the start, including the data assignments, or it will use the data you got from the first ‘find’.

I did not verify the rest of the code. I just used the log entries to show where the pass and fail routines should be put. And you shouldn’t use ‘:put’ in your script unless testing. It doesn’t work unless the script is run in a shell.

ADD: If you want to run this every minute, which seems to be your goal, use the /system scheduler to run the script every minute.