Python、Paramiko、Invoke_shell和乱码

3
当我运行以下Python代码时:
import workflow
import console
import paramiko
import time

strComputer = 'server.com'
strUser = 'user'
strPwd = 'passwd'

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=strComputer, username=strUser, password=strPwd)

channel = client.invoke_shell()
channel.send('ls\n')
time.sleep(3)
output=channel.recv(2024)
print(output)

#Close the connection
client.close()
print('Connection closed.')

我得到了带有难看字符的期望输出:
Last login: Thu Jun 19 23:37:55 2014 from 192.168.0.10

ls
user@server:~$ ls
[0m[01;34mbin[0m                                         Rplots1.pdf
[01;32mbtsync[0m                                      Rplots.pdf
btsync.conf~                                [01;31mrstudio-server-0.95.265-amd64.deb[0m
[01;31mbtsync_glibc23_x64.tar[0m                      screen.vba
[01;34mbudget[0m                                      [01;34mshiny[0m
[01;3
Connection closed.

有人能为我解释一下发生了什么,以及如何获得漂亮的输出吗? 谢谢。
1个回答

7
这些是终端颜色代码,用于ls突出显示目录、可执行文件等。您可以显式调用/bin/ls(或在某些发行版上,ls --color=never)以避免调用别名等并获得未着色的输出。
这些颜色使用类似[0m[01;34m的加密代码定义。以下是启用ls着色时终端的外观:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接