check that the username and others variable are valid

I'm trying to make a script to check that the username is valid...but unfortunately my knowledge is still low...
Can you help me??
What am I doing wrong??

Thanks


Script I'm using (maybe it's all wrong..)

:local userName1 "@.-_012abcABC"               ; # valid
:local userName2 " !|#~%&/()=?'`^[]+*{},;:<>"  ; # invalid
:local userName3 "ºª¡·¬¿´¨ñÑçÇ€"               ; # not printable
:local userName4 "]["                          ; # problems
:local userName5 "[]"                          ; # problems


:global checkString do={
    :local stringValue $1
    :local stringName $2
    :local isValid true

:local invalidChars "[ ºª\\!|\"·#\$~%&¬/()='?¿¡`^+*´¨çÇñÑ,;:<>{}\\[\\]]"
#:local invalidChars "[ ºª\\!|\"·#\$~%&¬/()='?¿¡`^+*´¨çÇñÑ,;:<>{}]"
#:local invalidChars "[\5B\20\C2\BA\C2\AA\5C\21\7C\22\C2\B7\23\24\7E\25\26\C2\AC\2F\28\29\3D\27\3F\C2\BF\C2\A1\60\5E\2B\2A\C2\B4\C2\A8\C3\A7\C3\87\C3\B1\C3\91\2C\3B\3A\3C\3E\7B\7D\5D]"

    :if ([:len $stringValue] = 0) do={
        :put ($stringName . " value can't be empty")
        :set isValid false
    } else={
        :if ([:tostr $stringValue] ~ [:tostr $invalidChars]) do={
            :put ($stringName . " value can't contain invalid chars")
            :set isValid false
        }
    }
    :put ($stringName . " " . $stringValue)
    :return $isValid
}

:put "Value - User for /system user"
:local userName1Valid [$checkString $userName1 "User name 1 ->"]
:local userName2Valid [$checkString $userName2 "User name 2 ->"]
:local userName3Valid [$checkString $userName3 "User name 3 ->"]
:local userName4Valid [$checkString $userName4 "User name 4 ->"]
:local userName5Valid [$checkString $userName5 "User name 5 ->"]

Results:

#--------------------------------------------------------------------------------------------------#

Test 1

:local invalidChars "[ ºª\!|"·#$~%&¬/()='?¿¡^+*´¨çÇñÑ,;:<>{}\\[\\]]" #:local invalidChars "[ ºª\\!|\"·#\$~%&¬/()='?¿¡^+*´¨çÇñÑ,;:<>{}]"

#--------------------------------------------------------------------------------------------------#

Test 1-1

:local userName1 "@.-_012abcABC"
:local userName2 " !|#~%&/()=?'`[1]+*{},;:<>"
#:local userName3 "ºª¡·¬¿´¨ñÑçÇ€"
#:local userName4 "]["
#:local userName5 ""

Result on Terminal
User name 1 -> @.-_012abcABC
User name 2 -> value can't contain invalid chars
User name 2 -> !|#~%&/()=?'`[2]+*{},;:<>
User name 3 -> value can't be empty
User name 3 ->
User name 4 -> value can't be empty
User name 4 ->
User name 5 -> value can't be empty
User name 5 ->

#--------------------------------------------------------------------------------------------------#

Test 2-1

:local userName1 "@.-_012abcABC"
:local userName2 " !|#~%&/()=?'`[3]+*{},;:<>"
:local userName3 "ºª¡·¬¿´¨ñÑçÇ€"
#:local userName4 "]["
#:local userName5 ""

Result on Terminal
User name 1 -> @.-_012abcABC
User name 2 -> value can't contain invalid chars
User name 2 -> !|#~%&/()=?'`[4]+*{},;:<>
User name 3 ->
User name 4 -> value can't be empty
User name 4 ->
User name 5 -> value can't be empty
User name 5 ->

#--------------------------------------------------------------------------------------------------#

Test 3-1

:local userName1 "@.-_012abcABC"
:local userName2 " !|#~%&/()=?'`[5]+*{},;:<>"
:local userName3 "ºª¡·¬¿´¨ñÑçÇ€"
:local userName4 "]["
#:local userName5 ""

Result on Terminal
User name 1 -> @.-_012abcABC
User name 2 -> value can't contain invalid chars
User name 2 -> !|#~%&/()=?'`[6]+*{},;:<>
User name 3 ->
User name 4 -> ][
User name 5 -> value can't be empty
User name 5 ->

#--------------------------------------------------------------------------------------------------#

Test 4-1

:local userName1 "@.-_012abcABC"
:local userName2 " !|#~%&/()=?'`[7]+*{},;:<>"
:local userName3 "ºª¡·¬¿´¨ñÑçÇ€"
:local userName4 "]["
:local userName5 ""

Result on Terminal
User name 1 -> @.-_012abcABC
User name 2 -> value can't contain invalid chars
User name 2 -> !|#~%&/()=?'`[8]+*{},;:<>
User name 3 ->
User name 4 -> ][
User name 5 -> value can't contain invalid chars
User name 5 ->

#--------------------------------------------------------------------------------------------------#

Test 2

#:local invalidChars "[ ºª\!|"·#$~%&¬/()='?¿¡^+*´¨çÇñÑ,;:<>{}\\[\\]]" :local invalidChars "[ ºª\\!|\"·#\$~%&¬/()='?¿¡^+*´¨çÇñÑ,;:<>{}]"

#--------------------------------------------------------------------------------------------------#

Test 1-2

:local userName1 "@.-_012abcABC"
:local userName2 " !|#~%&/()=?'`[9]+*{},;:<>"
#:local userName3 "ºª¡·¬¿´¨ñÑçÇ€"
#:local userName4 "]["
#:local userName5 ""

Result on Terminal
User name 1 -> @.-_012abcABC
User name 2 -> value can't contain invalid chars
User name 2 -> !|#~%&/()=?'`[10]+*{},;:<>
User name 3 -> value can't be empty
User name 3 ->
User name 4 -> value can't be empty
User name 4 ->
User name 5 -> value can't be empty
User name 5 ->

#--------------------------------------------------------------------------------------------------#

Test 2-2

:local userName1 "@.-_012abcABC"
:local userName2 " !|#~%&/()=?'`[11]+*{},;:<>"
:local userName3 "ºª¡·¬¿´¨ñÑçÇ€"
#:local userName4 "]["
#:local userName5 ""

Result on Terminal
User name 1 -> @.-_012abcABC
User name 2 -> value can't contain invalid chars
User name 2 -> !|#~%&/()=?'`[12]+*{},;:<>
User name 3 -> value can't contain invalid chars
User name 3 ->
User name 4 -> value can't be empty
User name 4 ->
User name 5 -> value can't be empty
User name 5 ->

#--------------------------------------------------------------------------------------------------#

Test 3-2

:local userName1 "@.-_012abcABC"
:local userName2 " !|#~%&/()=?'`[13]+*{},;:<>"
:local userName3 "ºª¡·¬¿´¨ñÑçÇ€"
:local userName4 "]["
#:local userName5 ""

Result on Terminal
User name 1 -> @.-_012abcABC
User name 2 -> value can't contain invalid chars
User name 2 -> !|#~%&/()=?'`[14]+*{},;:<>
User name 3 -> value can't contain invalid chars
User name 3 ->
User name 4 -> ][
User name 5 -> value can't be empty
User name 5 ->

#--------------------------------------------------------------------------------------------------#

Test 4-2

:local userName1 "@.-_012abcABC"
:local userName2 " !|#~%&/()=?'`[15]+*{},;:<>"
:local userName3 "ºª¡·¬¿´¨ñÑçÇ€"
:local userName4 "]["
:local userName5 ""

Result on Terminal
User name 1 -> @.-_012abcABC
User name 2 -> value can't contain invalid chars
User name 2 -> !|#~%&/()=?'`[16]+*{},;:<>
User name 3 -> value can't contain invalid chars
User name 3 ->
User name 4 -> ][
User name 5 ->

#--------------------------------------------------------------------------------------------------#


  1. ↩︎

  2. ↩︎

  3. ↩︎

  4. ↩︎

  5. ↩︎

  6. ↩︎

  7. ↩︎

  8. ↩︎

  9. ↩︎

  10. ↩︎

  11. ↩︎

  12. ↩︎

  13. ↩︎

  14. ↩︎

  15. ↩︎

  16. ↩︎

http://forum.mikrotik.com/t/how-to-check-if-special-character-is-present-in-the-message/168997/4


Instead of lost time to identify unsupported RouterOS characters (from 0x80 to 0xFF) or invalid / not printable / with problems,
simply:
:global uvalid do={
# \2D = -
# \2E = .
# \30..\39 = 0..9
# \40 = @
# \41 .. \5A = A..Z
# \5F = _
# \61..\7A a..z
# so, in this case, the accepted characters are: - . 0..9 @ A..Z _
:if ($1~“[\01-\2C\2F\3A-\3F\5B-\5E\60\7B-\FF]”) do={ return false }
:return true
}

{
:local uname “inval!d=user”
:put “$uname is valid is $[$uvalid $uname]”

:local uname “Av@l1d_user.”
:put “$uname is valid is $[$uvalid $uname]”
}

Simple and effective

I saw and tried your post but I didn’t have the expected result.
http://forum.mikrotik.com/t/how-to-check-if-special-character-is-present-in-the-message/168997/4

To do the same in a userPass variable…I would just have to change/adapt this line…right?
“[\01-\2C\2F\3A-\3F\5B-\5E\60\7B-\FF]”

Grazie mille rextended

Create separate pvalid function like
:global pvalid do={
#all characters are valid, from [SPACE] to “~” except unsupported from RouterOS
:if ($1~“[\01-\19\7F-\FF]”) do={ return false }
:return true
}

It's a script to install after a default reset...and I already have the result I wanted...I still need to add some more checks...the USB (for Pi-hole) and the IP Service Ports and WireGuard ports...but I think you've solved the hardest part for me

Maybe it's silly to make such a big script to configure only 20 variables but these are the first ones I've made and this way I practice

Learning is always good

Thanks again rextended



  • View in Terminal
    Tested values

Value - Identity for /system identity
Identity1 -> !#%&'()*+,-./09:;<=>?@AZ^_`az{|}~
Identity2 -> value can't contain invalid chars
Identity3 -> value can't be empty

Value - User-Pass for /system user
User name1 -> -.09@AZ_az
User name2 -> value can't start with invalid chars
User name3 -> value can't contain invalid chars
User name4 -> value can't be empty
User pass1 -> !#%&'()*+,-./09:;<=>?@AZ^_`az{|}~
User pass2 -> value can't contain invalid chars
User pass3 -> value can't be empty
User pass4 -> value can be empty

Values - WiFi for /interface/wifi
WiFi1.........ssid -> espace !#%&'()*+,-./09:;<=>?@AZ^_az{|}~ WiFi2.........ssid -> value can't contain invalid chars WiFi3.........ssid -> value can't be empty WiFi1...passphrase -> !#%&'()*+,-./09:;<=>?@AZ[]^_az{|}~
WiFi2...passphrase -> value can't contain invalid chars
WiFi3...passphrase -> value can't be empty
WiFi4...passphrase -> value can be empty

Values - WireGuard for /interface/wireguard
WireGuard.......................key1 -> +/09=AZaz
WireGuard.......................key2 -> value can't start with invalid chars
WireGuard.......................key3 -> value can't contain invalid chars
WireGuard.......................key4 -> value can be empty
WireGuard Peer......client endpoint1 -> -./09:az
WireGuard Peer......client endpoint2 -> value can't start with invalid chars
WireGuard Peer......client endpoint3 -> value can't contain invalid chars
WireGuard Peer......client endpoint4 -> value can be empty

Value - Pi-hole for /container
Pi-hole...passphrase1 -> -.09@AZ_az
Pi-hole...passphrase2 -> value can't start with invalid chars
Pi-hole...passphrase3 -> value can't contain invalid chars
Pi-hole...passphrase4 -> value can't be empty



Script will NOT run


Script file loaded and executed successfully