Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
escapeString function Mikrotik RouterboardOS Script
:global escapeString do={
# convert non-printable characters in $value to HEX-escaped compatible with the Scripting Language
# string loop based on URL encoding in http://forum.mikrotik.com/viewtopic.php?t=84705
:local result "";
# set empty associative array: http://forum.mikrotik.com/viewtopic.php?p=418796&sid=52e10f322b216132a5fd8179bbbcf08a#p418796
:local convert ({})
# special as this is non-printable as well
:set ($convert->("\7F")) ("\\7F")
#generated >>
:set ($convert->("\00")) ("\\00")
:set ($convert->("\01")) ("\\01")
:set ($convert->("\02")) ("\\02")
:set ($convert->("\03")) ("\\03")
:set ($convert->("\04")) ("\\04")
:set ($convert->("\05")) ("\\05")
:set ($convert->("\06")) ("\\06")
:set ($convert->("\07")) ("\\07")
:set ($convert->("\08")) ("\\08")
:set ($convert->("\09")) ("\\09")
:set ($convert->("\00")) ("\\00")
:set ($convert->("\0A")) ("\\0A")
:set ($convert->("\0B")) ("\\0B")
:set ($convert->("\0C")) ("\\0C")
:set ($convert->("\0D")) ("\\0D")
:set ($convert->("\0E")) ("\\0E")
:set ($convert->("\0F")) ("\\0F")
:set ($convert->("\10")) ("\\10")
:set ($convert->("\11")) ("\\11")
:set ($convert->("\12")) ("\\12")
:set ($convert->("\13")) ("\\13")
:set ($convert->("\14")) ("\\14")
:set ($convert->("\15")) ("\\15")
:set ($convert->("\16")) ("\\16")
:set ($convert->("\17")) ("\\17")
:set ($convert->("\18")) ("\\18")
:set ($convert->("\19")) ("\\19")
:set ($convert->("\10")) ("\\10")
:set ($convert->("\1A")) ("\\1A")
:set ($convert->("\1B")) ("\\1B")
:set ($convert->("\1C")) ("\\1C")
:set ($convert->("\1D")) ("\\1D")
:set ($convert->("\1E")) ("\\1E")
:set ($convert->("\1F")) ("\\1F")
:set ($convert->("\80")) ("\\80")
:set ($convert->("\81")) ("\\81")
:set ($convert->("\82")) ("\\82")
:set ($convert->("\83")) ("\\83")
:set ($convert->("\84")) ("\\84")
:set ($convert->("\85")) ("\\85")
:set ($convert->("\86")) ("\\86")
:set ($convert->("\87")) ("\\87")
:set ($convert->("\88")) ("\\88")
:set ($convert->("\89")) ("\\89")
:set ($convert->("\80")) ("\\80")
:set ($convert->("\8A")) ("\\8A")
:set ($convert->("\8B")) ("\\8B")
:set ($convert->("\8C")) ("\\8C")
:set ($convert->("\8D")) ("\\8D")
:set ($convert->("\8E")) ("\\8E")
:set ($convert->("\8F")) ("\\8F")
:set ($convert->("\90")) ("\\90")
:set ($convert->("\91")) ("\\91")
:set ($convert->("\92")) ("\\92")
:set ($convert->("\93")) ("\\93")
:set ($convert->("\94")) ("\\94")
:set ($convert->("\95")) ("\\95")
:set ($convert->("\96")) ("\\96")
:set ($convert->("\97")) ("\\97")
:set ($convert->("\98")) ("\\98")
:set ($convert->("\99")) ("\\99")
:set ($convert->("\90")) ("\\90")
:set ($convert->("\9A")) ("\\9A")
:set ($convert->("\9B")) ("\\9B")
:set ($convert->("\9C")) ("\\9C")
:set ($convert->("\9D")) ("\\9D")
:set ($convert->("\9E")) ("\\9E")
:set ($convert->("\9F")) ("\\9F")
:set ($convert->("\00")) ("\\00")
:set ($convert->("\01")) ("\\01")
:set ($convert->("\02")) ("\\02")
:set ($convert->("\03")) ("\\03")
:set ($convert->("\04")) ("\\04")
:set ($convert->("\05")) ("\\05")
:set ($convert->("\06")) ("\\06")
:set ($convert->("\07")) ("\\07")
:set ($convert->("\08")) ("\\08")
:set ($convert->("\09")) ("\\09")
:set ($convert->("\00")) ("\\00")
:set ($convert->("\0A")) ("\\0A")
:set ($convert->("\0B")) ("\\0B")
:set ($convert->("\0C")) ("\\0C")
:set ($convert->("\0D")) ("\\0D")
:set ($convert->("\0E")) ("\\0E")
:set ($convert->("\0F")) ("\\0F")
:set ($convert->("\A0")) ("\\A0")
:set ($convert->("\A1")) ("\\A1")
:set ($convert->("\A2")) ("\\A2")
:set ($convert->("\A3")) ("\\A3")
:set ($convert->("\A4")) ("\\A4")
:set ($convert->("\A5")) ("\\A5")
:set ($convert->("\A6")) ("\\A6")
:set ($convert->("\A7")) ("\\A7")
:set ($convert->("\A8")) ("\\A8")
:set ($convert->("\A9")) ("\\A9")
:set ($convert->("\A0")) ("\\A0")
:set ($convert->("\AA")) ("\\AA")
:set ($convert->("\AB")) ("\\AB")
:set ($convert->("\AC")) ("\\AC")
:set ($convert->("\AD")) ("\\AD")
:set ($convert->("\AE")) ("\\AE")
:set ($convert->("\AF")) ("\\AF")
:set ($convert->("\B0")) ("\\B0")
:set ($convert->("\B1")) ("\\B1")
:set ($convert->("\B2")) ("\\B2")
:set ($convert->("\B3")) ("\\B3")
:set ($convert->("\B4")) ("\\B4")
:set ($convert->("\B5")) ("\\B5")
:set ($convert->("\B6")) ("\\B6")
:set ($convert->("\B7")) ("\\B7")
:set ($convert->("\B8")) ("\\B8")
:set ($convert->("\B9")) ("\\B9")
:set ($convert->("\B0")) ("\\B0")
:set ($convert->("\BA")) ("\\BA")
:set ($convert->("\BB")) ("\\BB")
:set ($convert->("\BC")) ("\\BC")
:set ($convert->("\BD")) ("\\BD")
:set ($convert->("\BE")) ("\\BE")
:set ($convert->("\BF")) ("\\BF")
:set ($convert->("\C0")) ("\\C0")
:set ($convert->("\C1")) ("\\C1")
:set ($convert->("\C2")) ("\\C2")
:set ($convert->("\C3")) ("\\C3")
:set ($convert->("\C4")) ("\\C4")
:set ($convert->("\C5")) ("\\C5")
:set ($convert->("\C6")) ("\\C6")
:set ($convert->("\C7")) ("\\C7")
:set ($convert->("\C8")) ("\\C8")
:set ($convert->("\C9")) ("\\C9")
:set ($convert->("\C0")) ("\\C0")
:set ($convert->("\CA")) ("\\CA")
:set ($convert->("\CB")) ("\\CB")
:set ($convert->("\CC")) ("\\CC")
:set ($convert->("\CD")) ("\\CD")
:set ($convert->("\CE")) ("\\CE")
:set ($convert->("\CF")) ("\\CF")
:set ($convert->("\D0")) ("\\D0")
:set ($convert->("\D1")) ("\\D1")
:set ($convert->("\D2")) ("\\D2")
:set ($convert->("\D3")) ("\\D3")
:set ($convert->("\D4")) ("\\D4")
:set ($convert->("\D5")) ("\\D5")
:set ($convert->("\D6")) ("\\D6")
:set ($convert->("\D7")) ("\\D7")
:set ($convert->("\D8")) ("\\D8")
:set ($convert->("\D9")) ("\\D9")
:set ($convert->("\D0")) ("\\D0")
:set ($convert->("\DA")) ("\\DA")
:set ($convert->("\DB")) ("\\DB")
:set ($convert->("\DC")) ("\\DC")
:set ($convert->("\DD")) ("\\DD")
:set ($convert->("\DE")) ("\\DE")
:set ($convert->("\DF")) ("\\DF")
:set ($convert->("\E0")) ("\\E0")
:set ($convert->("\E1")) ("\\E1")
:set ($convert->("\E2")) ("\\E2")
:set ($convert->("\E3")) ("\\E3")
:set ($convert->("\E4")) ("\\E4")
:set ($convert->("\E5")) ("\\E5")
:set ($convert->("\E6")) ("\\E6")
:set ($convert->("\E7")) ("\\E7")
:set ($convert->("\E8")) ("\\E8")
:set ($convert->("\E9")) ("\\E9")
:set ($convert->("\E0")) ("\\E0")
:set ($convert->("\EA")) ("\\EA")
:set ($convert->("\EB")) ("\\EB")
:set ($convert->("\EC")) ("\\EC")
:set ($convert->("\ED")) ("\\ED")
:set ($convert->("\EE")) ("\\EE")
:set ($convert->("\EF")) ("\\EF")
:set ($convert->("\F0")) ("\\F0")
:set ($convert->("\F1")) ("\\F1")
:set ($convert->("\F2")) ("\\F2")
:set ($convert->("\F3")) ("\\F3")
:set ($convert->("\F4")) ("\\F4")
:set ($convert->("\F5")) ("\\F5")
:set ($convert->("\F6")) ("\\F6")
:set ($convert->("\F7")) ("\\F7")
:set ($convert->("\F8")) ("\\F8")
:set ($convert->("\F9")) ("\\F9")
:set ($convert->("\F0")) ("\\F0")
:set ($convert->("\FA")) ("\\FA")
:set ($convert->("\FB")) ("\\FB")
:set ($convert->("\FC")) ("\\FC")
:set ($convert->("\FD")) ("\\FD")
:set ($convert->("\FE")) ("\\FE")
:set ($convert->("\FF")) ("\\FF")
# generated <<
# :put "$convert"
:for i from=0 to=([:len $value] - 1) do={
:local char [:pick $value $i]
:local converted ($convert->"$char")
:local convertedType [:typeof $converted]
# :put "$char $converted $convertedType"
:if ($convertedType = "str") do={
:set $char $converted
}
:set result ($result.$char)
}
:return $result;
}
# Example:
# :put [$escapeString value=("f o\00\01\7E\7F\80\81\FE\FFo")]
# f o\00\01~\7F\80\81\FE\FFo
:global convert ({})
#:set ($convert->"\00") "\\00"
:set ($convert->"\7F") "\\7F"
:set ($convert->"\81") "\\81"
:local hexChars "01234567890ABCDEF"
:local groupChars "01890ABCDEF"
:for groupCharsIndex from=0 to=([:len $groupChars] - 1) do={
:local groupChar [:pick $groupChars $groupCharsIndex]
:for hexCharsIndex from=0 to=([:len $hexChars] - 1) do={
:local hexChar [:pick $hexChars $hexCharsIndex]
:put ":set (\$convert->(\"\\$groupChar$hexChar\")) (\"\\\\$groupChar$hexChar\")"
}
}
:put [/system script environment print]
:put [:typeof $convert]
:put "$convert"
:put ($convert->"\00")
:put ($convert->"\81")
:put ($convert->"f")
:put [:typeof ($convert->"\00")]
:put [:typeof ($convert->"\81")]
:put [:typeof ($convert->"f")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.