使用script 录制终端会话


转载时请务必以超链接形式标明文章 原始出处和作者信息及本版权声明
链接:http://www.dbasky.net/archives/2009/04/script.html
       当我们用命令管理linux下的Oracle数据库时,是否为了记录我们所用到的命令而烦恼过呢?,当然我们可以借助第三方工具或者其它办法,是否有更加方便的方法呢?答案我们今天要讲linux下的script工具。
     通常情况下,script和scriptreplay在linux都默认安装的。如果没有可以到util-linux-ng下载安装,安装方法很简单,只需要把文件下下来然后COPY到/usr/bin/目录下即可。现在我们将当前终端会话中的所有输入输出结果捕获到一个指定的文件里。
看一下这个工具的man输出:
centos-> which script
/usr/bin/script
centos-> man script
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 timeing 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 indi-
             cates how many characters were output this time. This informa-
             tion can be used to replay typescripts with realistic typing 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 type-
     script file.  Script works best with commands 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), replay(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
接下来通过一个例子来说明下:
centos-> script
Script started, file is typescript
centos-> sqlplus " /as sysdba"

SQL*Plus: Release 11.1.0.7.0 - Production on Thu Mar 12 19:48:30 2009

Copyright (c) 1982, 2008, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area  849530880 bytes
Fixed Size                  1316684 bytes
Variable Size             641730740 bytes
Database Buffers          201326592 bytes
Redo Buffers                5156864 bytes
Database mounted.
Database opened.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
centos-> exit
exit
Script done, file is typescript
现在我们来验证下typescript中记录的内容是否是我们输入的内容:
centos-> less typescript
Script started on 2009年03月12日 星期四 19时48分23秒
centos-> sqlplus " /as sysdba"

SQL*Plus: Release 11.1.0.7.0 - Production on Thu Mar 12 19:48:30 2009

Copyright (c) 1982, 2008, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area  849530880 bytes
Fixed Size                  1316684 bytes
Variable Size             641730740 bytes
Database Buffers          201326592 bytes
Redo Buffers                5156864 bytes
Database mounted.
Database opened.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Produ
ction
With the Partitioning, OLAP, Data Mining and Real Application Testing options
centos-> ^MESC[Kcentos-> exit
exit

Script done on 2009年03月12日 星期四 19时49分31秒
其实script的作用不单单就这样子,在linuxinsight中有详细的介绍和说明。

录制会话:

script -t 2> text.timing -a text.session

输入'exit'结束录制。

播放会话:

scriptreplay text.timing text.session

会话和时序文件都是可读的,如果不需要播放,只需:

srcipt -a text.txt

当发现录制和播放都需要两个文件(会话和时序),有点麻烦。因此写了个名为script.sh的bash脚 本,只产生一个文件,录制和播放都比较方便。

录制:

./script.sh -r

播放:

./script.sh -p

获得script.sh并尝试,上传文件后需chmod +x使该脚本可执行。

可以下载我的输出文件-test,并用script.sh播放。

./script.sh -p test

发表评论

Close