目前linux终端回放回话的工具常见的就是asciinema和script了, 这两种工具都有那种类似于视频回放的效果。虽然这样做的代价是录制过程中需要占用一定的cpu资源以及录制后可能会因为视频文件太大而不太好在网络之间传送与分享从而交换信息,(或许这类工具的创造初衷并不一定就是要将所有的东西都录进去),将终端操作录制下来,并具有回话功能, 有助于系统操作的审计, 利于系统安全保护 。
asciinema是Linux系统下一款"高级"终端会话记录和回放的神器, 它是一个在终端下非常棒的录制分享软件,基于文本的录屏工具,对终端输入输出进行捕捉, 然后以文本的形式来记录和回放!这使其拥有非常炫酷的特性:在播放过程中随时可以暂停, 然后对"播放器"中的文本进行复制或者其它操作!并且它支持各个操作系统(除了windows之外,目前还不支持windows)。
asciinema 可以让你轻松记录终端会话,并在终端和网页浏览器中重播它们. 之所以说asciinema比script略微技高一筹, 那是因为它们在处理方式上不同: 1) asciinema略显得"高级"和智能的是,asciinema录制与播放都是使用的同一个工具和文件。 不仅如此,它还可以播放来自网络的会话文件。也不需要另外的文件来记录时间序列,回放时也不需要使用另外的工具。 script可以借助于管道来同步显示输出,而asciinama做不到这一点。 2) asciinema是一个用python实现的程序,因此它的安装很简单,大多数较新的Linux发行都在官方源包含了这个工具,只需要使用包管理器就能直接安装它。 如果没有在官方源中找到这个工具,asciinama网站上已经提供了较详细的安装教程,git上也提供另外的安装方法。 3) script 录制的终端会话不仅需要用两个文件来存放,而且还需要单独的工具 scriptreplay 进行回放。它也不能存放到网站上。 4) asciinema 在录制与播放时使用的都是同一个工具和文件,而且还不需要有另外的文件来记录时间序列,同时回放时也不需要使用另外的工具。不仅如此,它还可以播放来自 asciinema 网站上的会话文件。 5) 不过,script 却有一个功能是 asciinema 所不具有的,那就是它可以借助于管道来同步显示输出。
asciinema安装方式: https://github.com/asciinema/asciinema/blob/master/README.md https://asciinema.org/docs/installation#installing-on-linux
======================下面就asciinema的安装和使用说明进行梳理=======================
一. asciinema 安装
1) 安装Python3环境 (和默认的python版本共存)
[root@localhost ~]# python -V
Python 2.6.6
[root@localhost ~]# yum -y install xz epel-release zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz
[root@localhost src]# tar -vxf Python-3.5.3.tar.xz
[root@localhost src]# cd Python-3.5.3
[root@localhost Python-3.5.3]# ./configure --prefix=/usr/local/python3
[root@localhost Python-3.5.3]# make && make install
从 Python 3.4 开始就已经自带了pip和easy_install(setuptools 包带的命令) 包管理命令,可以在 /usr/local/python3/bin/ 目录下看到这些安装的扩展包:
[root@localhost Python-3.5.3]# /usr/local/python3/bin/python3 -V
Python 3.5.3
[root@localhost Python-3.5.3]# /usr/local/python3/bin/pip3 -V
创建软链接
[root@localhost ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@localhost ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
查看版本
[root@localhost ~]# python -V
Python 2.6.6
[root@localhost ~]# python3 -V
Python 3.5.3
[root@localhost ~]# pip3 -V
pip 9.0.1 from /usr/local/python3/lib/python3.5/site-packages (python 3.5)
2) 安装asciinema
[root@localhost ~]# pip3 install asciinema
[root@localhost ~]# find / -name asciinema
/usr/local/python3/lib/python3.5/site-packages/asciinema
/usr/local/python3/bin/asciinema
[root@localhost ~]# ln -s /usr/local/python3/bin/asciinema /usr/bin/asciinema
[root@localhost ~]# asciinema --version
asciinema 2.0.1
查看asciinema帮助信息
[root@localhost ~]# asciinema --help
usage: asciinema [-h] [--version] {rec,play,cat,upload,auth} ...
Record and share your terminal sessions, the right way.
positional arguments:
{rec,play,cat,upload,auth}
rec Record terminal session # 记录终端会话
play Replay terminal session # 播放重播终端会话
cat Print full output of terminal session # 打印终端会话的全部输出
upload Upload locally saved terminal session to asciinema.org #上传本地保存的终端会话到asciinema.org
auth Manage recordings on asciinema.org account # 管理asciinema.org帐户上的记录
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit #显示版本号
示例用法:
记录终端并将其上传到asciinema.org
# asciinema rec
将终端记录到本地文件
# asciinema rec demo.cast
记录终端并将其上传到asciinema.org,指定标题:"my aslog1"
# asciinema rec -t "my aslog1"
将终端记录到本地文件,将空闲时间限制到最大2.5秒
# asciinema rec -i 2.5 demo.cast
从本地文件重放终端记录
# asciinema play demo.cast
重放托管在asciinema.org上的终端记录
# asciinema play https://asciinema.org/a/difqlgx86ym6emrmd8u62yqu8
打印记录的会话的全部输出
# asciinema cat demo.cast
二. asciinema的使用
asciinema 比起 script 来说简单的太多了, 仅仅需要打开一个终端窗口运行"asciinema rec" 命令将回话上传到asciinema.org网站 或者运行 "asciinema rec local-file" 将会话保存到服务器本地local-file文件即可! ( 然而使用script 时, 可能因为 script 录制时产生的时序信息是以标准错误产生的,如果不将它们单独重定向到一个文件中(后来才知道可以使用长参数避免这个问题),它们不仅会发送到终端窗口上而让终端一片混乱,还会导致 script 录制的终端会话 scriptreplay 无法回放) !