在Linux上記錄和重放終端會話活動(dòng)
通常,Linux 管理員們都使用 history 命令來跟蹤在先前的會話中執(zhí)行過哪些命令,但是 history 命令的局限性在于它不存儲命令的輸出。在某些情況下,我們要檢查上一個(gè)會話的命令輸出,并希望將其與當(dāng)前會話進(jìn)行比較。除此之外,在某些情況下,我們正在對 Linux 生產(chǎn)環(huán)境中的問題進(jìn)行故障排除,并希望保存所有終端會話活動(dòng)以供將來參考,因此在這種情況下,script 命令就變得很方便。
script 是一個(gè)命令行工具,用于捕獲/記錄你的 Linux 服務(wù)器終端會話活動(dòng),以后可以使用 scriptreplay 命令重放記錄的會話。在本文中,我們將演示如何安裝 script 命令行工具以及如何記錄 Linux 服務(wù)器終端會話活動(dòng),然后,我們將看到如何使用 scriptreplay 命令來重放記錄的會話。
安裝 script 工具
在 RHEL 7/ CentOS 7 上安裝 script 工具
script 命令由 RPM 包 util-linux 提供,如果你沒有在你的 CentOS 7 / RHEL 7 系統(tǒng)上安裝它,運(yùn)行下面的 yum 安裝它:
[root@linuxtechi ~]# yum install util-linux -y
在 RHEL 8 / CentOS 8 上安裝 script 工具
運(yùn)行下面的 dnf 命令來在 RHEL 8 / CentOS 8 上安裝 script 工具:
[root@linuxtechi ~]# dnf install util-linux -y
在基于 Debian 的系統(tǒng)(Ubuntu / Linux Mint)上安裝 script 工具
運(yùn)行下面的 apt-get 命令來安裝 script 工具:
root@linuxtechi ~]# apt-get install util-linux -y
如何使用 script 工具
直接使用 script 命令,在終端上鍵入 script 命令,然后按回車,它將開始在名為 typescript 的文件中捕獲當(dāng)前的終端會話活動(dòng)。
[root@linuxtechi ~]# scriptScript started, file is typescript[root@linuxtechi ~]#
要停止記錄會話活動(dòng),請鍵入 exit 命令,然后按回車:
[root@linuxtechi ~]# exitexitScript done, file is typescript[root@linuxtechi ~]#
script 命令的語法格式:
~] # script {options} {file_name}
能在 script 命令中使用的不同選項(xiàng):
options-script-command
讓我們開始通過執(zhí)行 script 命令來記錄 Linux 終端會話,然后執(zhí)行諸如 w,route -n,df -h 和 free -h,示例如下所示:
script-examples-linux-server
正如我們在上面看到的,終端會話日志保存在文件 typescript 中:
現(xiàn)在使用 cat / vi 命令查看 typescript 文件的內(nèi)容,
[root@linuxtechi ~]# ls -l typescript-rw-r--r--. 1 root root 1861 Jun 21 00:50 typescript[root@linuxtechi ~]#
typescript-file-content-linux
以上內(nèi)容確認(rèn)了我們在終端上執(zhí)行的所有命令都已保存在 typescript 文件中。
在 script 命令中使用定制文件名
假設(shè)我們要使用自定義文件名來執(zhí)行 script 命令,可以在 script 命令后指定文件名。在下面的示例中,我們使用的文件名為 session-log-(當(dāng)前日期時(shí)間).txt。
[root@linuxtechi ~]# script sessions-log-$(date +%d-%m-%Y-%T).txtScript started, file is sessions-log-21-06-2019-01:37:39.txt[root@linuxtechi ~]#
現(xiàn)在運(yùn)行該命令并輸入 exit:
[root@linuxtechi ~]# exitexitScript done, file is sessions-log-21-06-2019-01:37:39.txt[root@linuxtechi ~]#
附加命令輸出到 script 記錄文件
假設(shè) script 命令已經(jīng)將命令輸出記錄到名為 session-log.txt 的文件中,現(xiàn)在我們想將新會話命令的輸出附加到該文件中,那么可以在 script 命令中使用 -a 選項(xiàng)。
[root@linuxtechi ~]# script -a sessions-log.txtScript started, file is sessions-log.txt[root@linuxtechi ~]# xfs_info /dev/mapper/centos-rootmeta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2746624 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0 spinodes=0data = bsize=4096 blocks=10986496, imaxpct=25= sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal bsize=4096 blocks=5364, version=2= sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0[root@linuxtechi ~]# exitexitScript done, file is sessions-log.txt[root@linuxtechi ~]#
要查看更新的會話記錄,使用 cat session-log.txt 命令。
無需 shell 交互而捕獲命令輸出到 script 記錄文件
假設(shè)我們要捕獲命令的輸出到會話記錄文件,那么使用 -c 選項(xiàng),示例如下所示:
[root@linuxtechi ~]# script -c "uptime && hostname && date" root-session.txtScript started, file is root-session.txt01:57:40 up 2:30, 3 users, load average: 0.00, 0.01, 0.05linuxtechiFri Jun 21 01:57:40 EDT 2019Script done, file is root-session.txt[root@linuxtechi ~]#
以靜默模式運(yùn)行 script 命令
要以靜默模式運(yùn)行 script 命令,請使用 -q 選項(xiàng),該選項(xiàng)將禁止 script 的啟動(dòng)和完成消息,示例如下所示:
[root@linuxtechi ~]# script -c "uptime && date" -q root-session.txt02:01:10 up 2:33, 3 users, load average: 0.00, 0.01, 0.05Fri Jun 21 02:01:10 EDT 2019[root@linuxtechi ~]#
要將時(shí)序信息記錄到文件中并捕獲命令輸出到單獨(dú)的文件中,這可以通過在 script 命令中傳遞時(shí)序文件(-timing)實(shí)現(xiàn),示例如下所示:
語法格式:
~ ]# script -t <timing-file-name> {file_name}
[root@linuxtechi ~]# script --timing=timing.txt session.logScript started, file is session.log[root@linuxtechi ~]# uptime02:27:59 up 3:00, 3 users, load average: 0.00, 0.01, 0.05[root@linuxtechi ~]# dateFri Jun 21 02:28:02 EDT 2019[root@linuxtechi ~]# free -htotal used free shared buff/cache availableMem: 3.9G 171M 2.0G 8.6M 1.7G 3.3GSwap: 3.9G 0B 3.9G[root@linuxtechi ~]# whoamiroot[root@linuxtechi ~]# exitexitScript done, file is session.log[root@linuxtechi ~]#[root@linuxtechi ~]# ls -l session.log timing.txt-rw-r--r--. 1 root root 673 Jun 21 02:28 session.log-rw-r--r--. 1 root root 414 Jun 21 02:28 timing.txt[root@linuxtechi ~]#
重放記錄的 Linux 終端會話活動(dòng)
現(xiàn)在,使用 scriptreplay 命令重放錄制的終端會話活動(dòng)。
注意:scriptreplay 也由 RPM 包 util-linux 提供。scriptreplay 命令需要時(shí)序文件才能工作。
[root@linuxtechi ~]# scriptreplay --timing=timing.txt session.log
上面命令的輸出將如下所示,
記錄所有用戶的 Linux 終端會話活動(dòng)
在某些關(guān)鍵業(yè)務(wù)的 Linux 服務(wù)器上,我們希望跟蹤所有用戶的活動(dòng),這可以使用 script 命令來完成,將以下內(nèi)容放在 /etc/profile 文件中,
[root@linuxtechi ~]# vi /etc/profile……………………………………………………if [ "x$SESSION_RECORD" = "x" ]thentimestamp=$(date +%d-%m-%Y-%T)session_log=/var/log/session/session.$USER.$$.$timestampSESSION_RECORD=startedexport SESSION_RECORDscript -t -f -q 2>${session_log}.timing $session_logexitfi……………………………………………………
保存文件并退出。
在 /var/log 文件夾下創(chuàng)建 session 目錄:
[root@linuxtechi ~]# mkdir /var/log/session
給該文件夾指定權(quán)限:
[root@linuxtechi ~]# chmod 777 /var/log/session/[root@linuxtechi ~]#
現(xiàn)在,驗(yàn)證以上代碼是否有效。在我正在使用 pkumar 用戶的情況下,登錄普通用戶到 Linux 服務(wù)器:
~ ] # ssh root@linuxtechiroot@linuxtechi's password:[root@linuxtechi ~]$ uptime04:34:09 up 5:06, 3 users, load average: 0.00, 0.01, 0.05[root@linuxtechi ~]$ dateFri Jun 21 04:34:11 EDT 2019[root@linuxtechi ~]$ free -htotal used free shared buff/cache availableMem: 3.9G 172M 2.0G 8.6M 1.7G 3.3GSwap: 3.9G 0B 3.9G[root@linuxtechi ~]$ iduid=1001(pkumar) gid=1002(pkumar) groups=1002(pkumar) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023[root@linuxtechi ~]$ whoamipkumar[root@linuxtechi ~]$ exitLogin as root and view user’s linux terminal session activity[root@linuxtechi ~]# cd /var/log/session/[root@linuxtechi session]# ls -l | grep pkumar-rw-rw-r--. 1 pkumar pkumar 870 Jun 21 04:34 session.pkumar.19785.21-06-2019-04:34:05-rw-rw-r--. 1 pkumar pkumar 494 Jun 21 04:34 session.pkumar.19785.21-06-2019-04:34:05.timing[root@linuxtechi session]#
Session-output-file-linux
我們還可以使用 scriptreplay 命令來重放用戶的終端會話活動(dòng):
[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)容,請?jiān)谙旅娴脑u論部分中分享你的反饋和評論。




















 
 
 











 
 
 
 