加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

在Linux上记录和重放终端会话活动

发布时间:2019-10-12 00:44:40 所属栏目:Windows 来源:Pradeep Kumar
导读:副标题#e# 通常,Linux 管理员们都使用 history 命令来跟踪在先前的会话中执行过哪些命令,但是 history 命令的局限性在于它不存储命令的输出。在某些情况下,我们要检查上一个会话的命令输出,并希望将其与当前会话进行比较。除此之外,在某些情况下,我们

假设 script 命令已经将命令输出记录到名为 session-log.txt 的文件中,现在我们想将新会话命令的输出附加到该文件中,那么可以在 script 命令中使用 -a 选项。

  1. [root@linuxtechi ~]# script -a sessions-log.txt
  2. Script started, file is sessions-log.txt
  3. [root@linuxtechi ~]# xfs_info /dev/mapper/centos-root
  4. meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2746624 blks
  5. = sectsz=512 attr=2, projid32bit=1
  6. = crc=1 finobt=0 spinodes=0
  7. data = bsize=4096 blocks=10986496, imaxpct=25
  8. = sunit=0 swidth=0 blks
  9. naming =version 2 bsize=4096 ascii-ci=0 ftype=1
  10. log =internal bsize=4096 blocks=5364, version=2
  11. = sectsz=512 sunit=0 blks, lazy-count=1
  12. realtime =none extsz=4096 blocks=0, rtextents=0
  13. [root@linuxtechi ~]# exit
  14. exit
  15. Script done, file is sessions-log.txt
  16. [root@linuxtechi ~]#

要查看更新的会话记录,使用 cat session-log.txt 命令。

无需 shell 交互而捕获命令输出到 script 记录文件

假设我们要捕获命令的输出到会话记录文件,那么使用 -c 选项,示例如下所示:

  1. [root@linuxtechi ~]# script -c "uptime && hostname && date" root-session.txt
  2. Script started, file is root-session.txt
  3. 01:57:40 up 2:30, 3 users, load average: 0.00, 0.01, 0.05
  4. linuxtechi
  5. Fri Jun 21 01:57:40 EDT 2019
  6. Script done, file is root-session.txt
  7. [root@linuxtechi ~]#

以静默模式运行 script 命令

要以静默模式运行 script 命令,请使用 -q 选项,该选项将禁止 script 的启动和完成消息,示例如下所示:

  1. [root@linuxtechi ~]# script -c "uptime && date" -q root-session.txt
  2. 02:01:10 up 2:33, 3 users, load average: 0.00, 0.01, 0.05
  3. Fri Jun 21 02:01:10 EDT 2019
  4. [root@linuxtechi ~]#

要将时序信息记录到文件中并捕获命令输出到单独的文件中,这可以通过在 script 命令中传递时序文件(-timing)实现,示例如下所示:

语法格式:

  1. ~ ]# script -t <timing-file-name> {file_name}
  1. [root@linuxtechi ~]# script --timing=timing.txt session.log
  2. Script started, file is session.log
  3. [root@linuxtechi ~]# uptime
  4. 02:27:59 up 3:00, 3 users, load average: 0.00, 0.01, 0.05
  5. [root@linuxtechi ~]# date
  6. Fri Jun 21 02:28:02 EDT 2019
  7. [root@linuxtechi ~]# free -h
  8. total used free shared buff/cache available
  9. Mem: 3.9G 171M 2.0G 8.6M 1.7G 3.3G
  10. Swap: 3.9G 0B 3.9G
  11. [root@linuxtechi ~]# whoami
  12. root
  13. [root@linuxtechi ~]# exit
  14. exit
  15. Script done, file is session.log
  16. [root@linuxtechi ~]#
  17. [root@linuxtechi ~]# ls -l session.log timing.txt
  18. -rw-r--r--. 1 root root 673 Jun 21 02:28 session.log
  19. -rw-r--r--. 1 root root 414 Jun 21 02:28 timing.txt
  20. [root@linuxtechi ~]#

重放记录的 Linux 终端会话活动

现在,使用 scriptreplay 命令重放录制的终端会话活动。

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读