0

I want to list all the files in my server. But it returns empty like this []. What can I do? And what is wrong with my code? I am using a module called 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)
arak
  • 21
  • 4

1 Answers1

0

Your code has nothing to do with SFTP. You are executing ls command in remote shell.

To list files using SFTP, use SFTPClient.listdir_attr:

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

Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992