Hi,
I’m trying to extract the current link rate via Rest API.
An URL like https:///rest/interface/ethernet/*1 won’t work because the result only contains the attribute “speed” which is not the current rate but the default rate if auto negotiation fails.
Within the CLI the current rate can only obtained via
/interface/ethernet/monitor 1
and not via a print command as far as I know (which would probably be easy to translate to a Rest API call).
Is there any way to get the link rate via Rest API?
Additional Info: I tried this on RouterOS 7.8 with a CRS309-1G-8S+
You need to use the POST method for “monitor”, since it isn’t a basic RouterOS “get” (HTTP GET) or “set” (HTTP PUT) command.
Using curl as example, this will return an array of those values. If you change the duration or interface, that will control how many items in returned array and which ones.
Of course, replacing your username and password in the “-u”, and your router name/IP in the URL, too. Be same URL and JSON (in --data) if use another tool/language, same details: “POST”, “Basic Authorization”, “content-type” as above should be all that’s needed if cert if valid. If you’re using self-signed certifications, the root cert of it may have to be added to the calling desktop/server.
Sorry you were asking about /internet/ethernet/monitor, not the top level one. That does take an .id (e.g. “numbers”). Replace the URL and --data in above for that:
Install the jo and jq packages as that helps with the JSON. “jo” parse RouterOS-style variables into JSON. And “jq” is useful on the results to pull out any data need. On MacOS with brew, it’s “brew install jo jq”, and most linux/WSL something like “apt install jo jq”. Check the manage (or Google), lots of examples on using them and helpful with “curl”.
There is a “–json” option in the most recent curl that avoid the need for the -H “content-type”… stuff
So the above can be shorten to using the above with --json, the “jo” tool (e.g. backtick ` runs a command within a command), and a couple shell variable makes curl way less painful…
“jq” is more complex topic, but if want to use some data from RouterOS, it makes quick work of pulling out a value for the shell from the JSON return. See https://stedolan.github.io/jq/
Here are some examples of jq. Just pipping the output get you a “pretty” view of the JSON returns, and it support more complex queries but some simple ones below: