import paramiko, time client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect('192.168.100.2', username='mi4', password='m14', look_for_keys=False, allow_agent=False) # Simple test simple_code = ':put "hello world" ; :local x 5 ; :put $x' escaped = simple_code.replace('"', '\\"').replace('$', '\\$').replace('\\', '\\\\') print(f"Original: {simple_code}") print(f"Escaped: {escaped}") chan = client.invoke_shell() chan.settimeout(5) time.sleep(1.5) out = b'' while chan.recv_ready(): out += chan.recv(65535) # Create test script chan.send(f'/system script add name=test_esc source="{escaped}"\n') time.sleep(2) out = b'' while chan.recv_ready(): out += chan.recv(65535) print(f"\nCreate result: {out.decode(errors='replace')[:200]}") # Read it back stdin, stdout, stderr = client.exec_command(':put [/system script get test_esc source]') time.sleep(0.5) result = stdout.read().decode().strip() print(f"\nStored source: {result}") # Cleanup chan.send('/system script remove test_esc\n') time.sleep(1) chan.close() client.close()