Community discussions

MikroTik App
 
NatwanTilembar
just joined
Topic Author
Posts: 2
Joined: Fri Oct 07, 2022 9:07 am

RouterOS scripting

Tue Dec 06, 2022 8:13 am

I’m not a developer or admin of any kind, but automating things and writing scripts is to me what 6000-piece puzzles were to my grandfather. I love the tinkering. I’m no expert in any language, and will use whatever seems the most sensible.

For a script that makes sure I’m connected to the best VPN server, I started out in Python because I needed to download and process a JSON file. For the interaction with my RB4011, I currently use ssh commands but may attempt to switch over to using the API.

This got me thinking though: why not write the whole thing in the RouterOS scripting language. Most of what the script does is pretty straight-forward. The only things I’m not sure about are these:

Can I easily download a file from a public (https) server, and store it locally for further processing?

Has anybody ever gone through the trouble of figuring out how to process JSON in this language? Can’t imagine this to be a trivial task.

I’m expecting this isn’t going to work out, but I thought I’d ask to be sure!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: RouterOS scripting

Tue Dec 06, 2022 11:26 am

There is a difference between asking if it can be done (almost certainly: yes) and having someone do it to you... :P
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: RouterOS scripting

Tue Dec 06, 2022 12:09 pm

FWIW, @rextended's footer actually has some really fun and good examples of RouterOS script.

For JSON, I use this code to PARSE JSON from (e.g. response) from a call to /tool/fetch:
https://raw.githubusercontent.com/Winan ... eFunctions
And yeah non-trivial but seems to work.

If you cut-and-paste that to terminal, you should be able to use the $JSONLoads to parse JSON data from the /tool/fetch (what requests HTTPS like "curl" but different). A complex "one-line" that uses it looks like this...
:put "$([$JSONLoads ([/tool/fetch url="https://wttr.in/Riga?format=j1" output=user as-value]->"data")]->"current_condition")"
which prints (:put):
FeelsLikeC=-9;FeelsLikeF=15;cloudcover=100;humidity=93;localObsDateTime=2022-12-06 08:12 AM;observation_time=06:12 AM;precipInches=0.0;precipMM=0.3;pressure=1017;pressureInches=30
;temp_C=-4;temp_F=25;uvIndex=1;visibility=9;visibilityMiles=5;weatherCode=326;weatherDesc=value=Light snow;weatherIconUrl=value=;winddir16Point=SSE;winddirDegree=160;windspeedKmph
=15;windspeedMiles=9

Unwinding that, it uses data from wttr.in (for Riga Lativa), get the "data" from the responds, then calls $JSONLoads from the above JParseFunctions library. After that it uses the JSON data as a Miktotik array type, to access one part of the weather for "current conditions". With the ":put" being same as "print" in most languages. And "as-value" tells most commands, including /tool/fetch, you want to return data to use it variable/code, not print it screen.

As you can see possible, but learning all the [] vs () vs {} vs ; vs , vs $ vs \$ vs "$var" vs "$(var)" vs "$[script]" takes more time.

To generate the JSON (e.g. a request), you just need to learn /tool/fetch specifics params (see help.mirkotik.com), and RouterOS syntax (see Scripting section in help) for escaping since both JSON and RouterOS - essentially both dollar signs (unless your want to substitute a variable) and double quotes needs to be escaped in the http-data of /tool/fetch.

Tinker myself. Now a more fun example, I wrote a "command line remote control" for my RokuTV a while back. So you can switch TV channels (well streaming services, I guess), volume, etc:
viewtopic.php?t=189283

Who is online

Users browsing this forum: No registered users and 24 guests