如何使用“script”和“scriptreplay”命令记录和重播 Linux 终端会话
在本指南中,我们将了解如何在 Linux 中使用 script 和 scriptreplay 命令,它们可以帮助您在给定的时间内记录命令及其在终端上打印的输出会议。
历史命令是一个很棒的命令行实用程序,可以帮助用户存储以前使用的命令,但它不存储命令的输出。
不要错过: Showterm.io – Linux 终端录制工具
不要错过: 8 个最佳 Linux 桌面屏幕录像机
因此,script 命令可以派上用场,为您提供强大的功能,帮助您将终端上打印的所有内容记录到 log_file 中。如果您想从log_file查看历史记录中的命令输出,您可以稍后参考该文件。
您还可以通过使用计时信息重播使用 scriptreplay 命令录制的命令。
如何使用 script 命令录制 Linux 终端
script 命令将终端活动存储在可由用户命名的日志文件中,当用户未提供名称时,将使用默认文件名 typescript 。
脚本命令的基本语法
script [options] - -timing=timing_file log_filename
要开始记录 Linux 终端,请输入脚本并添加日志文件名,如图所示。
tecmint@tecmint ~ $ script history_log.txt
Script started, file is history_log.txt
要停止脚本,请输入exit,然后按[Enter]。
tecmint@tecmint ~ $ exit
Script done, file is history_log.txt
如果脚本无法写入指定的日志文件,则会显示错误。
例如,在下面的输出中,文件 typescript 的权限不允许任何用户或组读取、写入和执行该文件。当您在没有日志文件名的情况下运行脚本命令时,它会尝试写入默认文件,typescript,因此显示错误。
tecmint@tecmint ~ $ ls -l typescript
--------- 1 ubuntu ubuntu 144 Sep 15 00:00 typescript
tecmint@tecmint ~ $ script
script: open failed: typescript: Permission denied
Terminated
使用脚本命令的示例
在下面的示例中,我将日志文件命名为 script.log,您可以为您的文件指定不同的名称。
tecmint@tecmint ~ $ script script.log
现在尝试执行一些命令,以允许脚本记录终端上执行的命令。
tecmint@tecmint ~ $ cal
September 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
tecmint@tecmint ~ $ w
14:49:40 up 4:06, 2 users, load average: 1.37, 1.56, 1.62
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
tecmint tty8 :0 10:45 4:06m 7:40 0.36s x-session-manager
tecmint pts/5 :0 13:42 4.00s 0.07s 0.00s script script.log
tecmint@tecmint ~ $ uptime
14:49:43 up 4:06, 2 users, load average: 1.37, 1.56, 1.62
tecmint@tecmint ~ $ whoami
tecmint
tecmint@tecmint ~ $ echo 'using script'
using script
tecmint@tecmint ~ $ exit
exit
Script done, file is script.log
现在尝试查看所有记录命令的日志文件“script.log”,当您查看日志时,您会发现脚本还存储换行符和退格键。
tecmint@tecmint ~ $ vi script.log
样本输出
^[[0m^[[255D^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m cal^M
September 2015 ^M
Su Mo Tu We Th Fr Sa ^M
1 2 3 4 5 ^M
6 7 8 9 10 11 12 ^M
13 14 15 ^[[7m16^[[27m 17 18 19 ^M
20 21 22 23 24 25 26 ^M
27 28 29 30 ^M
^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m w^M
14:49:40 up 4:06, 2 users, load average: 1.37, 1.56, 1.62^M
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT^M
tecmint tty8 :0 10:45 4:06m 7:40 0.36s x-session-manager^M
tecmint pts/5 :0 13:42 4.00s 0.07s 0.00s script script.log^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m uptime^M
14:49:43 up 4:06, 2 users, load average: 1.37, 1.56, 1.62^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m whoami^M
tecmint^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m echo ''^Hu'^Hs'^Hi'^Hn'^Hg'^H '^Hs'^Hc'^Hr'^Hi'^Hp'^Ht'^H^M
using script^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m exit^M
exit^M
Script done on Wednesday 16 September 2015 02:49:59 PM IST
~
您可以使用-a选项附加日志文件或打字稿,保留先前的内容。
tecmint@tecmint ~ $ script -a script.log
Script started, file is script.log
tecmint@tecmint ~ $ date
Wed Sep 16 14:59:36 IST 2015
tecmint@tecmint ~ $ pwd
/home/tecmint
tecmint@tecmint ~ $ whereis script
script: /usr/bin/script /usr/bin/X11/script /usr/share/man/man1/script.1.gz
tecmint@tecmint ~ $ whatis script
script (1) - make typescript of terminal session
查看脚本的内容,使用-a选项附加它后记录。
tecmint@tecmint ~ $ vi script.log
样本输出
^[[0m^[[255D^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m date^M
Wed Sep 16 14:59:36 IST 2015^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m pwd^M
/home/tecmint^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m whre^H^[[K^H^[[Kereis script^M
script: /usr/bin/script /usr/bin/X11/script /usr/share/man/man1/script.1.gz^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m whatis script^M
script (1) - make typescript of terminal session^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m vi s^H^[[K^H^[[K^H^[[K^H^[[Kexit^M
exit^M
要记录除交互式 shell 会话之外的单个命令的结果,请使用 -c 选项。
tecmint@tecmint ~ $ script -c 'hostname' script.log
Script started, file is script.log
linux-console.net
Script done, file is script.log
如果您希望脚本以安静模式运行,则可以使用 -q 选项。您不会看到显示脚本正在启动或退出的消息。
tecmint@tecmint ~ $ script -c 'who' -q script.log
tecmint tty8 2015-09-16 10:45 (:0)
tecmint pts/5 2015-09-16 13:42 (:0)
要将计时信息设置为标准错误或文件,请使用-timing选项。当您想要重新显示存储在 log_file 中的输出时,计时信息非常有用。
让我们启动脚本并运行以下命令 w、uptime 和 cal 进行记录。
tecmint@tecmint ~ $ script --timing=time.txt script.log
Script started, file is script.log
tecmint@tecmint ~ $ w
15:09:31 up 4:26, 2 users, load average: 1.38, 1.39, 1.47
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
tecmint tty8 :0 10:45 4:26m 8:15 0.38s x-session-manager
tecmint pts/5 :0 13:42 3.00s 0.09s 0.00s script --timing=time.txt script.log
tecmint@tecmint ~ $ uptime
15:09:36 up 4:26, 2 users, load average: 1.43, 1.40, 1.48
tecmint@tecmint ~ $ cal
September 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
您可以查看上面计时命令的script.log和time.txt文件。
tecmint@tecmint ~ $ vi script.log
样本输出
^[[0m^[[255D^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m w^M
15:12:05 up 4:28, 2 users, load average: 1.31, 1.37, 1.45^M
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT^M
tecmint tty8 :0 10:45 4:28m 8:20 0.38s x-session-manager^M
tecmint pts/5 :0 13:42 5.00s 0.09s 0.00s script --timing=time.txt script.log^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m uptime^M
15:12:07 up 4:28, 2 users, load average: 1.29, 1.36, 1.45^M
^[[01;32mtecmint@tecmint^[[01;34m ~ $^[[00m cal^M
September 2015 ^M
Su Mo Tu We Th Fr Sa ^M
1 2 3 4 5 ^M
6 7 8 9 10 11 12 ^M
13 14 15 ^[[7m16^[[27m 17 18 19 ^M
20 21 22 23 24 25 26 ^M
27 28 29 30 ^M
^M
现在查看 time.txt 文件。
tecmint@tecmint ~ $ vi time.txt
样本输出
0.259669 306
0.037680 829
0.000006 2
0.000002 100
0.000002 2
0.000002 102
0.000019 202
0.000004 2
0.000002 102
0.000015 100
0.000002 2
0.000003 2
0.000002 99
0.000011 2
0.000003 82
...
time.txt 文件有两列,第一列显示自上次显示以来经过的时间,第二列显示本次显示的字符数。
使用手册页和-help来寻求更多选项和使用脚本命令行实用程序的帮助。
使用 scriptreplay 使用计时信息重播脚本
scriptreplay 命令有助于重播 log_file 中由 script 命令记录的信息。
计时信息由与 script 命令一起使用的 -timing=file 选项定义,在本例中,file 为 file.txt 与 script 命令一起使用。
请记住,您需要指定与脚本命令一起使用的log_file。
现在让我们重播我们运行的最后三个命令 w、uptime 和 cal,如下所示。
tecmint@tecmint ~ $ scriptreplay --timing=time.txt script.log
当使用计时信息重播log_file时,将运行记录的命令并在记录时显示原始输出的同时显示其输出。
概括
script 和 scriptreplay 这两个命令易于使用,并且在您需要多次运行同一批命令时很有帮助。它们对于管理只有命令行界面与系统交互的服务器有很大帮助。希望本指南有用,如果您有任何需要添加的内容或在使用它们时遇到挑战,请随时发表评论。