Trying to stitch a little script together

Hello guys.
I know some basic scripting in my debian based system and general linux commands, but i am absolutely ignorant with any Mikrotik scripting.

I have a hAP AC 2, with router OS 7.17.2.
I am behind a CGNAT with my ISP, so I am not able to use the built in Mikrotik’s cloud service with the xxx.sn.mynetname.net domain.

Here is a description of what i would like to do:

I need to create a script on my Mikrotik router that checks a URL (e.g., mydomain.com/textfile.txt), reads the content of the text file, and uses an IF statement to perform one of two actions based on the word in the file. The file contains just one word, and I can edit it on my web server. If the word matches a certain condition (true), the script will send a Wake-on-LAN (WoL) command to a specific MAC address in my network. The goal is to wake up a small server that I only need occasionally, rather than keeping it on all the time.

So far i was able to find a command that downloads the TXT file to the mikrotik’s file system.

/tool fetch url="https://domain.com/wol.txt" mode=https http-method=get keep-result=yes

But then i wasted an hour trying to make the router read the contents of the file, for some reason it prints just a blank line, i am stuck there.
I downloaded the TXT file from the mikrotik to my computer, and the file has the word in there, the command is getting the right file with the contents.
This is what i tried so far:

:local content [/file get wol.txt contents];
:put $content



:local content [/file get wol.txt];
:put $content

So this is supposed to put the contents of the text file into a variable and then print the variable contents in the terminal? It just prints a blank line.

I would rather make the mikrotik read the TXT file straight off the web server and put it in RAM instead of downloading a text file every 5 minutes to the router’s file system to avoid stressing the flash memory.

Thank you for any tips.

What happens with

:global content [/file get wol.txt];
:put $content

local on command line needs to have the whole stuff enclosed in curly brackets.

{
:local content [/file get wol.txt contents];
:put $content
}

should do.

This said, if you control the web server you could probably have it send a different http response code , and make use of netwatch (http-status-code or abusing of http-code-min and http-code-max):
https://help.mikrotik.com/docs/spaces/ROS/pages/8323208/Netwatch

Thank you jaclaz for your help.
Yes, changing the commands with the ones you suggested makes the terminal to spit the output, although is not only the single word in the text file but a bunch of other information. Anyways, let’s scrap that.. i got the script working :smiley:

Here is my script :

:local response [/tool fetch url="https://MyDomain.com/wol.txt" mode=https as-value output=user];
:local content ($response->"data");

:if ($content="wake") do={
    /tool wol interface=bridge mac=xx:xx:xx:xx:xx:xx
    :log info "Waking up the server!"
} else={
    :log info "No wake command found."
}

Now i have to setup the scheduler to run it.
I had an issue where the machine wakes up, then i shut it down immediately and the script does not work anymore. But i think is the NIC freezing up or something. Unplugging the ethernet wire and plugin in back on makes the script work again.
Thank you.

Maybe also turning off/on (disabling and re-enabling) the ethernet port on the Mikrotik and might do to “reset” the NIC.