Scripting skills

I am at a baby level at scripting and progressing extremely slowly, but I wrote (okay, adapted slightly from @jotne’s work) a script to log some basic device info.

I know you experts will cringe at a simpleness, but I’m proud of myself.

 # Collect system resource
    /system resource
    :local cpuload [get cpu-load]
    :local freemem ([get free-memory]/1048576)
    :local totmem ([get total-memory]/1048576)
    :local freehddspace ([get free-hdd-space]/1048576)
    :local totalhddspace ([get total-hdd-space]/1048576)
    :local up [get uptime]
    :local sector [get write-sect-total]
    :log info message=\"free_memory=\$freemem MB total_memory=\$totmem MB free_hdd_space=\$freehddspace MB total_hdd_space=\$totalhddspace MB cpu_load=\$cpuload uptime=\$up write-sect-total=\$sector\"
    
    
 # Collect system information
    :local model na
    :local ffirmware na
    :local cfirmware na
    :local ufirmware na
    :local version ([/system resource get version])
    :local board ([/system resource get board-name])
    :local identity ([/system identity get name])
    :do {
    :if (\$board!=\"CHR\" OR \$board!=\"x86\") do={
    /system routerboard
    :set model ([get model])
    :set ffirmware ([get factory-firmware])
    :set cfirmware ([get current-firmware])
    :set ufirmware ([get upgrade-firmware])
    }
    } on-error={}
    :log info message=\"version=\\\"\$version\\\" board-name=\\\"\$board\\\" model=\\\"\$model\\\" identity=\\\"\$identity\\\"\"
    
   
 # Collect IP addresses
    :foreach neighborID in=[/ip address find] do={
    :local nb [/ip address get \$neighborID]
    :local id [:pick (\"\$nb\"->\".id\") 1 99]
    :foreach key,value in=\$nb do={
    :local newline [:find \$value \"\\"]
    :if ([\$newline]>0) do={
    :set value [:pick \$value 0 \$newline]
    }
    :if (\$key~\"add\") do={
    :log info message=\"System IP Address \$value\"\
        }
        }
    }

Why are you posting in the General Forum instead of the Scripting Forum???

Want a ---->
cookie.jpg
??

Apologies.

Yes, please.

Congrats on your scripting journey, seriously! I will say you are braver than I. I have only dabbled in scripting and am mostly content to use functionality as already available, and thus admire anyone that makes the effort. Where I think people are just plain nuts is there love for capsman. I am highly allergic…

Thank you so very much! That is very kind of you.

I happen to know very well that you are an extremely skilled and talented person in the ways of ROS – firewalls, wireguard, routing and much more.

My skills are actually limited but my ability to pester those far more knowledgeable are second to none!

Self-knowledge is the beginning of all wisdom …

One big trick, I think, is using “/system/script/edit source” to use Mikrotik’s editor. Unlike Winbox’s script editor, it will show red marks if the script is invalid (in realtime in edit).
While I like @rextended, I know he uses notepad.exe to edit script, but without color-syntax coloring he’s crazy. Same with winbox’s script widow. The colors do help.

Take my $PIANO script, if the script is “correct”, you’ll see all the colors indicating types
bad-syntax-in-editor.png
Even a light red shows [undeclared] args, which is correct here, but using function args is rare, and undecleared variables are normally bad… why $bpm, $silient, etc are red - but still parses so you see colors everywhere).

But throw a bad command into the mix, all colors gone means it cannot be parsed. And if edit cannot colorize it, the script engine would be able run it either:
everyting-is-okay.png
Now having it all colored does not mean it will “just work”, but at least it won’t be a syntax error that’s the issue.

There are routeros plugins for other code editors like VSCode, etc. too. But I swear the colors really help with these dense RouterOS scripts. But the “/system/script/edit” is actually the most accurate, since it’s using the real script engine. Winbox’s script window does not help you - it more a hope-and-pray it’s right sitution.

Not true, I use Macromedia HomeSite 5.5+ for almost everything…

Is beter you lost instantly bad habit like :local version ([/system resource get version**])** and do not post export like a script because on this way is full of errors.
Copy & paste from winbox to the forum, or posst all the exported script parts.

Also use space indentation, or is hard readable…

And also study differencies between MegaByte and MebiByte…

na??? use quotes on strings “na”

OR not exist, is or

missing \ ???
:local newline [:find $value "****\"]


Syntax corrected, but uncorrected other errors…

Collect system resource

/system resource
:local cpuload [get cpu-load ]
:local freemem ([get free-memory ] / 1048576)
:local totmem ([get total-memory ] / 1048576)
:local freehddspace ([get free-hdd-space ] / 1048576)
:local totalhddspace ([get total-hdd-space ] / 1048576)
:local up [get uptime ]
:local sector [get write-sect-total]

:log info “free_memory=$freemem MB total_memory=$totmem MB free_hdd_space=$freehddspace MB total_hdd_space=$totalhddspace MB cpu_load=$cpuload uptime=$up write-sect-total=$sector”

Collect system information

:local model “na”
:local ffirmware “na”
:local cfirmware “na”
:local ufirmware “na”
:local version [/system resource get version ]
:local board [/system resource get board-name]
:local identity [/system identity get name ]
:do {
:if ($board!=“CHR” or $board!=“x86”) do={
/system routerboard
:set model [get model ]
:set ffirmware [get factory-firmware]
:set cfirmware [get current-firmware]
:set ufirmware [get upgrade-firmware]
}
} on-error={}

:log info message=“version="$version" board-name="$board" model="$model" identity="$identity"”

Collect IP addresses

:foreach neighborID in=[/ip address find] do={
:local nb [/ip address get $neighborID]
:local id [:pick ($nb->“.id”) 1 99]
:foreach key,value in=$nb do={
:local newline [:find $value “\”]
:if ([$newline] > 0) do={
:set value [:pick $value 0 $newline]
}
:if ($key~“add”) do={
:log info message=“System IP Address $value”
}
}
}

Thank you!

What is wrong with:

:local version ([/system resource get version])

And, I cannot get this line to work:

 :local newline  [:find \$value \"\\\"]

I edited the script and made brackets and lines line up uses spaces, but when I copied and pasted into this forum post, the alignment gets messed up again.

Here is the edited script:

# Collect system resource
/system resource
  :local cpuload                  [get cpu-load                                  ]
  :local freemem              ([get free-memory                        ] / 1048576)
  :local totmem                ([get total-memory                       ] / 1048576)
  :local freehddspace  ([get free-hdd-space                  ] / 1048576)
  :local totalhddspace ([get total-hdd-space                ] / 1048576)
  :local up                               [get uptime                                      ]
  :local sector                      [get write-sect-total                 ]

:log info message="free_memory=$freemem MB total_memory=$totmem MB

free_hdd_space=$freehddspace MB total_hdd_space=$totalhddspace MB cpu_load=$cpuload uptime=$up write-sect-total=$sector"

# Collect system information 5.5 added ID for non routerBoard 5.6 Remvoed serial
:local model                    "na"
:local ffirmware             "na"
:local cfirmware            "na"
:local ufirmware            "na"
:local version                 ([/system resource get version])
:local board                    ([/system resource get board-name])
:local identity                ([/system identity get name])
:do {
    :if     ($board!="CHR" OR $board!="x86") do={
              /system routerboard
             :set model            ([get model])
             :set ffirmware    ([get factory-firmware])
             :set cfirmware   ([get current-firmware])
             :set ufirmware   ([get upgrade-firmware])
          }
} on-error={}
:log info message="version=\"$version\" board-name=\"$board\" model=\"$model\"identity=\"$identity\""


# Collect IP addresses
:foreach neighborID in=[/ip address find] do={
       :local nb [/ip address get $neighborID]
       :local id [:pick ("$nb"->".id") 1 99]
       :foreach key,value in=$nb do={
   #        :local newline  [:find \$value \"\\\"]
           :if    ([$newline]>0) do={
                    :set value [:pick $value 0 $newline]
           }
           :if     ($key~"add") do={
                 :log info message="System IP Address $value"
            }
       }
}

This is a fantastic feature. Which, no surprise, I had no idea existed.

Is this what you use to do your scripting?

Other than having achieved a skill level where syntax errors are not made, why use any other tool for scripting?

I’m sure VSS, notepad, Homesite, etc., but without this level of confirmation of syntax correctness, why use them?

Even in Winbox4 they still use a proportional font. I know scripting, and I have a difficult time understanding a script in winbox’s dialog when the code get “compressed” by the font and [default] shorter line width. So I partially blame the font for folks scripting difficulties :wink:.

I ain’t going to get into editor recommendation business - beyond folks should know that /system/script/edit is there for “debugging”. I think some of YouTube video subtly show using it.

Just to answer, I use VSCode, in vi mode. But VSCode is actually not as accurate as RouterOS’s built-in editor - and no external editor will be*. So I do sometimes use “edit” for minor edits and as “linter” for the script. It just lacks a lot of editor features (like mouse support, regex/“search-and-replace”, etc)… but showing what’s wrong in a script the built-in edit is excellent.

*unless Mikrotik add an LSP, see https://langserver.org

You could have avoided many questions by comparing what I wrote with what you wrote.

Easy: useless parenthesis ( ) that cause other useless calcs…


Why you escape all $ " \ etc. with "" ? What way you use for create and edit scripts???
You do export, edit the export and re-import the export for edit the script? Is a mess.


You use winbox for edit the script? You must use “notepad” or similar proportional spacing editor, like notepad++ and others.
And NEVER use on RouterOS scripts…

Regarding VSCode and script error checking, this VSCode task can be useful for debugging but it requires to setup non-interactive (PKI) SSH login to ROS and errors are marked after execution because error position is parsed from script error output after is performed. Not ideal but since there is no LSP for RSC, as @Amm0 mentioned, is better than nothing.

The parenthesis I now understand. I did not know that an extra set of parenthesis around the entire bracketed command would cause problems.

The "" escape characters and the tabs are because I (stupidly) used notepadd++ and did various find/replace and copy/pastes within it and between winbox and notepad. I know better now – thank you.