自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在Linux上記錄和重放終端會話活動

系統(tǒng) Linux
我們正在對 Linux 生產(chǎn)環(huán)境中的問題進(jìn)行故障排除,并希望保存所有終端會話活動以供將來參考,因此在這種情況下,script 命令就變得很方便。

[[278386]]

通常,Linux 管理員們都使用 history 命令來跟蹤在先前的會話中執(zhí)行過哪些命令,但是 history 命令的局限性在于它不存儲命令的輸出。在某些情況下,我們要檢查上一個會話的命令輸出,并希望將其與當(dāng)前會話進(jìn)行比較。除此之外,在某些情況下,我們正在對 Linux 生產(chǎn)環(huán)境中的問題進(jìn)行故障排除,并希望保存所有終端會話活動以供將來參考,因此在這種情況下,script 命令就變得很方便。

script 是一個命令行工具,用于捕獲/記錄你的 Linux 服務(wù)器終端會話活動,以后可以使用 scriptreplay 命令重放記錄的會話。在本文中,我們將演示如何安裝 script 命令行工具以及如何記錄 Linux 服務(wù)器終端會話活動,然后,我們將看到如何使用 scriptreplay 命令來重放記錄的會話。

安裝 script 工具

在 RHEL 7/ CentOS 7 上安裝 script 工具

script 命令由 RPM 包 util-linux 提供,如果你沒有在你的 CentOS 7 / RHEL 7 系統(tǒng)上安裝它,運行下面的 yum 安裝它:

  1. [root@linuxtechi ~]# yum install util-linux -y

在 RHEL 8 / CentOS 8 上安裝 script 工具

運行下面的 dnf 命令來在 RHEL 8 / CentOS 8 上安裝 script 工具:

  1. [root@linuxtechi ~]# dnf install util-linux -y

在基于 Debian 的系統(tǒng)(Ubuntu / Linux Mint)上安裝 script 工具

運行下面的 apt-get 命令來安裝 script 工具:

  1. root@linuxtechi ~]# apt-get install util-linux -y

如何使用 script 工具

直接使用 script 命令,在終端上鍵入 script 命令,然后按回車,它將開始在名為 typescript 的文件中捕獲當(dāng)前的終端會話活動。

  1. [root@linuxtechi ~]# script
  2. Script started, file is typescript
  3. [root@linuxtechi ~]#

要停止記錄會話活動,請鍵入 exit 命令,然后按回車:

  1. [root@linuxtechi ~]# exit
  2. exit
  3. Script done, file is typescript
  4. [root@linuxtechi ~]#

script 命令的語法格式:

  1. ~] # script {options} {file_name}

能在 script 命令中使用的不同選項:

options-script-command

讓我們開始通過執(zhí)行 script 命令來記錄 Linux 終端會話,然后執(zhí)行諸如 w,route -n,df -hfree -h,示例如下所示:

script-examples-linux-server

正如我們在上面看到的,終端會話日志保存在文件 typescript 中:

現(xiàn)在使用 cat / vi 命令查看 typescript 文件的內(nèi)容,

  1. [root@linuxtechi ~]# ls -l typescript
  2. -rw-r--r--. 1 root root 1861 Jun 21 00:50 typescript
  3. [root@linuxtechi ~]#

typescript-file-content-linux

以上內(nèi)容確認(rèn)了我們在終端上執(zhí)行的所有命令都已保存在 typescript 文件中。

在 script 命令中使用定制文件名

假設(shè)我們要使用自定義文件名來執(zhí)行 script 命令,可以在 script 命令后指定文件名。在下面的示例中,我們使用的文件名為 session-log-(當(dāng)前日期時間).txt。

  1. [root@linuxtechi ~]# script sessions-log-$(date +%d-%m-%Y-%T).txt
  2. Script started, file is sessions-log-21-06-2019-01:37:39.txt
  3. [root@linuxtechi ~]#

現(xiàn)在運行該命令并輸入 exit

  1. [root@linuxtechi ~]# exit
  2. exit
  3. Script done, file is sessions-log-21-06-2019-01:37:39.txt
  4. [root@linuxtechi ~]#

附加命令輸出到 script 記錄文件

假設(shè) script 命令已經(jīng)將命令輸出記錄到名為 session-log.txt 的文件中,現(xiàn)在我們想將新會話命令的輸出附加到該文件中,那么可以在 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 記錄文件

假設(shè)我們要捕獲命令的輸出到會話記錄文件,那么使用 -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)實現(xiàn),示例如下所示:

語法格式:

  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 終端會話活動

現(xiàn)在,使用 scriptreplay 命令重放錄制的終端會話活動。

注意:scriptreplay 也由 RPM 包 util-linux 提供。scriptreplay 命令需要時序文件才能工作。

  1. [root@linuxtechi ~]# scriptreplay --timing=timing.txt session.log

上面命令的輸出將如下所示,

記錄所有用戶的 Linux 終端會話活動

在某些關(guān)鍵業(yè)務(wù)的 Linux 服務(wù)器上,我們希望跟蹤所有用戶的活動,這可以使用 script 命令來完成,將以下內(nèi)容放在 /etc/profile 文件中,

  1. [root@linuxtechi ~]# vi /etc/profile
  2. ……………………………………………………
  3. if [ "x$SESSION_RECORD" = "x" ]
  4. then
  5. timestamp=$(date +%d-%m-%Y-%T)
  6. session_log=/var/log/session/session.$USER.$$.$timestamp
  7. SESSION_RECORD=started
  8. export SESSION_RECORD
  9. script -t -f -q 2>${session_log}.timing $session_log
  10. exit
  11. fi
  12. ……………………………………………………

保存文件并退出。

/var/log 文件夾下創(chuàng)建 session 目錄:

  1. [root@linuxtechi ~]# mkdir /var/log/session

給該文件夾指定權(quán)限:

  1. [root@linuxtechi ~]# chmod 777 /var/log/session/
  2. [root@linuxtechi ~]#

現(xiàn)在,驗證以上代碼是否有效。在我正在使用 pkumar 用戶的情況下,登錄普通用戶到 Linux 服務(wù)器:

  1. ~ ] # ssh root@linuxtechi
  2. root@linuxtechi's password:
  3. [root@linuxtechi ~]$ uptime
  4. 04:34:09 up 5:06, 3 users, load average: 0.00, 0.01, 0.05
  5. [root@linuxtechi ~]$ date
  6. Fri Jun 21 04:34:11 EDT 2019
  7. [root@linuxtechi ~]$ free -h
  8. total used free shared buff/cache available
  9. Mem: 3.9G 172M 2.0G 8.6M 1.7G 3.3G
  10. Swap: 3.9G 0B 3.9G
  11. [root@linuxtechi ~]$ id
  12. uid=1001(pkumar) gid=1002(pkumar) groups=1002(pkumar) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
  13. [root@linuxtechi ~]$ whoami
  14. pkumar
  15. [root@linuxtechi ~]$ exit
  16.  
  17. Login as root and view user’s linux terminal session activity
  18.  
  19. [root@linuxtechi ~]# cd /var/log/session/
  20. [root@linuxtechi session]# ls -l | grep pkumar
  21. -rw-rw-r--. 1 pkumar pkumar 870 Jun 21 04:34 session.pkumar.19785.21-06-2019-04:34:05
  22. -rw-rw-r--. 1 pkumar pkumar 494 Jun 21 04:34 session.pkumar.19785.21-06-2019-04:34:05.timing
  23. [root@linuxtechi session]#

Session-output-file-linux

我們還可以使用 scriptreplay 命令來重放用戶的終端會話活動:

  1. [root@linuxtechi session]# scriptreplay --timing session.pkumar.19785.21-06-2019-04\:34\:05.timing session.pkumar.19785.21-06-2019-04\:34\:05

以上就是本教程的全部內(nèi)容,請在下面的評論部分中分享你的反饋和評論。

 

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2014-05-12 10:57:41

TermRecord終端會話

2022-01-27 15:37:34

Asciinema終端會話記錄器開源

2023-11-18 11:43:23

2013-11-07 15:38:59

Linux命令行工具

2018-08-27 08:43:11

Linux終端會話SVG動畫

2017-05-19 13:19:16

LinuxAsciinema終端會話

2022-05-07 09:30:08

watchtailLinux 系統(tǒng)

2020-08-23 21:45:18

Linux終端文件

2023-03-04 13:32:35

Linux創(chuàng)建目錄

2023-08-01 08:26:24

Linux文件目錄

2018-02-26 08:14:20

LinuxDocker容器

2025-02-10 09:20:00

LinuxScreen終端

2022-04-11 15:30:39

LinuxGlowMarkdown

2023-11-25 17:13:49

Linux重命名文件

2020-02-10 09:23:09

LinuxReddit命令

2019-06-03 20:00:09

LinuxCPU溫度GPU溫度

2020-03-27 17:37:45

Linux密碼時效

2018-06-25 13:10:16

Linux復(fù)制重命名

2023-10-15 14:53:22

Arch LinuxYay

2019-06-11 15:46:13

screen命令Linux
點贊
收藏

51CTO技術(shù)棧公眾號