Python, paramiko, invoke_shell and ugly characters

最后都变了- 提交于 2019-12-22 10:49:48

问题


When I run the Python code below:

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.')

I get the desired output mixed with ugly characters:

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.

Can anyone explain me what is going on, and how to get a pretty output instead? Thanks


回答1:


those are terminal color codes used by ls to highlight directories, executable files etc. you can call /bin/ls (or, on some distributions, ls --color=never) explicitly to avoid calling aliases etc. and get un-colored output.

the colors are defined using those cryptic codes like [0m[01;34m. here's how the terminal looks like when ls coloring is enabled:



来源:https://stackoverflow.com/questions/24308400/python-paramiko-invoke-shell-and-ugly-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!