Community discussions

MikroTik App
 
GiannisD
just joined
Topic Author
Posts: 4
Joined: Sat Sep 30, 2023 7:00 pm

How can i use rest api to run a script?

Fri Mar 08, 2024 8:22 pm

routers ip is 192.168.20.1
i followed the documentation about rest api I have also watched the video where he sets up the certificate and firewall rule.
I made a user just for using the api i gave it permisions like read api rest api and web
I tested with this curl payload and it worked for me
curl -k -u admin:pass https://192.168.20.1/rest/system/resource
The script i want to run is a wake on lan script for my computer and i would like to run it from my home assistant server so i can power on my laptop from it

Now before you say that this is posible from home assistant yes it is but since im running it on a docker container on a bridged network (tried changing to host but it didnt work).I also tried to use a custom integration that currently doesnt work with 7.13

The docker container is running on a raspberry pi 4 and not on the router it self
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3510
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How can i use rest api to run a script?

Fri Mar 08, 2024 9:42 pm

You may need the Content-Type set. Something like this:

USER=admin:pass ROUTER=192.168.20.1
curl -k -u $USER -X GET -H "Content-Type: application/json" https://$ROUTER/rest/system/resource

You do need to have certificate on /ip/service for HTTPS. You can use Let's Encrypt to do this. Or, REST API works on HTTP although less ideal since password goes in clear – but might be helpful for testing if certificate issue.

The "-k" in `curl` says to ignore any certificate checks, but RouterOS HTTPS still need to have some certificate assigned to work. If you used self-signed cert, and copied the public key of the root CA from RouterOS to homeassisant linux (or just used Let's Encrypt), you could remove the -k. But only once it was already working first ;). And, "-X GET" is curl's default, so could be excluded.

I'm not sure what you're trying to do with WOL. But you can also use .../tool/wol mac=XX:DD:CC:XX:XX:XX via REST API... so something like this should work. Note the -X POST:
USER='admin:pass' 
ROUTER=192.168.20.1
curl -k -u $USER -X POST -H "Content-Type: application/json" https://$ROUTER/rest/tool/wol --data '{"mac": "AA:AA:AA:AA:AA:AA"}'
 
GiannisD
just joined
Topic Author
Posts: 4
Joined: Sat Sep 30, 2023 7:00 pm

Re: How can i use rest api to run a script?

Sat Mar 09, 2024 1:38 am

I mostly wanted to run this as a script since the script works and i dont need to modify the api command i just need this for a single host. Anyways i tested your script i did it like this
curl -k -u admin:pass -X POST -H "Content-Type: application/json" https://192.168.20.1/rest/tool/wol --data '{"mac": "3C:7C:3F:XX:XX:XX"}' and it didnt show me any errors only thing is that i want to have a specified interface because it doesnt work with the default broadcast address i dont think it finds it. I tried the same on my script and without an interface it couldnt wake my pc but when i specified ether4 (sometimes even lan can work) it worked.

update just tried adding the interface and it worked here is the command
curl -k -u admin:pass -X POST -H "Content-Type: application/json" https://192.168.20.1/rest/tool/wol --data '{"mac": "3C:7C:3F:XX:XX:XX","interface":"ether4"}'
One last thing what permisions does wake on lan need I want to give some permisions to a user that will be used for this api call

Also if you can, can you provide the structure of running scripts?
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3510
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How can i use rest api to run a script?

Sat Mar 09, 2024 3:22 am

Yeah you may need (or at least should) specify the interface for WOL. I don't know the internals of WOL, but certainly if you know the interface, it be better to specify interface=: it might flood all interface, or use ARP cache without the interface.

For user group permissions, I believe just "rest-api", "read" and "test" are needed. Perhaps "write", but shouldn't need that. Some V7 versions use "api" instead of "rest-api" which came in some recent V7.

But the core problem here for curl is the following is what critical for all REST calls:
-H "Content-Type: application/json"

FWIW, in newer version of curl there is the --json option which does this for you:
curl -k -u admin:pass -X POST https://192.168.20.1/rest/tool/wol --json '{"mac": "3C:7C:3F:XX:XX:XX","interface":"ether4"}'
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3510
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How can i use rest api to run a script?

Sat Mar 09, 2024 3:39 am

Also if you can, can you provide the structure of running scripts?
You use multiple `curl` commands is easiest. The REST does not directly let you run "a script" – each REST operation is one command. So in a bash, you can just set some environment variables (e.g. ROUTER= USER=.

Theoretically, you could "-X PUT" to upload a long RouterOSscript using `{ "name": "myrestscript", "source": "..." }` to https://$ROUTER/rest/system/script, the "-X POST" to run it using .../system/script/run. Or, use
curl -u admin:pass -k -X POST https://$ROUTER/rest/execute --json '{"script": "/log/info test"}' 

Issue is escaping between your shell and the RouterOS.... curl allows a file to be used with the JSON using "@" (see curl man page). But since any RouterOS script with " quotes have escaped. You can create the script once on the router, the use -X GET to return the JSON for the script – that would contain all the needed escaping for example. Get confusing quickly.

If you're using `curl`... the more UNIX thing to do is env variables with the ROUTER and USER (or add'l ROUTERURL="https://$ROUTER/rest/" to save more typing per command). Then add multiple calls to `curl` in a #!/bin/sh script file.

`jq` is common Linux too that helps with getting data out of JSON at the bourne/bash/zsh shell returned from `curl`. See this post for example: viewtopic.php?t=194105&hilit=jq#p987988

Who is online

Users browsing this forum: No registered users and 11 guests