重新播放终端会话的所有操作

scriptreplay 用于在终端中,根据 script 命令记录的终端数据文件和时间日志文件,重现当时用户的所有操作和命令的输出信息。简而言之,重现播放当时终端会话发生的一切信息,而不是重新运行一遍命令。例如,用户当时在输入某条命令时,字符的键入和删除也都会被重现。非常适合用于教程演示场合。而且,在机器 A 上面使用 script 命令记录终端操作,可以在机器 B 上面使用 scriptreplay 命令重新播放。
scriptreplay [options] [-t] timingfile [typescript [divisor]]
选项
-t, --timing file # 记录时间日志的文件名称
-s, --typescript file # 记录终端数据信息的日志文件名称
-d, --divisor number # 表示倍速播放,把时间日志文件记录的时间间隔都除以 number
# -d 2 表示播放速度是原始输入单条命令的速度的两倍,-d 0.1 表示播放单条命令的速度减慢 10 倍
-m, --maxdelay number # 表示命令之间的最大延迟时间(单位是秒)
# -m 2 表示 command.log 中存放的两条命令之间的间隔时间如果大于两秒,则按两秒执行播放
-V, --version # 显示版本信息并退出
-h, --help # 显示帮助文本并退出
时间日志文件:存储时间日志信息的文件名称
终端数据文件:存储终端数据信息的文件名称
scriptreplay time.file command.log
scriptreplay -d 1 -m 2 -t time.file -s command.log
记录终端内容到文件
[root@master1 /tmp]# script -t 2>time.file -a -f command.log
Script started, file is command.log
[root@master1 /tmp]#cd /opt/ser^C
[root@master1 /tmp]#exit
Script done, file is command.log
[root@master1 /tmp]#
[root@master1 /tmp]#
[root@master1 /tmp]#
[root@master1 /tmp]#rm -rf command.log time.file
[root@master1 /tmp]# script -t 2>time.file -a -f command.log
Script started, file is command.log
[root@master1 /tmp]#cd /opt/ansible/
[root@master1 /opt/ansible]#ll -ths
total 8.0K
0 drwxr-xr-x 4 root root 75 Jul 16 14:09 chapt7
0 drwxr-xr-x 3 root root 50 Jul 16 13:29 chapt6
0 drwxr-xr-x 2 root root 230 Jul 15 17:00 chapt5
4.0K -rw-r--r-- 1 root root 876 Jul 15 10:51 ssh-config.yaml
0 drwxr-xr-x 10 root root 172 Jul 15 08:40 first_role
4.0K -rw-r--r-- 1 root root 130 Jul 12 15:45 var.yaml
[root@master1 /opt/ansible]#cat var.yaml
- hosts: k8s-master
remote_user: root
tasks:
- name: create log file
file: name=/tmp/{{ ansible_fqdn }} state=touch
[root@master1 /opt/ansible]#kubectl top nodes
Error from server (ServiceUnavailable): the server is currently unable to handle the request (get nodes.metrics.k8s.io)
[root@master1 /opt/ansible]#kubectl get po -n nexus
^C
[root@master1 /opt/ansible]#ok
bash: ok: command not found
[root@master1 /opt/ansible]#echo bye
bye
重新播放终端内容
[root@master1 /tmp]#scriptreplay -d 2 -t time.file -s command.log
[root@master1 /tmp]#cd /opt/ansible/
[root@master1 /opt/ansible]#ll -ths
total 8.0K
0 drwxr-xr-x 4 root root 75 Jul 16 14:09 chapt7
0 drwxr-xr-x 3 root root 50 Jul 16 13:29 chapt6
0 drwxr-xr-x 2 root root 230 Jul 15 17:00 chapt5
4.0K -rw-r--r-- 1 root root 876 Jul 15 10:51 ssh-config.yaml
0 drwxr-xr-x 10 root root 172 Jul 15 08:40 first_role
4.0K -rw-r--r-- 1 root root 130 Jul 12 15:45 var.yaml
[root@master1 /opt/ansible]#cat var.yaml
- hosts: k8s-master
remote_user: root
tasks:
- name: create log file
file: name=/tmp/{{ ansible_fqdn }} state=touch
[root@master1 /opt/ansible]#kubectl top nodes
Error from server (ServiceUnavailable): the server is currently unable to handle the request (get nodes.metrics.k8s.io)
[root@master1 /opt/ansible]#kubectl get po -n nexus
^C
[root@master1 /opt/ansible]#ok
bash: ok: command not found
[root@master1 /opt/ansible]#echo bye
bye

其中,只有命令scriptreplay -d 1 -m 2 -t time.file -s command.log是用户输入,其他均为自动呈现(且视觉效果与真实用户的操作一致)。通过查看上面输出的时间2020-12-23 20:48:46,可以证明,这是重新播放当时的记录,而非重新执行一遍命令。也就是说,可以把time.file和command.log文件移动到任意一台支持scriptreplay命令的机器上,都可以动态重现命令输入与终端回显。