使用script记录Linux终端会话
许多系统管理员都知道保留一个包含各种任务、配置改变等活动日志的重要性。对一些组织而言,保留“我做了这件事”或“约翰做了那件事”的简单日志就已足够;但另一些组织则需要记录所有改变。
对终端输出进行复制粘贴可能非常乏味,我们使用一个叫做script的鲜为人知的程序来解决这个问题,它是大多数Linux产品util-linux软件包的一部分。
script记录会话的一切内容:你输入的内容和你看到的内容。它甚至记录颜色;因此如果你的命令提示符或程序输出中包含颜色,script将记录它。
要使用script,简单执行以下命令:
$ script
默认情况下,它向当前目录的typescript文件中写入内容。然后,你输入的一切内容都被记录到那个文件中。要往另一个文件中记录日志,只需使用script/path/to/file命令。
完成记录后,输入exit退出。这个命令将关闭script会话并保存文件。现在你可以使用cat或其它任何程序来检查日志文件。
使用script的缺点在于,它记录所有特殊的字符;因此你输入的文件中将充满控制字符和ANSI转义序列。你可以在script中使用一个非常简单的shell来解决这个问题:
$ SHELL=/bin/sh PS1=”$ ” script
使用script时,不要使用交互式程序或处理窗口的程序,如vior top。它们会破坏会话的输出结果。另外,日志文件会记录你使用的任何命令行程序和你完成一项任务所采取的步骤。如果你需要在脚本中编辑一个文件,考虑退出script会话,然后用script –a(它在旧会话后添加新会话)对文件进行编辑后再重新启动会话。
SCRIPT(1) BSD General Commands Manual SCRIPT(1)
NAME
script – make typescript of terminal session
SYNOPSIS
script [-a] [-c COMMAND] [-f] [-q] [-t] [file]
DESCRIPTION
Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy
record of an interactive session as proof of an assignment, as the typescript file can be printed out later with
lpr(1).
If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is
saved in the file typescript.
Options:
-a Append the output to file or typescript, retaining the prior contents.
-c COMMAND
Run the COMMAND rather than an interactive shell. This makes it easy for a script to capture the output
of a program that behaves differently when its stdout is not a tty.
-f Flush output after each write. This is nice for telecooperation: One person does ‘mkfifo foo; script -f
foo’ and another can supervise real-time what is being done using ‘cat foo’.
-q Be quiet.
-t Output timing data to standard error. This data contains two fields, separated by a space. The first
field indicates how much time elapsed since the previous output. The second field indicates how many
characters were output this time. This information can be used to replay typescripts with realistic typ-
ing and output delays.
The script ends when the forked shell exits (a control-D to exit the Bourne shell (sh(1)), and exit, logout or
control-d (if ignoreeof is not set) for the C-shell, csh(1)).
Certain interactive commands, such as vi(1), create garbage in the typescript file. Script works best with com-
mands that do not manipulate the screen, the results are meant to emulate a hardcopy terminal.
ENVIRONMENT
The following environment variable is utilized by script:
SHELL If the variable SHELL exists, the shell forked by script will be that shell. If SHELL is not set, the
Bourne shell is assumed. (Most shells set this variable automatically).
SEE ALSO
csh(1) (for the history mechanism), scriptreplay(1).
HISTORY
The script command appeared in 3.0BSD.
BUGS
Script places everything in the log file, including linefeeds and backspaces. This is not what the naive user
expects.
Linux July 30, 2000 Linux
近期评论