Noob script question

Greetings - I’m just getting going with RouterOS scripting and I’m challenged to get the syntax right to run a script from a file. I’ve uploaded the script via ftp as test.txt, but can’t figure out how to specify the file as the source of the script.

[admin@MikroTik] /system script> /system script add name=test-script policy=read,test source=test.txt
syntax error (line 1 column 60)

I’ve put in an hour of googling and forum surfing and don’t get it. Can someone help a noob out? Thanks in advance.

I don’t think that You can run script from file…

There are two possible way to do that:

  1. script file name must end with ‘.auto.rsc’, eg. ‘blah.auto.rsc’ and will be executed automatically after upload.
  2. upload your script file (with extension ‘.rsc’) then execute ‘import file-name=your_script.rsc’ command.

HTH,

First I always check if the script will run on the version of ROS i am using, to test the script for syntax errors you can in terminal put { at the start and } at the end of the script also you can test from terminal /system script use print command and it will show any syntax errors, a least then after the tests and any corrections to syntax errors you will have a working script. hope this will be of some help to you.

Hi. I do not understand – you have this file test.rsc in the / directory. Now I do not understand what you mean by in terminal putting { before and } after the script and why it would not execute the script?

Any way to import the script into the system script interface, so I could schedule it or whatever?

Thanks.

As ditonet indicated use:

/import file-name="thescript.rsc"

.

So, to repeat.

  1. Name your file as a .rsc file such as thescript.rsc".
  2. Upload file
  3. From terminal run "/import file-name=“thescript.rsc”
  4. This will then essentially “run” whatever code you had in the file.

If you wanted the uploaded file to ‘create a script’ for you that you can run again later the .rsc file itself must create the required script. Here’s an example:

/system script
# Remove old script with same name if it exists
:if ([:len [/file find name="TestScript"]] > 0) do={remove ([/system script find name~"TestScript"]);}

#Add new script
add name=TestScript policy=ftp,reboot,read,write,test,winbox,password,sensitive source="\
# Everything from this point is part of the script called TestScript

# log a warning as example
/log warning \"When this script is called it will log a warning in the system log\"

# End of script
"

Now, if you wanted the above to automatically execute when you ftp it - simply name it thescript.auto.rsc.

Once uploaded the file will then automatically execute and create a script called “TestScript” which will then be available for you to run on the router.

Hope this helps?

Thanks much – just one more question – what did the previous poster mean to say that in terminal you could put { and } around the script to prevent execution – I do not understand this.

Thanks agian.