linux下Script小计

前言:测试服务器直接暴露给研发、测试多角色多人员,使用script可以方便记录各人员操作日志;交付产品,客户服务器不会直接提供给运维人员使用,需要提前录制操作步骤,从而在生产环境中一键执行。

1. 安装

验证环境为 CentOS Linux release 7.2.1511 (Core),已经自带了script命令。如果不可用可以自行安装:

[root@it-no ~]# 
[root@it-no ~]# whereis script
script: /usr/bin/script /usr/share/man/man1/script.1.gz
[root@it-no ~]# 
[root@it-no ~]# rpm -qf /usr/bin/script
util-linux-2.23.2-63.el7.x86_64
[root@it-no ~]#

使用下方命令安装即可(安装完成可以使用script录制屏幕交互,同时也会安装scriptreplay用于演示命令交互):

yum install util-linux* -y

下面查看命令用法:

[root@it-no ~]# 
[root@it-no ~]# script --help

Usage:
 script [options] [file]

Options:
 -a, --append            append the output
 -c, --command <command> run command rather than interactive shell
 -e, --return            return exit code of the child process
 -f, --flush             run flush after each write
     --force             use output file even when it is a link
 -q, --quiet             be quiet
 -t, --timing[=<file>]   output timing data to stderr (or to FILE)
 -V, --version           output version information and exit
 -h, --help              display this help and exit

[root@it-no ~]#
  • 说明:
    • -t 记录时间戳
    • -a 追加模式添加进日志文件
    • -f 每次写入文件刷新 (可以通过tailf xxx.log 实时查看某用户的操作信息)
    • -q 日志记录不提醒用户 (若不加此参数,用户登录后会提示操作被记录)

2. 配置监控

把下方配置信息添加至环境变量 /etc/profile

if [ $UID -ge 0 ] ; then
exec /usr/bin/script -t   2>/opt/logs/scripts/$USER-$UID-`date +%Y%m%d%H%M`.date -a -f -q /opt/logs/scripts/$USER-$UID-`date +%Y%m%d%H%M`.log
fi

使环境变量生效

source /etc/profile
  • 说明:
    • 若监控root以外用户,需修改为 $UID -ge 500
    • 若只监控某一用户,只需要把上一步骤修改后的内容添加至该用户家目录下的~/.bashrc下即可

3. 使用

查看当前用户操作日志记录位置

[root@it-no ~]# w
 15:16:04 up 140 days, 23:30,  4 users,  load average: 0.00, 0.01, 0.07
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    10.3.122.111     14:09    4.00s  0.20s  0.08s /usr/bin/script -t -a -f -q /opt/logs/scripts/root-0-202101051409.log
root     pts/2                     14:09    4.00s  0.22s  0.00s w
[root@it-no ~]#
  • 可直接使用catmorelesstailf等命令查看操作日志
  • 可使用 scriptreplay file.date file.log 按时间顺序播放录制的内容
原文地址:https://www.cnblogs.com/wjlv/p/14236030.html