From 91a254cdec44109e4adf5ac8f201277126bd7cc4 Mon Sep 17 00:00:00 2001 From: shokinn Date: Mon, 27 Jan 2025 21:27:59 +0100 Subject: [PATCH] Add global functions --- README.md | 54 ++++++++++++++++++++++++++------- global-functions-custom-phg.rsc | 39 ++++++++++++++++++++++++ template.rsc.tpl | 33 ++++++++++++++++++++ 3 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 global-functions-custom-phg.rsc create mode 100644 template.rsc.tpl diff --git a/README.md b/README.md index 2149e7f..5967a89 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -Custom RouterOS Scripts -======================= +# Custom RouterOS Scripts [RouterOS](https://mikrotik.com/software) is the operating system developed by [MikroTik](https://mikrotik.com/aboutus) for networking tasks. This @@ -9,21 +8,55 @@ to manage RouterOS devices or extend their functionality. *Use at your own risk*, pay attention to [license and warranty](#license-and-warranty)! -Requirements ------------- +## Table of Contents + +- [Custom RouterOS Scripts](#custom-routeros-scripts) + - [Table of Contents](#table-of-contents) + - [Requirements](#requirements) + - [Installation](#installation) + - [Initial Setup](#initial-setup) + - [Adding a script](#adding-a-script) + - [Available scripts](#available-scripts) + - [License and warranty](#license-and-warranty) + - [Upstream](#upstream) + +## Requirements This is a repository containing **custom** RouterOS scripts. These do depend on upstream project. Visit [RouterOS-Scripts](https://git.eworm.de/cgit/routeros-scripts/about/) and follow the instructions there for the basic installation and setup. -Available scripts ------------------ +## Installation -* [Hello World](doc/hello-world.md) +### Initial Setup -License and warranty --------------------- +Download the `global-functions-custom-phg.rsc` script: + +```rsc +$ScriptInstallUpdate global-functions-custom-phg "base-url=https://git.s1q.dev/phg/routeros-scripts-custom/raw/branch/main/"; +``` + +And finally load my custom functions and add a scheduler to load them on each startup. + +```rsc +/system/script/run global-functions-custom-phg; +/system/scheduler/add name="global-scripts-custom-phg" start-time=startup on-event="/system/script/run global-functions-custom-phg;"; +``` + +### Adding a script + +To add a script from the repository run function `$ScriptInstallUpdate` with a comma separated list of script names, as well as the parameter `"base-url=https://git.s1q.dev/phg/routeros-scripts-custom/raw/branch/main/"`. + +```rsc +$ScriptInstallUpdate ddns-hetzner,dns-to-ipv6-subnet-resolver "base-url=https://git.s1q.dev/phg/routeros-scripts-custom/raw/branch/main/"; +``` + +## Available scripts + +- [Hello World](doc/hello-world.md) + +## License and warranty This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -35,8 +68,7 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the [GNU General Public License](COPYING.md) for more details. -Upstream --------- +## Upstream URL: [git.s1q.dev](https://git.s1q.dev/phg/routeros-scripts-custom) diff --git a/global-functions-custom-phg.rsc b/global-functions-custom-phg.rsc new file mode 100644 index 0000000..fd994f4 --- /dev/null +++ b/global-functions-custom-phg.rsc @@ -0,0 +1,39 @@ +#!rsc by RouterOS +# RouterOS script: global-functions-custom-phg +# Copyright (c) 2025 Philip 'ShokiNN' Henning +# https://git.s1q.dev/phg/routeros-scripts-custom/about/COPYING.md +# +# requires RouterOS, version=7.14 +# +# global functions for my custom scripts +# https://git.s1q.dev/phg/routeros-scripts-custom/about + +:local ScriptName [ :jobname ]; + +# global variables not to be changed by user +:global GlobalFunctionsCustomPhgReady false; + +# global functions +:global SafelyResolve + +# Function: safelyResolve +# - Takes a DNS string (e.g. "example.com") +# - Takes an IP type [ipv4, ipv6] +# - Returns a string of and IP address or false if it can't be resolved +:set SafelyResolve do={ + :do { + :local DomainName [ :tostr $1 ]; + :if ( [ :tostr $2 ] = "ipv4" or [ :tostr $2 ] = "ipv6" ) do={ + :local IPType [ :tostr $2 ]; + } else={ + :local IPType "ipv4"; + } + :local IP [:resolve domain-name="$DomainName" type=$IPType]; + :return "$IP"; + } on-error={ + return false; + } +} + +# signal we are ready +:set GlobalFunctionsCustomPhgReady true; \ No newline at end of file diff --git a/template.rsc.tpl b/template.rsc.tpl new file mode 100644 index 0000000..b596501 --- /dev/null +++ b/template.rsc.tpl @@ -0,0 +1,33 @@ +#!rsc by RouterOS +# RouterOS script: +# Copyright (c) Philip 'ShokiNN' Henning +# https://git.s1q.dev/phg/routeros-scripts-custom/about/COPYING.md +# +# requires RouterOS, version= +# +# +# https://git.s1q.dev/phg/routeros-scripts-custom/about/doc/.md + +:global GlobalFunctionsReady; +:while ($GlobalFunctionsReady != true) do={ :delay 500ms; } + +:local ExitOK false; +:do { + :local ScriptName [ :jobname ]; + + :global LogPrint; + :global ParseKeyValueStore; + :global ScriptLock; + + # Local/global script specific variables + + :if ([ $ScriptLock $ScriptName ] = false) do={ + :set ExitOK true; + :error false; + } + + # Add Script from here: + +} on-error={ + :global ExitError; $ExitError $ExitOK [ :jobname ]; +} \ No newline at end of file