Community discussions

MikroTik App
 
Josephny
Long time Member
Long time Member
Topic Author
Posts: 665
Joined: Tue Sep 20, 2022 12:11 am

Scripting skills

Tue Oct 22, 2024 3:49 pm

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\"\
        }
        }
    }

 
User avatar
anav
Forum Guru
Forum Guru
Posts: 21226
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Scripting skills

Tue Oct 22, 2024 4:40 pm

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

Want a ---->
cookie.jpg
??
You do not have the required permissions to view the files attached to this post.
 
Josephny
Long time Member
Long time Member
Topic Author
Posts: 665
Joined: Tue Sep 20, 2022 12:11 am

Re: Scripting skills

Tue Oct 22, 2024 4:45 pm

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

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

Yes, please.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 21226
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Scripting skills

Tue Oct 22, 2024 5:00 pm

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......
 
Josephny
Long time Member
Long time Member
Topic Author
Posts: 665
Joined: Tue Sep 20, 2022 12:11 am

Re: Scripting skills

Tue Oct 22, 2024 5:05 pm

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.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 21226
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Scripting skills

Tue Oct 22, 2024 5:56 pm

My skills are actually limited but my ability to pester those far more knowledgeable are second to none!
 
holvoetn
Forum Guru
Forum Guru
Posts: 6273
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: Scripting skills

Tue Oct 22, 2024 7:24 pm

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 ...
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4089
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Scripting skills

Wed Oct 23, 2024 2:52 pm

ability to pester those far more knowledgeable
One big trick, I think, is using "/system/script/edit <scriptname> 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
everyting-is-okay.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:
bad-syntax-in-editor.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.
You do not have the required permissions to view the files attached to this post.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12438
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Scripting skills

Wed Oct 23, 2024 8:01 pm

While I like @rextended, I know he uses notepad.exe to edit script..
Not true, I use Macromedia HomeSite 5.5+ for almost everything...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12438
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Scripting skills

Wed Oct 23, 2024 8:05 pm

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...

nicely indexed code

# 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"
        }
    }
}
 
Josephny
Long time Member
Long time Member
Topic Author
Posts: 665
Joined: Tue Sep 20, 2022 12:11 am

Re: Scripting skills

Thu Oct 24, 2024 1:56 am

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...

nicely indexed code

# 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"
            }
       }
}
 
Josephny
Long time Member
Long time Member
Topic Author
Posts: 665
Joined: Tue Sep 20, 2022 12:11 am

Re: Scripting skills

Thu Oct 24, 2024 4:26 pm

ability to pester those far more knowledgeable
One big trick, I think, is using "/system/script/edit <scriptname> 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
everyting-is-okay.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:
bad-syntax-in-editor.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.
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?
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4089
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Scripting skills

Thu Oct 24, 2024 6:54 pm

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 ;).
... /system/script/edit ...
This is a fantastic feature. Which, no surprise, I had no idea existed.

Is this what you use to do your scripting?
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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12438
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Scripting skills

Thu Oct 24, 2024 10:17 pm

You could have avoided many questions by comparing what I wrote with what you wrote.
What is wrong with:
:local version ([/system resource get version])
Easy: useless parenthesis ( ) that cause other useless calcs...

And, I cannot get this line to work:
 :local newline  [:find \$value \"\\\"]
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.

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.
You use winbox for edit the script? You must use "notepad" or similar proportional spacing editor, like notepad++ and others.
And NEVER use <TAB> on RouterOS scripts...
Last edited by rextended on Thu Oct 24, 2024 10:26 pm, edited 1 time in total.
 
optio
Forum Veteran
Forum Veteran
Posts: 871
Joined: Mon Dec 26, 2022 2:57 pm

Re: Scripting skills

Thu Oct 24, 2024 10:25 pm

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.
 
Josephny
Long time Member
Long time Member
Topic Author
Posts: 665
Joined: Tue Sep 20, 2022 12:11 am

Re: Scripting skills

Fri Oct 25, 2024 12:46 am

You could have avoided many questions by comparing what I wrote with what you wrote.
What is wrong with:
:local version ([/system resource get version])
Easy: useless parenthesis ( ) that cause other useless calcs...

And, I cannot get this line to work:
 :local newline  [:find \$value \"\\\"]
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.

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.
You use winbox for edit the script? You must use "notepad" or similar proportional spacing editor, like notepad++ and others.
And NEVER use <TAB> on RouterOS scripts...
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.

Who is online

Users browsing this forum: No registered users and 6 guests