【问题标题】:How convert bytes console to string in python3?如何在python3中将字节控制台转换为字符串?
【发布时间】:2019-01-03 16:38:49
【问题描述】:

我有一个 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:~# 顶 顶部 - 15:15:01 21 天,5:00,1 个用户,平均负载:0.00、0.00、0.00 任务:共 185 个,运行 1 个,睡眠 184 个,停止 0 个,僵尸 0 个 %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 内存:总计 2047952,170368 免费,327540 已使用,1550044 buff/cache KiB 交换:总共 0 个,免费 0 个,已使用 0 个。 1373184 利用内存 PID 用户 PR NI VIRT RES SHR S %CPU %MEM TIME+ 命令 1 根 20 0 185396 6044 4020 S 0.0 0.3 0:23.03 systemd 2根20 0 0 0 0 S 0.0 0.0 0:00.06 kthreadd 3 根 20 0 0 0 0 S 0.0 0.0 0:00.98 ksoftirqd/0 5 根 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:+ 7 根 20 0 0 0 0 S 0.0 0.0 3:41.59 rcu_sched 8根20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 9 根 rt 0 0 0 0 S 0.0 0.0 0:01.01 迁移/0 10 根 rt 0 0 0 0 S 0.0 0.0 0:06.50 看门狗/0 11 根 rt 0 0 0 0 S 0.0 0.0 0:06.96 看门狗/1 12 根 rt 0 0 0 0 S 0.0 0.0 0:00.86 迁移/1 13 根 20 0 0 0 0 S 0.0 0.0 0:02.11 ksoftirqd/1 15 根 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1:+ 17 根 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs 18根0 -20 0 0 0 S 0.0 0.0 0:00.00 netns 19 根 0 -20 0 0 0 S 0.0 0.0 0:00.00 性能 20 根 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd 21 根 0 -20 0 0 0 S 0.0 0.0 0:00.00 回写 root@admin:~# 退出

但我明白了

]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 [米[39;49米[K 任务:(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/缓存(B[m[39;49m(B[m[39;49m [K KiB 交换:(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 [7m PID 用户 PR NI VIRT RES SHR S %CPU %MEM TIME+ 命令(B[m[39;49m[K (B[m 1 根 20 0 185396 6044 4020 S 0.0 0.3 0:23.03 systemd (B[m[39;49m[K (B[m 2 根 20 0 0 0 0 S 0.0 0.0 0:00.06 kthreadd (B[m[39;49m[K (B[m 3 根 20 0 0 0 0 S 0.0 0.0 0:00.98 ksoftirqd/0 (B[m[39;49m[K (B[m 5 根 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:+ (B[m[39;49m[K (B[m 7 根 20 0 0 0 0 S 0.0 0.0 3:41.59 rcu_sched (B[m[39;49m[K (B[m 8 根 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh (B[m[39;49m[K (B[m 9 根 rt 0 0 0 0 S 0.0 0.0 0:01.01 迁移/0 (B[m[39;49m[K (B[m 10 根 rt 0 0 0 0 S 0.0 0.0 0:06.50 看门狗/0 (B[m[39;49m[K (B[m 11 根 rt 0 0 0 0 S 0.0 0.0 0:06.96 看门狗/1 (B[m[39;49m[K (B[m 12 根 rt 0 0 0 0 S 0.0 0.0 0:00.86 迁移/1 (B[m[39;49m[K (B[m 13 根 20 0 0 0 0 S 0.0 0.0 0:02.11 ksoftirqd/1 (B[m[39;49m[K (B[m 15 根 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1:+ (B[m[39;49m[K (B[m 17 根 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs (B[m[39;49m[K (B[m 18 根 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns (B[m[39;49m[K (B[m 19 根 0 -20 0 0 0 S 0.0 0.0 0:00.00 perf (B[m[39;49m[K (B[m 20 根 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd (B[m[39;49m[K (B[m 21 根 0 -20 0 0 0 S 0.0 0.0 0:00.00 回写 (B[m[39;49m[K[?1l>[25;1H [?12l[?25h[K]0;root@admin:~root@admin:~#exit

【问题讨论】:

    标签: python unicode terminal ascii pty


    【解决方案1】:

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

    在您的 script.sh 中使用“top -b”而不是“top”来删除所有特殊显示字符。

    【讨论】:

    • 对不起,
       #!/usr/bin/env bash ssh root@example 
    【解决方案2】:

    您可以使用子进程并存储您正在运行的 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'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 2016-09-04
      • 2021-06-24
      • 2020-11-04
      • 1970-01-01
      相关资源
      最近更新 更多