The basics of reading and writing files in RouterOS

My good friend Greg was asking about how to store data to files onboard a MikroTik device so I thought I’d elaborate here with some information and examples.

Before we get started, some things to note:

  1. While you can fetch and read the contents of any file, you are limited to working with 4096 character files as this is a limitation on the amount of information that can be contained in a string variable in RouterOS at this time.
  2. When creating new files in RouterOS via terminal the extension .txt will be appended to anything that doesn’t already have .txt at the end.
  3. You can work with newlines \n\r as delimeters (which is super helpful when downloading something list of IP addresses from somewhere)

The basic commands for working with a file, using variables in place of static content or file names:
1. To create a new file

/file print file=$filename

2. To read an existing file

:set $filedata [/file get $filename contents]

3. To write to an existing file

/file set $filename contents=$newdata

4. To append to an existing file

/file set $filename contents=([get $filename contents] . $newdata)

Scriptlet: Halt MikroTik scheduled scripts if multiple instances are detected.

The following script can be run in terminal (or via any automation tool that can login to your MikroTik devices via SSH) and checks for any duplicate script ‘jobs’ and kills them.

I wrote this after noticing a few of my scripts that use fetch would hang periodically and leave multiple jobs open.

#kill duplicate script jobs
:global counter
:global counter2
:foreach counter in=[/system script job find] do={
:global job [/system script job get $counter script]
:if ([:len [/system script job find where script=$"job"]] > 0 && [:len $job] > 0) do={
:put "Duplicate script running: $job - terminating all"
:foreach counter2 in=[/system script job find where script=$"job"] do={
/system script job remove $counter2
}
}
}

Continue reading Scriptlet: Halt MikroTik scheduled scripts if multiple instances are detected.

TheBrothersWISP – Monitoring With NetXMS

Greg, Miller, Jeremy Austin, a little bit of Mike, Cox, and Tomas talk about monitoring and demo just a little of what NetXMS can do. We talk about what/how we monitoring now, and how we all wish we had a Tomas to build a system for us.

Topics include:

  • NetXMS
  • Tomas’ NetXMS instructional videos
  • What is syslog
  • What is(are) SNMP/SNMP Traps
  • Trending
  • Alerting
  • Observium
  • Cacti
  • Site24x7.com
  • Alertra
  • Here’s the video:(if you don’t see it, hit refresh)

    TheBrothersWISP 27 – US MUM 2016, Switches, Upcoming Events

    We are back peeps! Greg, Mike, Justin Miller, Justin Wilson, Alex, Tomas, and Tom chat about a few important things, and a lot of everything else. We gave a shout out to a few new people we’ve met, but please forgive me for forgetting a few(looking at you forestry service guys and Costa Ricans).

    Topics include:

  • US MUM Hardware
  • US MUM Presentations
  • US MUM Announcements
  • Switches(CRS, EdgeSwitch, UnifiSwitch, etc)
  • Ubiquiti AmpliFi
  • Security FlowSpec
  • Unimus – backup system – http://unimus.net/
  • StrayaNet – MDU ISP – http://StrayaNet.com
  • Here’s the video:(if you don’t see it, hit refresh)