Linu使用script记录终端会话的所有操作,包括在终端的所有操作和命令的输出信息
介绍
script 生成如下两个文件相关信息:
- 终端数据文件:在终端的所有- 操作、- 命令输出等信息会被以- raw格式存储在- 日志文件
- 时间日志文件:命令的时间信息会被储存为日志文件
在使用 script 命令时,输入 exit 或者使用快捷键 Ctrl + D 停止记录
使用
安装
apt install bsdutils -y
help
  
  
    
      $ script --help
Usage:
 script [options] [file]
Make a typescript of a terminal session.
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
 -o, --output-limit <size>     terminate if output files exceed size
 -q, --quiet                   be quiet
 -t[<file>], --timing[=<file>] output timing data to stderr or to FILE
 -h, --help                    display this help
 -V, --version                 display version
For more details see script(1).
     
   
 
手动启动示例
# 不带任何参数时,默认会在当前目录创建终端数据文件,名称为 typescript
script
# 指定终端数据文件文件
script command.log
# 指定终端数据文件文件,并指定时间日志文件为 time-file
script -t 2>command.timing command.log
# 制定命令
script -qa command.log -c "ping -c 1 qq.com"
自动记录服务器用户会话操作
Linux 中自动记录所有用户执行的命令
sudo mkdir -p /var/log/script-records/
sudo chmod 733 /var/log/script-records/
if [ $UID -ge 0 ]
then
    exec /usr/bin/script -t 2>/var/log/script-records/$USER-$UID-`date +%Y%m%d`.timing -a -f -q /var/log/script-records/$USER-$UID-`date +%Y%m%d`.log
fi
参数说明:
- -a追加方式写入文件,防止记录被覆盖删除
- -f终端内容发生变动时,立即写入文件
- -q采用安静模式,进入和退出时,不显示任何提示信息