作为参数传递以调用 ssh shell 脚本的字符串变量的正确格式是什么?

问题描述 投票:0回答:1

我试图传递一个字符串变量作为参数来调用

ssh_client
shell 命令来查找远程服务器上的最新文件夹。

value["folder"]
来自格式为
/home/server/device-2-21
的json文件,不带引号。

我尝试以不同的格式传递字符串参数,但没有成功。用于搜索最新文件夹的脚本仅适用于表达式

folder = "/home/server/device-2-21"

传递字符串变量

folder = /home/server/device-2-21
:

device = value["device"]            # from JSON
folder = 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 FAIL
folder = "/home/server/device"      # 16.test OK
print(f"folder var type: ", type(folder))
print(f"device folder: {folder}")

# pass the string variable to the search method
get_latest_device_folder(device, folder)



# connect and get the latest folder from the remote server
def 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()
 

我已经检查了这些帖子,但无法找出解决方案:

  1. ssh 使用参数执行 shell 脚本
  2. Python 使用参数调用 shell 脚本
  3. 格式化 ssh 参数的字符串 - Python
  4. 将变量值插入字符串
  5. 如何将变量的值放入字符串中(将其插入字符串中)?
  6. 如何在Python中编写字符串文字而不必转义它们?
  7. https://dnmtechs.com/writing-windows-paths-in-python-3-a-guide-to-string-literals/
  8. 我应该如何在Python字符串中编写Windows路径?
  9. 将 WindowsPath 转换为字符串

为什么传递表达式

folder = "/home/server/device"
folder = '/home/server/device'
可以工作,但传递变量
folder
却不起作用?我缺少什么?陷阱在哪里?

python python-3.x ssh arguments paramiko
1个回答
0
投票

感谢 @MartinPrikryl 的建议来比较这些值,我意识到一些 json 文件记录已损坏。问题是

/
中的条目开头缺少
value["folder"]
。很难注意到,但一旦并排比较就很清楚了。我应该记得先检查一下数据。考虑此案已结。

© www.soinside.com 2019 - 2024. All rights reserved.