I am writing on windows in VS code, version python 3.6.8. I want to get a list of folders and files over SSH (on Linux).

I am writing this code:

import paramiko host = '192.168.0.8' user = 'login' secret = 'password' port = 22 client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname=host, username=user, password=secret, port=port) stdin, stdout, stderr = client.exec_command('ls') data = stdout.read() + stderr.read() Print(data) client.close() 

Displays \ X32 \ X03 \ X62, etc. As I understood it, but I do not understand how to deal with these.

  • one
    Specify encoding somewhere - fedotsoldier
  • Yes, this is understandable, but what and how to specify? - Angel Pensive
  • Ssh can pass environment variables, such as LC_ *, LANG, and others. From there, take it. Local, respectively, from local. - 0andriy
  • What is Print () ? or just a typo? - Fat-Zer
  • And in the output of the letter X exactly capitalized? It looks like a byte string, but it usually has small x. - Xander

3 answers 3

You can try an alternative way to get a list of folders and files in a directory using STPClient (). listdir ()

 import paramiko host = '192.168.0.8' user = 'login' secret = 'password' port = 22 with paramiko.SSHClient() as client: client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(hostname=host, username=user, password=secret, port=port) client_ssh = client.open_sftp() text_content = client_ssh.listdir() print(text_content) 

    Displays \ X32 \ X03 \ X62 /. As I understand it, unicode

    This is NOT Unicode. If we assume that this is a TEXT line, then the first character is '2', but the second one is an unprintable ASCII character ETX. If it were two unicode bytes, then in the first byte there would be a code greater than 0x77. Here are the Cyrillic abwg letters in Unicode:

      D0 B0 D0 B1 D0 B2 D0 B3 а б в г 

    So what you have demonstrated is (most likely) just some kind of binary data. Where they come from is only you who know.

      Thanks to all. Understand the problem itself. Used:

       data.decode("utf-8") 

      and got a readable output