I am trying to pass a string variable as an argument to call a ssh_client shell command to find the latest folder on a remote server.
The value["folder"] comes from a json file in the format /home/server/device-2-21, without quotes.
I tried to pass the string argument in different formats without success. The script for searching the latest folder works only with the expression folder = "/home/server/device-2-21".
Pass the string variable folder = /home/server/device-2-21:
device = value["device"] # from JSONfolder = value["folder"] # from JSON# folder = Path(folder) # 1.test FAIL# folder = f'"{value["folder"]}"' # 2.test FAIL# folder = fr"{folder}" # 3.test FAIL# folder = f"""{folder}""" # 4.test FAIL# folder = '"'+folder+'"' # 5.test FAIL# folder = f"{folder}" # 6.test FAIL# folder = str(folder) # 7.test FAIL# folder = rf"{folder}" # 8.test FAIL# folder = '''+{folder}''' # 9.test FAIL# folder = repr(folder) # 10.test FAIL# folder = "%s" % folder # 11.test FAIL# folder = "{0}".format(folder) # 12.test FAIL# folder = "%(folder)s" % locals() # 13.test FAIL# folder = '/home/server/device' # 14.test OK# folder = "\home\server\device" # 15.test FAILfolder = "/home/server/device" # 16.test OKprint(f"folder var type: ", type(folder))print(f"device folder: {folder}")# pass the string variable to the search methodget_latest_device_folder(device, folder)# connect and get the latest folder from the remote serverdef get_latest_device_folder(device_serial, device_folder): # SSH server details remote_folder = device_folder serial_number = device_serial print(f"device path: {remote_folder}") try: # Create an SSH client ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Establish an SSH connection ssh_client.connect(ssh_server, port=ssh_port, username=ssh_username, password=ssh_password) # Get the latest folder get_latest_folder = f"ls -t {remote_folder} | head -n 1" stdin, stdout, stderr = ssh_client.exec_command(get_latest_folder) latest_folder = stdout.read().decode().strip() if latest_folder: print(f"device latest folder: {latest_folder}") latest_folder = f"{remote_folder}/{latest_folder}" print(f"device latest log full path: {latest_folder}") else: print(f"no folders found") # Close the SSH connection ssh_client.close()I have checked these posts but could not figure out the solution:
- ssh execute a shell script with arguments
- Python to call shell script with arguments
- formatting a string for ssh argument - Python
- Insert variable values into a string
- How do I put a variable’s value inside a string (interpolate it into the string)?
- How to write string literals in Python without having to escape them?
- https://dnmtechs.com/writing-windows-paths-in-python-3-a-guide-to-string-literals/
- How should I write a Windows path in a Python string literal?
- Convert WindowsPath to String
Why does it work with passing the expression folder = "/home/server/device" or folder = '/home/server/device' but not with passing the variable folder? What am I missing? Where is the catch?