如何在python3中将字节控制台转换为字符串?

时间:2018-07-26 15:51:56

标签: python unicode terminal ascii pty

我有python脚本,可以保存ssh会话中的所有数据

import os
import pty

out = open('output.txt', 'w')


def read(fd):
    data = os.read(fd, 1024)
    out.write(data.decode('utf-8', 'ignore'))
    return data


pty.spawn('./script.sh', read)
out.close()
 
#!/usr/bin/env bash 
ssh root@admin 

我想在output.txt中得到类似的东西

root@admin:~# top
top - 15:15:01 up 21 days,  5:00,  1 user,  load average: 0.00, 0.00, 0.00
Tasks: 185 total,   1 running, 184 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.0 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2047952 total,   170368 free,   327540 used,  1550044 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  1373184 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     
    1 root      20   0  185396   6044   4020 S   0.0  0.3   0:23.03 systemd     
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.06 kthreadd    
    3 root      20   0       0      0      0 S   0.0  0.0   0:00.98 ksoftirqd/0 
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:+ 
    7 root      20   0       0      0      0 S   0.0  0.0   3:41.59 rcu_sched   
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh      
    9 root      rt   0       0      0      0 S   0.0  0.0   0:01.01 migration/0 
   10 root      rt   0       0      0      0 S   0.0  0.0   0:06.50 watchdog/0  
   11 root      rt   0       0      0      0 S   0.0  0.0   0:06.96 watchdog/1  
   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.86 migration/1 
   13 root      20   0       0      0      0 S   0.0  0.0   0:02.11 ksoftirqd/1 
   15 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:+ 
   17 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs   
   18 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 netns       
   19 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 perf        
   20 root      20   0       0      0      0 S   0.0  0.0   0:00.00 khungtaskd  
   21 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 writeback   

root@admin:~# exit

但是我明白了

]0;root@admin: ~root@admin:~# ex[K[Ktop
[?1h=[?25l[H[2J(B[mtop - 15:15:01 up 21 days,  5:00,  1 user,  load average: 0.00, 0.00, 0.00(B[m[39;49m(B[m[39;49m[K
Tasks:(B[m[39;49m[1m 185 (B[m[39;49mtotal,(B[m[39;49m[1m   1 (B[m[39;49mrunning,(B[m[39;49m[1m 184 (B[m[39;49msleeping,(B[m[39;49m[1m   0 (B[m[39;49mstopped,(B[m[39;49m[1m   0 (B[m[39;49mzombie(B[m[39;49m(B[m[39;49m[K
%Cpu(s):(B[m[39;49m[1m  0.1 (B[m[39;49mus,(B[m[39;49m[1m  0.0 (B[m[39;49msy,(B[m[39;49m[1m  0.0 (B[m[39;49mni,(B[m[39;49m[1m 99.9 (B[m[39;49mid,(B[m[39;49m[1m  0.0 (B[m[39;49mwa,(B[m[39;49m[1m  0.0 (B[m[39;49mhi,(B[m[39;49m[1m  0.0 (B[m[39;49msi,(B[m[39;49m[1m  0.0 (B[m[39;49mst(B[m[39;49m(B[m[39;49m[K
KiB Mem :(B[m[39;49m[1m  2047952 (B[m[39;49mtotal,(B[m[39;49m[1m   170368 (B[m[39;49mfree,(B[m[39;49m[1m   327540 (B[m[39;49mused,(B[m[39;49m[1m  1550044 (B[m[39;49mbuff/cache(B[m[39;49m(B[m[39;49m[K
KiB Swap:(B[m[39;49m[1m        0 (B[m[39;49mtotal,(B[m[39;49m[1m        0 (B[m[39;49mfree,(B[m[39;49m[1m        0 (B[m[39;49mused.(B[m[39;49m[1m  1373184 (B[m[39;49mavail Mem (B[m[39;49m(B[m[39;49m[K
[K
[7m  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     (B[m[39;49m[K
(B[m    1 root      20   0  185396   6044   4020 S   0.0  0.3   0:23.03 systemd     (B[m[39;49m[K
(B[m    2 root      20   0       0      0      0 S   0.0  0.0   0:00.06 kthreadd    (B[m[39;49m[K
(B[m    3 root      20   0       0      0      0 S   0.0  0.0   0:00.98 ksoftirqd/0 (B[m[39;49m[K
(B[m    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:+ (B[m[39;49m[K
(B[m    7 root      20   0       0      0      0 S   0.0  0.0   3:41.59 rcu_sched   (B[m[39;49m[K
(B[m    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh      (B[m[39;49m[K
(B[m    9 root      rt   0       0      0      0 S   0.0  0.0   0:01.01 migration/0 (B[m[39;49m[K
(B[m   10 root      rt   0       0      0      0 S   0.0  0.0   0:06.50 watchdog/0  (B[m[39;49m[K
(B[m   11 root      rt   0       0      0      0 S   0.0  0.0   0:06.96 watchdog/1  (B[m[39;49m[K
(B[m   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.86 migration/1 (B[m[39;49m[K
(B[m   13 root      20   0       0      0      0 S   0.0  0.0   0:02.11 ksoftirqd/1 (B[m[39;49m[K
(B[m   15 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:+ (B[m[39;49m[K
(B[m   17 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs   (B[m[39;49m[K
(B[m   18 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 netns       (B[m[39;49m[K
(B[m   19 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 perf        (B[m[39;49m[K
(B[m   20 root      20   0       0      0      0 S   0.0  0.0   0:00.00 khungtaskd  (B[m[39;49m[K
(B[m   21 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 writeback   (B[m[39;49m[K[?1l>[25;1H
[?12l[?25h[K]0;root@admin: ~root@admin:~# exit

2 个答案:

答案 0 :(得分:1)

您将看到ANSI转义序列。它们用于控制颜色和光标位置。

在script.sh中使用“ top -b”代替“ top”来删除所有特殊显示字符。

答案 1 :(得分:0)

您可以使用子进程并存储正在运行的Shell脚本的输出。然后,您可以将文本输出到这样的文件中:

import subprocess
output = subprocess.Popen(['./script.sh'], stdout=subprocess.PIPE)
with open('outfile.txt', 'wb') as outfile:
    out, err = output.communicate()
    outfile.write(out.encode('utf-8'))