Community discussions

MikroTik App
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

✂ Rextended Fragments of Snippets

Thu Aug 12, 2021 3:44 pm

Over the years I have published dozens of scripts,
most need to be updated and are hard to find with standard search.
And they also need to be checked if they are still valid for the new stable v7 version.

I'm slowly re-reading all of my 7000 posts
and I'll add here all the useful Snippets I found,
with a description and the link to the topic where they are present.

In the future, when I'm done, I'll index everything for better search.
Last edited by rextended on Tue May 24, 2022 10:52 pm, edited 2 times in total.
Reason: New Snippets
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Wed Sep 15, 2021 10:29 am

wireless on off with mode button
viewtopic.php?f=7&t=115078&p=857648#p857648

create directory and subdirectory
viewtopic.php?f=9&t=151644&p=878316#p878368

mac ping results saved on variable
viewtopic.php?f=2&t=69773&p=876970#p876970

Put snmp-get value on variable
viewtopic.php?f=9&t=136809&p=876969#p876969

Convert character to uppercase or lowercase
viewtopic.php?f=9&t=75555&p=876693#p876693

UNIX time Epoch, day of the week, ISO:8601 date
viewtopic.php?f=9&t=75555#p876568

Import IP or IP prefix from file
viewtopic.php?f=9&t=166293#p872049

How to download only one piece of file at a time with /tool fetch and put it inside a variable
viewtopic.php?f=9&t=177530

Decode USSD hexstr2chrstr hexadecimal string to char string
viewtopic.php?f=9&t=129693&p=871742#p871742

hex2chr hexadecimal to char
viewtopic.php?f=9&t=108127&p=871741#p871741

definitive ip or ip-prefix posix regex
viewtopic.php?f=9&t=152632&p=871284#p871284

auto add (starlink) classless route
viewtopic.php?f=2&t=175272&p=871239#p871239

int2hex num2hex integer number to hexadecimal convert function
viewtopic.php?f=2&t=57665&p=868898#p868898

check host status
viewtopic.php?f=9&t=177005&p=868482#p868482

dual wan A-B-C failover
viewtopic.php?f=2&t=176574&p=865665#p865665

dual dhcp wan failover
viewtopic.php?f=13&t=176956&p=868082#p868082

How to ***really*** block invalid ICMP, TCP, UDP packets and others (ver. 2021)
viewtopic.php?f=9&t=83387&p=867386

comma separated value of interface list
viewtopic.php?f=9&t=176428&p=865545#p865545

num2month mon2MON mon2num number month
viewtopic.php?f=9&t=176408&p=864690#p864688

short address list ip address aggregator (to do)
viewtopic.php?f=23&t=148187&p=864415#p864415

send whatsapp notification
viewtopic.php?f=9&t=124157&p=862628#p860703

asynchronous script execution
viewtopic.php?f=9&t=87589&p=861988#p861988

enable x64 mode on RouterOS x86 installed on x64 (virtual or not) machine
viewtopic.php?f=2&t=116621&p=861102#p861102

random number generator (without OTP frills)
viewtopic.php?f=9&t=175453&p=858629

default firewall rules
viewtopic.php?f=13&t=175129&p=856824#p856824

convert string to ip-prefix
viewtopic.php?f=9&t=127039&p=854216#p854216

dynamic variables
viewtopic.php?f=9&t=178435&p=879152#p879152

manage fetch errors
viewtopic.php?f=2&t=178355&p=878643#p878643

sort array, sort file by date, date2ymd
viewtopic.php?f=9&t=178532#p879931
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Fri Sep 17, 2021 2:42 am

✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Fri Sep 17, 2021 11:50 am

DHCPv6 option 39 fqdn2encdns FQDN to DNS encoding DNS encoder

viewtopic.php?f=1&t=178607&p=880429#p880424
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Sat Oct 02, 2021 1:53 pm

Save and Restore global variables on reboot

viewtopic.php?f=9&t=170591&p=883422#p883422
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Sat Feb 26, 2022 11:25 am

Save RouterBOARD full backup of everything [configuration, certificates, host key, router users (no passwords), licence, user-manager, dude, other files]
viewtopic.php?f=1&t=175360&p=858564#p858564
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Mon May 02, 2022 5:23 am

Online function to obtain the start date and time of the RouterBOARD.
It takes into account the time zone set in the RouterBOARD.
viewtopic.php?t=185616#p930465
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Thu May 19, 2022 7:17 pm

Convert float to Celsius for TG-BT5-OUT
viewtopic.php?p=934123#p934123
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Mon May 23, 2022 2:39 pm

Convert the date string (May|may)/23/2022 to number 20220523
(the "date" type on RouterOS do not exist)
viewtopic.php?p=934798#p934798
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Tue May 24, 2022 5:12 pm

Fomr 2014: I forgot that on print and on find is possible to do some operations insde search string...
like (just an example)
/ip address print where (network + 1) = 192.168.88.1

viewtopic.php?p=411411#p411411
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Tue May 24, 2022 5:23 pm

✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Tue May 24, 2022 7:09 pm

Code: Select all

# TAG # lastcheck (25 Mar 2014, 13:34)
viewtopic.php?p=417214#p417214


TAG: ###RCHCK###
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Fri Jun 03, 2022 7:31 pm

Check if the URL is valid and read server response and redirects:
viewtopic.php?p=937034#p937124
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Tue Aug 09, 2022 2:54 pm

Create a log file and dynamically split the file every 4095 Bytes or less
viewtopic.php?p=950767#p950761
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Fri Aug 26, 2022 3:30 pm

Obtain some info from one IP, convert subnet mask to prefix, convert prefix to subnet mask, convert IP to num, convert num to IP, convert IP to HEX, convert HEX to IP etc.

If the IP have (do not have) subnet mask different from /32 (255.255.255.255)
other useful info can be obtained from this information.
(MikroTik actually do not support directly /31 addresses)

For example if you have this 10.31.42.56/16
{
:local source    10.31.42.56/16

:local ip        [:toip [:pick $source 0 [:find $source "/"]]]
:local prefix    [:tonum [:pick $source ([:find $source "/"] + 1) [:len $source]]]
:local submask   (~(255.255.255.255>>$prefix))
:local addrspace (~$submask)
:local network   ($ip & $submask)
:local broadcast ($ip | $addrspace)
:local first     ($network + 1)
:local last      ($broadcast - 1)
:put "       Source: $source"
:put "           IP: $ip"
:put "Subnet Prefix: $prefix"
:put "  Subnet Mask: $submask"
:put "Address Space: $addrspace"
:put "  Network* IP: $network"
:put "Broadcast* IP: $broadcast"
:put "    First* IP: $first"
:put "     Last* IP: $last"
}
* = Network / Broadcast / First IP and Last IP are valid only when the IP are distribuited on local LAN,
instead for routing only, all IP can be used.
.0 and .255 are perfectly valid IP if are not the network ip or the broadcast address,
but for compatibility with some end devices that have problems with .0 and .255 outside a /24, is better remove all .0 and all .255 from the IP pools assigned from DHCP Server.

For example if you have this IP 10.31.42.56 and subnet 255.255.0.0
{
:local sourceip    10.31.42.56
:local sourcesub   255.255.0.0

:local ip        [:toip $sourceip]
:local submask   [:toip $sourcesub]
:local prefix    32
:local addrspace (~$submask)
:local tempsub   [:tonum $addrspace]
:while ($tempsub > 0) do={:set tempsub ($tempsub / 2); :set prefix ($prefix - 1)}
:local network   ($ip & $submask)
:local broadcast ($ip | $addrspace)
:local first     ($network + 1)
:local last      ($broadcast - 1)
:put "    Source IP: $sourceip"
:put "Source Subnet: $sourcesub"
:put "Subnet Prefix: $prefix"
:put "  Subnet Mask: $submask"
:put "Address Space: $addrspace"
:put "  Network* IP: $network"
:put "Broadcast* IP: $broadcast"
:put "    First* IP: $first"
:put "     Last* IP: $last"
}

Convert IP to decimal number: (127.0.0.1 = 2130706433)
:put [:tonum 127.0.0.1]

Convert decimal number to IP: (2130706433 = 127.0.0.1)
:put (0.0.0.0 + 2130706433)

Convert IP to hexadecimal number
Using this:
viewtopic.php?f=2&t=57665&p=868898#p868898
The IP can be converted first to decimal, then to hexadecimal.
:put [$num2hex [:tonum 127.0.0.1]]

Convert hexadecimal number to IP
:put (0.0.0.0 + 0x7F000001)
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Mon Oct 31, 2022 12:31 pm

Script for convert codepage or replace characters inside a string
viewtopic.php?p=965180#p965180
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Mon Oct 31, 2022 2:55 pm

decimal 2 binary (only 1 byte)
:global dec2bin do={
    :local number [:tonum $1]
    :local b8 0 ; :if ($number & 128) do={:set b8 1}
    :local b7 0 ; :if ($number &  64) do={:set b7 1}
    :local b6 0 ; :if ($number &  32) do={:set b6 1}
    :local b5 0 ; :if ($number &  16) do={:set b5 1}
    :local b4 0 ; :if ($number &   8) do={:set b4 1}
    :local b3 0 ; :if ($number &   4) do={:set b3 1}
    :local b2 0 ; :if ($number &   2) do={:set b2 1}
    :local b1 0 ; :if ($number &   1) do={:set b1 1}
    :return "$b8$b7$b6$b5$b4$b3$b2$b1"
}

hexadecimal 2 binary (only 1 byte) expected as 0x00 ... 0xFF or 00... FF value:
:global hex2bin do={
    :local conv $1
    :if (!($conv~"^[0-9a-fA-F]{2}\$")) do={:return "00000000"}
    :if ([:typeof [:find $conv "0x" -1]] = "nil") do={:set conv "0x$conv"}
    :local number [:tonum $conv]
    :local b8 0 ; :if ($number & 128) do={:set b8 1}
    :local b7 0 ; :if ($number &  64) do={:set b7 1}
    :local b6 0 ; :if ($number &  32) do={:set b6 1}
    :local b5 0 ; :if ($number &  16) do={:set b5 1}
    :local b4 0 ; :if ($number &   8) do={:set b4 1}
    :local b3 0 ; :if ($number &   4) do={:set b3 1}
    :local b2 0 ; :if ($number &   2) do={:set b2 1}
    :local b1 0 ; :if ($number &   1) do={:set b1 1}
    :return "$b8$b7$b6$b5$b4$b3$b2$b1"
}
Last edited by rextended on Mon Oct 31, 2022 3:54 pm, edited 2 times in total.
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Mon Oct 31, 2022 3:32 pm

binary to decimal (only 8 bit / 1 byte) from 00000000 to 11111111
:global bin2dec do={
    :local bin $1
    :local dec  0
    :local mol  1
    :if (!($bin~"^[0-1]{8}\$")) do={:return 0}
    :for pos from=1 to=8 do={
        :local temp [:tonum [:pick $bin (8 - $pos) (8 - $pos + 1)]]
        :set dec ($dec + ($temp * $mol))
        :set mol ($mol * 2)
    }
    :return $dec
}

binary to hexadecimal (only 8 bit / 1 byte) from 00000000 to 11111111
the output is on 0x00 ... 0xFF format, for remove the 0x simplu remove 0x on last ":return"
:global bin2hex do={
    :local bin $1
    :local dec  0
    :local mol  1
    :if (!($bin~"^[0-1]{8}\$")) do={:return "0x00"}
    :for pos from=1 to=8 do={
        :local temp [:tonum [:pick $bin (8 - $pos) (8 - $pos + 1)]]
        :set dec ($dec + ($temp * $mol))
        :set mol ($mol * 2)
    }
    :local hexadec   "0"
    :local remainder 0
    :local hexChars  "0123456789ABCDEF"
    :if ($dec > 0) do={:set hexadec ""}
    :while ( $dec > 0 ) do={
          :set remainder ($dec % 16)
          :set dec       (($dec-$remainder) / 16)
          :set hexadec   ([:pick $hexChars $remainder].$hexadec)
    } 
    :if ([:len $hexadec] = 1) do={:set hexadec "0$hexadec"}
    :return "0x$hexadec"
}

this is the version of binary to decimal that support binary signed QWORD (64 bit) and is the max supported from RouterOS
:global binQW2dec do={
    :local bin $1
    :local dec  0
    :local mol  1
    :local lgt [:len $bin]
    :if (!($bin~"^[0-1]{$lgt}\$")) do={:return 0}
    :for pos from=1 to=$lgt do={
        :local temp [:tonum [:pick $bin ($lgt - $pos) ($lgt - $pos + 1)]]
        :set dec ($dec + ($temp * $mol))
        :set mol ($mol * 2)
    }
    :return $dec
}

this is the version of binary to hexadecimal that support binary signed QWORD (64 bit) and is the max supported from RouterOS
:global binQW2hex do={
    :local bin $1
    :local dec  0
    :local mol  1
    :local lgt [:len $bin]
    :if (!($bin~"^[0-1]{$lgt}\$")) do={:return "0x00"}
    :for pos from=1 to=$lgt do={
        :local temp [:tonum [:pick $bin ($lgt - $pos) ($lgt - $pos + 1)]]
        :set dec ($dec + ($temp * $mol))
        :set mol ($mol * 2)
    }
    :local firstchar ""
    :if ($dec < 0) do={
        :local chk (($dec & 0x7000000000000000) >> 60)
        :set firstchar [:pick "89ABCDEF" $chk ($chk + 1)]
        :set dec ($dec & 0x0FFFFFFFFFFFFFFF)
    }
    :local hexadec   "0"
    :local remainder 0
    :local hexChars  "0123456789ABCDEF"
    :if ($dec > 0) do={:set hexadec ""}
    :while ( $dec > 0 ) do={
          :set remainder ($dec % 16)
          :set dec       (($dec-$remainder) / 16)
          :set hexadec   ([:pick $hexChars $remainder].$hexadec)
    } 
    :if ($firstchar != "") do={
        :set hexadec "00000000000000$hexadec"
        :set hexadec "$firstchar$[:pick $hexadec ([:len $hexadec] - 15) [:len $hexadec]]"
    }
    :return "0x$hexadec"
}
Last edited by rextended on Mon Oct 31, 2022 6:33 pm, edited 4 times in total.
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==
 
User avatar
rextended
Forum Guru
Forum Guru
Topic Author
Posts: 8794
Joined: Tue Feb 25, 2014 12:49 pm
Location: 🇮🇹, my 💔 is in 🇺🇦

Re: ✂ Rextended Fragments of Snippets

Mon Oct 31, 2022 3:41 pm

for convert decimal to hexadecimal (not only one byte) can be used:
viewtopic.php?p=868898#p868898

for convert hexadecimal to decimal, on RouterOS already exist this two methods (0x must be present):
:put 0xFF85

:put [:tonum "0xFF85"]

but this can be used (not only one byte):
:global hex2dec do={
    :local conv $1
    :if (!($conv~"^[0-9a-fA-F]+\$")) do={:return 0}
    :if ([:typeof [:find $conv "0x" -1]] = "nil") do={:set conv "0x$conv"}
    :return [:tonum $conv]
}
WARNING: RouterOS have a bug that do not recognize negative hex values, on future I mod the previous script to support QWORD hex conversion
✂ Rextended Fragments of Snippets

Vld4UmVHUkdhelJUTTJzOQ==

Who is online

Users browsing this forum: diamuxin, sfaqih, td32 and 7 guests