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. 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"
.
Method for passing the string variable:
# Extract JSON data
def get_folder_from_json():
device_json_path = "devices.json"
# Read the JSON data from the file
with open(device_json_path, "r") as device_json_file:
device_data = json.load(device_json_file)
for key, value in device_data.items():
if value["device"] == selected_device:
print(f"key: {key}")
print(f"device: {selected_device}")
folder = value["folder"]
# folder = Path(folder) # 1.test
# folder = f'"{value["folder"]}"' # 2.test
# folder = fr"{folder}" # 3.test
# folder = f"""{folder}""" # 4.test
# folder = '"'+folder+'"' # 5.test
# folder = f"{folder}" # 6.test
# folder = str(folder) # 7.test
# folder = rf"{folder}" # 8.test
# folder = '''+{folder}''' # 9.test
# folder = repr(folder) # 10.test
# folder = "%s" % folder # 11.test
# folder = "{0}".format(folder) # 12.test
# folder = "%(folder)s" % locals() # 13.test
# folder = '/home/server/device-2-21' # 14.test
# folder = "homeserverdevice-2-21" # 15.test
folder = "/home/server/device-2-21" # working example
print(f"folder var type: ", type(folder))
print(f"device folder: {folder}")
get_latest_device_log_via_ssh_tunnel(selected_device, folder)
Method to establish an SSH connection and get the latest folder from the remote server:
def get_latest_device_log_via_ssh_tunnel(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 folder = "/home/server/device-2-21"
and not with any other string formatting? What am I missing? Where is the catch?