【问题标题】:I want to list all the files that are in my sftp server我想列出我的 sftp 服务器中的所有文件
【发布时间】:2022-01-10 10:06:55
【问题描述】:

我想列出我服务器中的所有文件。但它像这样[]返回空。我能做些什么?我的代码有什么问题?我正在使用一个名为 paramiko 的模块。

command = "ls"

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)

stdin, stdout, stderr = ssh.exec_command(command)
lines = stdout.readlines()
print(lines)

【问题讨论】:

    标签: python python-3.x sftp paramiko


    【解决方案1】:

    您的代码与 SFTP 无关。您正在 remote shell 中执行 ls 命令。

    要使用 SFTP 列出文件,请使用 SFTPClient.listdir_attr

    sftp = ssh.open_sftp()
    for entry in sftp.listdir_attr():
        print(entry.filename)
    

    强制警告:不要使用AutoAddPolicy - 这样做会失去对MITM attacks 的保护。如需正确解决方案,请参阅Paramiko "Unknown Server"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多