s = ':put "hello" ; :local x 5 ; :put $x' print('orig:', repr(s)) print('orig raw:', s) # The original escape logic escaped = s.replace('\\', '\\\\').replace('"', '\\"').replace('$', '\\$') print('escaped repr:', repr(escaped)) print('escaped raw:', escaped) # What RouterOS expects: # In RouterOS CLI: /system script add source=":put \"hello\" ; :local x 5 ; :put \$x" # The CLI parser sees: \", \$, and stores: :put "hello" ; :local x 5 ; :put $x # Test: what does the CLI actually store? # Note: the escaped value needs to be: # - \" for literal double quote # - \$ for literal dollar sign # - \\ for literal backslash correct = s.replace('"', '\\"').replace('$', '\\$') print('\ncorrect repr:', repr(correct)) print('correct raw:', correct)