重新播放終端會話的所有操作
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
# 重新播放終端內容,播放快進速度為 1 ,命令之間最大延時為 2 秒
scriptreplay -d 1 -m 2 -t time.file -s command.log
記錄終端內容到文件
zfb@localhost:~$ script -t 2>time.file -a -f command.log
Script started, file is command.log
zfb@localhost:~$ echo "hello, world"
hello, world
zfb@localhost:~$ echo $(date "+%Y-%m-%d %H:%M:%S")
2020-12-23 20:48:46
zfb@localhost:~$ echo "Bye"
Bye
zfb@localhost:~$ ls -al
total 20
drwxr-xr-x 2 zfb zfb 4096 Dec 23 20:48 .
drwxr-xr-x 37 zfb zfb 4096 Dec 23 20:49 ..
-rw-r--r-- 1 zfb zfb 0 Dec 23 19:03 a.txt
-rw-r--r-- 1 zfb zfb 12 Dec 23 19:04 b.txt
-rw-r--r-- 1 zfb zfb 2744 Dec 23 20:49 command.log
-rw-r--r-- 1 zfb zfb 790 Dec 23 20:49 time.file
zfb@localhost:~$ exit
Script done, file is command.log
zfb@localhost:~$
重新播放終端內容
zfb@localhost:~$ scriptreplay -d 1 -m 2 -t time.file -s command.log
zfb@localhost:~$ echo "hello, world"
hello, world
zfb@localhost:~$ echo $(date "+%Y-%m-%d %H:%M:%S")
2020-12-23 20:48:46
zfb@localhost:~$ echo "Bye"
Bye
zfb@localhost:~$ ls -al
total 20
drwxr-xr-x 2 zfb zfb 4096 Dec 23 20:48 .
drwxr-xr-x 37 zfb zfb 4096 Dec 23 20:49 ..
-rw-r--r-- 1 zfb zfb 0 Dec 23 19:03 a.txt
-rw-r--r-- 1 zfb zfb 12 Dec 23 19:04 b.txt
-rw-r--r-- 1 zfb zfb 2744 Dec 23 20:49 command.log
-rw-r--r-- 1 zfb zfb 790 Dec 23 20:49 time.file
zfb@localhost:~$ exit
zfb@localhost:~$
其中,只有命令scriptreplay -d 1 -m 2 -t time.file -s command.log
是用戶輸入,其他均為自動呈現(且視覺效果與真實用戶的操作一致)。通過查看上面輸出的時間2020-12-23 20:48:46
,可以證明,這是重新播放當時的記錄,而非重新執行一遍命令。也就是說,可以把time.file
和command.log
文件移動到任意一臺支持scriptreplay
命令的機器上,都可以動態重現命令輸入與終端回顯。