目前linux終端回放工具常見的就是asciinema和script了, 這兩種工具都有那種類似於視頻回放的效果。雖然這樣做的代價是錄制過程中需要占用一定的cpu資源以及錄制后可能會因為視頻文件太大而不太好在網絡之間傳送與分享從而交換信息,(或許這類工具的創造初衷並不一定就是要將所有的東西都錄進去),將終端操作錄制下來,並具有回話功能, 有助於系統操作的審計, 利於系統安全保護 。
asciinema是Linux系統下一款"高級"終端會話記錄和回放的神器, 它是一個在終端下非常棒的錄制分享軟件,基於文本的錄屏工具,對終端輸入輸出進行捕捉, 然后以文本的形式來記錄和回放!這使其擁有非常炫酷的特性:在播放過程中隨時可以暫停, 然后對"播放器"中的文本進行復制或者其它操作!並且它支持各個操作系統(除了windows之外,目前還不支持windows)。
asciinema 可以讓你輕松記錄終端會話,並在終端和網頁瀏覽器中重播它們. 之所以說asciinema比script略微技高一籌, 那是因為它們在處理方式上不同:
1) asciinema略顯得"高級"和智能的是,asciinema錄制與播放都是使用的同一個工具和文件。 不僅如此,它還可以播放來自網絡的會話文件。也不需要另外的文件來記錄時間序列,回放時也不需要使用另外的工具。 script可以借助於管道來同步顯示輸出,而asciinama做不到這一點。
2) asciinema是一個用python實現的程序,因此它的安裝很簡單,大多數較新的Linux發行都在官方源包含了這個工具,只需要使用包管理器就能直接安裝它。 如果沒有在官方源中找到這個工具,asciinama網站上已經提供了較詳細的安裝教程,git上也提供另外的安裝方法。
3) script 錄制的終端會話不僅需要用兩個文件來存放,而且還需要單獨的工具 scriptreplay 進行回放。它也不能存放到網站上。
4) asciinema 在錄制與播放時使用的都是同一個工具和文件,而且還不需要有另外的文件來記錄時間序列,同時回放時也不需要使用另外的工具。不僅如此,它還可以播放來自 asciinema 網站上的會話文件。
5) 不過,script 卻有一個功能是 asciinema 所不具有的,那就是它可以借助於管道來同步顯示輸出。
asciinema安裝方式:
https://github.com/asciinema/asciinema/blob/master/README.md
https://asciinema.org/docs/installation#installing-on-linux
===================asciinema的安裝和使用梳理====================
一. asciinema 安裝
1) 安裝Python3環境 (和默認的python版本共存)
[root@localhost ~]# python -V
Python 2.6.6
[root@localhost ~]# yum -y install xz epel-release zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz
[root@localhost src]# tar -vxf Python-3.5.3.tar.xz
[root@localhost src]# cd Python-3.5.3
[root@localhost Python-3.5.3]# ./configure --prefix=/usr/local/python3
[root@localhost Python-3.5.3]# make && make install
從 Python 3.4 開始就已經自帶了pip和easy_install(setuptools 包帶的命令) 包管理命令,可以在 /usr/local/python3/bin/ 目錄下看到這些安裝的擴展包:
[root@localhost Python-3.5.3]# /usr/local/python3/bin/python3 -V
Python 3.5.3
[root@localhost Python-3.5.3]# /usr/local/python3/bin/pip3 -V
創建軟鏈接
[root@localhost ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@localhost ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
查看版本
[root@localhost ~]# python -V
Python 2.6.6
[root@localhost ~]# python3 -V
Python 3.5.3
[root@localhost ~]# pip3 -V
pip 9.0.1 from /usr/local/python3/lib/python3.5/site-packages (python 3.5)
2) 安裝asciinema
[root@localhost ~]# pip3 install asciinema
[root@localhost ~]# find / -name asciinema
/usr/local/python3/lib/python3.5/site-packages/asciinema
/usr/local/python3/bin/asciinema
[root@localhost ~]# ln -s /usr/local/python3/bin/asciinema /usr/bin/asciinema
[root@localhost ~]# asciinema --version
asciinema 2.0.1
查看asciinema幫助信息
[root@localhost ~]# asciinema --help
usage: asciinema [-h] [--version] {rec,play,cat,upload,auth} ...
Record and share your terminal sessions, the right way.
positional arguments:
{rec,play,cat,upload,auth}
rec Record terminal session # 記錄終端會話
play Replay terminal session # 播放重播終端會話
cat Print full output of terminal session # 打印終端會話的全部輸出
upload Upload locally saved terminal session to asciinema.org #上傳本地保存的終端會話到asciinema.org
auth Manage recordings on asciinema.org account # 管理asciinema.org帳戶上的記錄
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit #顯示版本號
示例用法:
記錄終端並將其上傳到asciinema.org
# asciinema rec
將終端記錄到本地文件
# asciinema rec demo.cast
記錄終端並將其上傳到asciinema.org,指定標題:"my aslog1"
# asciinema rec -t "my aslog1"
將終端記錄到本地文件,將空閑時間限制到最大2.5秒
# asciinema rec -i 2.5 demo.cast
從本地文件重放終端記錄
# asciinema play demo.cast
重放托管在asciinema.org上的終端記錄
# asciinema play https://asciinema.org/a/difqlgx86ym6emrmd8u62yqu8
打印記錄的會話的全部輸出
# asciinema cat demo.cast
二. asciinema的使用
asciinema 比起 script 來說簡單的太多了, 僅僅需要打開一個終端窗口運行"asciinema rec" 命令將回話上傳到asciinema.org網站 或者運行 "asciinema rec local-file" 將會話保存到服務器本地local-file文件即可! ( 然而使用script 時, 可能因為 script 錄制時產生的時序信息是以標准錯誤產生的,如果不將它們單獨重定向到一個文件中(后來才知道可以使用長參數避免這個問題),它們不僅會發送到終端窗口上而讓終端一片混亂,還會導致 script 錄制的終端會話 scriptreplay 無法回放) !
[root@localhost ~]# asciinema --version
asciinema 2.0.1
1) 將終端會話錄制下來, 並上傳到asciinema.org網站 ("asciinema rec")
[root@localhost ~]# asciinema rec #執行該命令, 回車就已經進入到asciinema 終端會話錄制過程中了
asciinema: recording asciicast to /tmp/tmpyvq2q5os-ascii.cast
asciinema: press <ctrl-d> or type "exit" when you're done
[root@localhost ~]# ll /etc/hosts #這是已經記錄在asciinema終端會話的操作
-rw-r--r-- 1 root root 158 Dec 25 20:36 /etc/hosts
[root@localhost ~]# echo "12313"
12313
[root@localhost ~]# exit #退出asciinema 會話錄制
asciinema: recording finished
asciinema: press <enter> to upload to asciinema.org, <ctrl-c> to save locally #按"enter"鍵, 就會將會話錄制上傳到asciinema.org網站, 通過下面的url可以查看
View the recording at: #按"ctrl+c"鍵, 就會將會話錄制默認保存到本地的/tmp/tmpyvq2q5os-ascii.cast文件中
https://asciinema.org/a/nE9TpIRsp50f5kKyMEkTYflvB #此會話錄制上傳到asciinema.org網站的url地址
This installation of asciinema recorder hasn't been linked to any asciinema.org
account. All unclaimed recordings (from unknown installations like this one)
are automatically archived 7 days after upload.
If you want to preserve all recordings made on this machine, connect this
installation with asciinema.org account by opening the following link:
https://asciinema.org/connect/d71653c1-dede-4925-af20-c5665a1fa541
* 如上, 按"enter" 鍵將會返回一個 asciinema 會話錄制的網絡播放地址, 這里的地址即為上面的"https://asciinema.org/a/nE9TpIRsp50f5kKyMEkTYflvB ",
通過這個地址就可以直接訪問以html5播放視頻方式播放錄制的會話了, 並且播放過程中可以暫停, 進行復制操作, 不過這個需要網絡.
* 如果上面不按"enter" 鍵, 按ctrl+c鍵, 則就會將錄制的會話保存到本地的/tmp/tmpyvq2q5os-ascii.cast文件里, 這樣通過
"asciinema play /tmp/tmpyvq2q5os-ascii.cast" 才能播放錄制的會話內容.
2) 將終端會話錄制下來,並保存到本地服務器上 ("asciinema rec filename")
[root@localhost ~]# mkdir /opt/operation
[root@localhost ~]# asciinema rec /opt/operation/root-$(date +%Y%m%d-%H%M%S).log
asciinema: recording asciicast to /opt/operation/root-20181228-104254.log
asciinema: press <ctrl-d> or type "exit" when you're done
[root@localhost ~]# ls /usr/local/lib # 這是已經記錄在asciinema終端會話的操作
libpcre.a libpcrecpp.so libpcre.la libpcreposix.so libpcre.so pkgconfig
libpcrecpp.a libpcrecpp.so.0 libpcreposix.a libpcreposix.so.0 libpcre.so.1
libpcrecpp.la libpcrecpp.so.0.0.1 libpcreposix.la libpcreposix.so.0.0.3 libpcre.so.1.2.5
[root@localhost ~]# echo "123123213"
123123213
[root@localhost ~]# exit $退出asciinema會話錄制
asciinema: recording finished
asciinema: asciicast saved to /opt/operation/root-20181228-104254.log
如上將終端會話錄制保存到本地的 /opt/operation/root-20181228-104254.log 文件里了
3) 將上面錄制並保存到本地的會話文件上傳到asciinema.org網站 ("asciinema upload filename")
[root@localhost ~]# ll /opt/operation/root-20181228-104254.log
-rw-r--r-- 1 root root 2026 Dec 28 10:43 /opt/operation/root-20181228-104254.log
[root@localhost ~]# asciinema upload /opt/operation/root-20181228-104254.log
View the recording at:
https://asciinema.org/a/KLVNbaUZhw3MWlWiRyuD7X7ip
This installation of asciinema recorder hasn't been linked to any asciinema.org
account. All unclaimed recordings (from unknown installations like this one)
are automatically archived 7 days after upload.
If you want to preserve all recordings made on this machine, connect this
installation with asciinema.org account by opening the following link:
如上錄制會話的本地文件上傳成功后,就會返回一個播放的鏈接。
=====這里注意下=====
在使用網絡播放鏈接打開錄制的會話內容時, 在播放界面下面的"untitled"左側有個"Download",
點擊"Download"會下載一個以數字名稱的.cats格式的文件, 將其下載到本地,比如下載名稱為218727.cast
然后將該文件218727.cast上傳到服務上, 使用"asciinema play 218727.cast"命令就能正常播放這個錄制的終端會話了
4) 將上面錄制的終端會話記錄的全部shell命令輸出打印到終端 ("asciinema cat filename")
[root@localhost ~]# asciinema cat /opt/operation/root-20181228-104254.log
[root@localhost ~]# ls /usr/local/lib
libpcre.a libpcrecpp.so libpcre.la libpcreposix.so libpcre.so pkgconfig
libpcrecpp.a libpcrecpp.so.0 libpcreposix.a libpcreposix.so.0 libpcre.so.1
libpcrecpp.la libpcrecpp.so.0.0.1 libpcreposix.la libpcreposix.so.0.0.3 libpcre.so.1.2.5
[root@localhost ~]# echo "123123213"
123123213
[root@localhost ~]# exit
[root@localhost ~]#
==========================================================
==========================================================
asciinema play命令參數解釋:
[root@localhost ~]# asciinema play --help
usage: asciinema play [-h] [-i IDLE_TIME_LIMIT] [-s SPEED] filename
positional arguments:
filename local path, http/ipfs URL or "-" (read from stdin)
optional arguments:
-h, --help show this help message and exit
-i IDLE_TIME_LIMIT, --idle-time-limit IDLE_TIME_LIMIT
limit idle time during playback to given number of
seconds
-s SPEED, --speed SPEED
playback speedup (can be fractional)
參數說明:
-i 表示播放時終端空閑時間不超過多少秒
-s 表示以多少倍的速度播放
1) 播放錄制並保存到服務器本地的終端會話 (正常播放)
[root@localhost ~]# asciinema play /opt/operation/root-20181228-104254.log #執行后, 就會自動播放錄制的會話內容, 播放完之后就會自動退出!
2) 以2倍速度播放錄制的終端會話 (-s 參數, 后面跟數字n表示2倍速度)
[root@localhost ~]# asciinema play -s 2 /opt/operation/root-20181228-104254.log
3) 以正常速度播放錄制的終端會話,但空閑時間限制為2秒 (-i 參數, 后面跟數字n 表示空閑時間不超過n秒)
[root@localhost ~]# asciinema play -i 2 /opt/operation/root-20181228-104254.log #空間時間不超過2秒
[root@localhost ~]# asciinema play -i 0.5 /opt/operation/root-20181228-104254.log #空閑時間不超過0.5秒
==========================================================
==========================================================
asciinema rec 命令幫助參數解釋:
[root@localhost ~]# asciinema rec --help
usage: asciinema rec [-h] [--stdin] [--append] [--raw] [--overwrite]
[-c COMMAND] [-e ENV] [-t TITLE] [-i IDLE_TIME_LIMIT]
[-y] [-q]
[filename]
positional arguments:
filename filename/path to save the recording to
optional arguments:
-h, --help show this help message and exit
--stdin enable stdin recording, disabled by default
--append append to existing recording
--raw save only raw stdout output
--overwrite overwrite the file if it already exists
-c COMMAND, --command COMMAND
command to record, defaults to $SHELL
-e ENV, --env ENV list of environment variables to capture, defaults to
SHELL,TERM
-t TITLE, --title TITLE
title of the asciicast
-i IDLE_TIME_LIMIT, --idle-time-limit IDLE_TIME_LIMIT
limit recorded idle time to given number of seconds
-y, --yes answer "yes" to all prompts (e.g. upload confirmation)
-q, --quiet be quiet, suppress all notices/warnings (implies -y)
參數說明:
--stdin 表示啟用標准輸入(鍵盤)錄制(請參閱下文)
--append 表示追加到現有的錄音
--raw 表示保存原始STDOUT輸出,無需定時信息或其他元數據
--overwrite 表示覆蓋已存在的記錄
-c, --command=<command> 表示指定要記錄的命令,默認為$ SHELL
-e, --env=<var-names> 表示要捕獲的環境變量列表,默認為 SHELL,TERM
-t, --title=<title> 表示指定asciicast的標題
-i, --idle-time-limit=<sec> 表示記錄的終端非活動<sec>時間限制為最大秒數
-y, --yes 表示對所有提示回答"是"(例如上傳確認)
-q, --quiet 表示保持安靜,壓制所有通知/警告(暗示-y). 這樣在進入或退出asciinema終端錄制過程中就不會有任何提示信息了!!!!!!!!!!!
示例1: 以標准輸出錄制一個終端會話, 保存到本地服務器上
[root@localhost ~]# asciinema rec /opt/operation/test1.log --stdin
asciinema: recording asciicast to /opt/operation/test1.log
asciinema: press <ctrl-d> or type "exit" when you're done
[root@localhost ~]# echo "wangshibo"
wangshibo
[root@localhost ~]# exit
asciinema: recording finished
asciinema: asciicast saved to /opt/operation/test1.log
[root@localhost ~]# asciinema cat /opt/operation/test1.log
[root@localhost ~]# echo "wangshibo"
wangshibo
[root@localhost ~]# exit
[root@localhost ~]#
示例2: 在上面錄制的終端會話文件中追加錄制內容
[root@localhost ~]# asciinema rec /opt/operation/test1.log --append
asciinema: appending to asciicast at /opt/operation/test1.log
asciinema: press <ctrl-d> or type "exit" when you're done
[root@localhost ~]# echo "kevin"
kevin
[root@localhost ~]# exit
asciinema: recording finished
asciinema: asciicast saved to /opt/operation/test1.log
[root@localhost ~]# asciinema cat /opt/operation/test1.log
[root@localhost ~]# echo "wangshibo"
wangshibo
[root@localhost ~]# exit
[root@localhost ~]# echo "kevin"
kevin
[root@localhost ~]# exit
[root@localhost ~]#
示例3: 錄制終端會話, 並覆蓋掉之前存在的記錄
[root@localhost ~]# asciinema rec /opt/operation/test1.log --overwrite
asciinema: recording asciicast to /opt/operation/test1.log
asciinema: press <ctrl-d> or type "exit" when you're done
[root@localhost ~]# echo "6666666"
6666666
[root@localhost ~]# exit
asciinema: recording finished
asciinema: asciicast saved to /opt/operation/test1.log
[root@localhost ~]# asciinema cat /opt/operation/test1.log
[root@localhost ~]# echo "6666666"
6666666
[root@localhost ~]# exit
[root@localhost ~]#
示例4: 錄制終端會話, 錄制中終端空閑時間(即非活動時間)限制為0.5秒 (即最大不超過0.5秒)
[root@localhost ~]# asciinema rec /opt/operation/test3.log -i 0.5
asciinema: recording asciicast to /opt/operation/test3.log
asciinema: press <ctrl-d> or type "exit" when you're done
[root@localhost ~]# ll -d /usr/local/lib
drwxr-xr-x. 3 root root 4096 10月 24 14:28 /usr/local/lib
[root@localhost ~]# echo 77777
77777
[root@localhost ~]# exit
asciinema: recording finished
asciinema: asciicast saved to /opt/operation/test3.log
[root@localhost ~]# asciinema cat /opt/operation/test3.log
[root@localhost ~]# ll -d /usr/local/lib
drwxr-xr-x. 3 root root 4096 10月 24 14:28 /usr/local/lib
[root@localhost ~]# echo 77777
77777
[root@localhost ~]# exit
[root@localhost ~]#
示例5: 錄制終端會話, 錄制過程中保持安靜,壓制所有通知/警告. 即錄制過程中屏蔽所有提示信息
[root@localhost ~]# asciinema rec /opt/operation/test9.log -q
[root@localhost ~]# hostname
localhost
[root@localhost ~]# echo yyyyyy
yyyyyy
[root@localhost ~]# exit
[root@localhost ~]# asciinema cat /opt/operation/test9.log
[root@localhost ~]# hostname
localhost
[root@localhost ~]# echo yyyyyy
yyyyyy
[root@localhost ~]# exit
[root@localhost ~]#
也就是說, 在錄制會話過程中, 只要添加了-q參數, 在進入錄制或退出錄制時, 都沒有提示信息!
==========================================================
==========================================================
asciinema還提供了一個可以管理asciinema個人賬戶所擁有的會話文件的功能, 命令為"asciinema auth"
[root@localhost ~]# asciinema auth
Open the following URL in a web browser to link your install ID with your asciinema.org user account:
https://asciinema.org/connect/d71653c1-dede-4925-af20-c5665a1fa541
This will associate all recordings uploaded from this machine (past and future ones) to your account, and allow you to manage them (change title/theme, delete) at asciinema.org.
[root@localhost ~]# asciinema auth
Open the following URL in a web browser to link your install ID with your asciinema.org user account:
https://asciinema.org/connect/d71653c1-dede-4925-af20-c5665a1fa541
This will associate all recordings uploaded from this machine (past and future ones) to your account, and allow you to manage them (change title/theme, delete) at asciinema.org.
如上, 執行命令"asciinema auth"命令后, 會返回一個網絡地址, 點擊這個地址就會打開asciinema個人賬號注冊界面,
使用個人郵箱注冊, 注冊好之后, 每次登錄(使用郵箱)都會往個人郵箱發一個鏈接, 點擊打開這個鏈接,
就能看到你的賬號下所有的終端錄制的會話播放視頻了!
==========================================================
==========================================================
這里記錄一個故障:
在 UTF-8 環境下運行 asciinema, 遇到錯誤信息:
asciinema needs a UTF-8 native locale to run. Check the output of `locale` command.
解決方法: 生成並導出 UTF-8 語言環境。例如:
[root@localhost ~]# localedef -c -f UTF-8 -i en_US en_US.UTF-8
[root@localhost ~]# export LC_ALL=en_US.UTF-8
asciinema auth登錄個人賬號, 可以管理錄制的終端會話, 效果如下:

三. 使用asciinema 做服務器系統的操作審計
可以利用asciinema的終端會話錄制功能進行linux服務器系統操作審計, 在對應系統賬號下添加asciinema終端會話錄制的設置,使得每次登錄對應系統賬號下的時候, 自動進入asciinema終端會話的錄制過程中. 部署方法記錄如下:
[root@localhost ~]# echo $HOME
/root
[root@localhost ~]# echo $USER
root
[root@localhost ~]# echo $(date +%Y-%m-%d-%H:%M:%S)
2018-12-28-12:30:19
[root@localhost ~]# which asciinema
/usr/bin/asciinema
創建兩個賬號
[root@localhost ~]# useradd bobo
[root@localhost ~]# useradd grace
切換到這兩個賬號, 生產公私鑰文件
[root@localhost ~]# su - bobo
[bobo@localhost ~]$ ssh-keygen -t rsa //一直按回車鍵
[root@localhost ~]# su - grace
[grace@localhost ~]$ ssh-keygen -t rsa //一直按回車鍵
[root@localhost ~]# ll -d /home/bobo/.ssh/
drwx------ 2 bobo bobo 4096 12月 28 12:37 /home/bobo/.ssh/
[root@localhost ~]# ll -d /home/grace/.ssh/
drwx------ 2 grace grace 4096 12月 28 12:37 /home/grace/.ssh/
特別注意:
一定要保證終端會話錄制保存到本地服務器上的路徑真實存在!
如下設置, 要提前確保對應系統賬號的~/.ssh目錄存在
否則, 如下設置好每個系統賬戶的asciinema錄制功能后, 會有報錯的!
如下設置, 將asciinema錄制終端會話的文件存放在了本地服務器的~/.ssh目錄下,
當然, 也可以選擇存放在別的路徑下, 但是一定要提前確保這個存放路徑真實存在.
在linux系統的每個用戶的家目錄下的.bash_profile添加下面一段 :
[root@localhost ~]# vim /root/.bash_profile
..........
/usr/bin/asciinema rec $HOME/.ssh/$USER-$(date +%Y-%m-%d-%H:%M:%S).log -q
[root@localhost ~]# vim /home/bobo/.bash_profile
.........
/usr/bin/asciinema rec $HOME/.ssh/$USER-$(date +%Y-%m-%d-%H:%M:%S).log -q
[root@localhost ~]# vim /home/grace/.bash_profile
........
/usr/bin/asciinema rec $HOME/.ssh/$USER-$(date +%Y-%m-%d-%H:%M:%S).log -q
如上, 在root, bobo, grace 三個系統賬號下分別設置了asciinema的終端會話錄制功能, 設置之后:
每次登錄這三個賬號, 都會自動進入asciinema終端會話錄制過程中, 即自動多登錄一次!
由於asciinema錄制命令中添加了 -q 參數, 所以登錄和退出錄制都不會有任何提示信息!!!
示例如下:
通過xshell 或 ssh從遠程登錄到該服務器的root賬號:
Last login: Fri Dec 28 12:41:36 2018 from 172.16.24.199
[root@localhost ~]# exit #即設置好系統賬戶的asciinema錄制功能后, 登錄root賬戶, 就自動進入到asciinema錄制過程了, "ctrl+d" 即可退出
[root@localhost ~]# ll ~/.ssh/
總用量 248
-rwxr-xr-x 1 root root 96 12月 27 16:00 asciinema.sh
-rw-r--r-- 1 root root 395 12月 27 14:15 known_hosts
-rw-r--r-- 1 root root 207 12月 27 16:01 root-2018-12-27-16:01:56.log
-rw-r--r-- 1 root root 8246 12月 27 16:02 root-2018-12-27-16:02:12.log
[root@localhost ~]# su - bobo
[bobo@localhost ~]$ exit # 同樣, 登錄bobo賬號, 也是自動進入asciinema錄制過程中
[bobo@localhost ~]$ ll ~/.ssh/ # "ctrl+d"退出后, 可以查看到在~/.ssh 目錄下
總用量 16
-rw-rw-r-- 1 bobo bobo 228 12月 28 12:41 bobo-2018-12-28-12:41:42.log
-rw-rw-r-- 1 bobo bobo 227 12月 28 12:42 bobo-2018-12-28-12:42:08.log
-rw------- 1 bobo bobo 1675 12月 28 12:37 id_rsa
-rw-r--r-- 1 bobo bobo 396 12月 28 12:37 id_rsa.pub
[root@localhost ~]# su - grace # 同樣的道理
[grace@localhost ~]$ exit
[grace@localhost ~]$ ll ~/.ssh/
總用量 12
-rw-rw-r-- 1 grace grace 228 12月 28 12:45 grace-2018-12-28-12:45:40.log
-rw------- 1 grace grace 1675 12月 28 12:37 id_rsa
-rw-r--r-- 1 grace grace 397 12月 28 12:37 id_rsa.pub
可以選擇播放其中的一個會話錄制
[root@localhost ~]# asciinema play /home/bobo/.ssh/bobo-2018-12-28-12:42:08.log -s 2
[bobo@localhost ~]$ hostname
localhost
[bobo@localhost ~]$ echo haha
haha
[bobo@localhost ~]$ ll /etc/passwd
-rw-r--r-- 1 root root 1876 12月 28 12:35 /etc/passwd
[bobo@localhost ~]$ exit
[root@localhost ~]#
==============================================
當如上設置好對應系統賬號的asciinema終端會話錄制功能后, 由於服務器登錄頻繁, 操作頻繁, 過了一段時間后,
會發現對應賬號的~/.ssh 目錄下會產生很多大量的會話文件, 記錄多的話, 可能會造成磁盤壓力.
這就需要寫一個腳本, 用於刪除這些會話文件, 由於每台服務器的操作頻率不一樣, 可能有的機器錄制的文件多,
有的機器錄制的文件少, 最好別統一定時刪除. 最好是寫一個腳本, 用於自己自定義刪除多久的會話文件.
如下:
[root@localhost ~]# cd ~/.ssh/
[root@localhost .ssh]# ls -l|grep "$USER-"
-rw-r--r-- 1 root root 36547 12月 28 12:10 root-2018-12-02-11:38:21.log
-rw-r--r-- 1 root root 3694 12月 28 12:11 root-2018-12-03-12:10:44.log
-rw-r--r-- 1 root root 228 12月 28 12:41 root-2018-12-05-12:41:36.log
-rw-r--r-- 1 root root 3552 12月 28 10:21 root-2018-12-08-10:15:43.log
-rw-r--r-- 1 root root 227 12月 28 12:36 root-2018-12-09-12:36:11.log
-rw-r--r-- 1 root root 227 12月 28 12:34 root-2018-12-10-12:34:37.log
-rw-r--r-- 1 root root 228 12月 28 12:41 root-2018-12-13-12:41:54.log
-rw-r--r-- 1 root root 228 12月 27 16:03 root-2018-12-15-16:03:01.log
-rw-r--r-- 1 root root 228 12月 28 12:37 root-2018-12-16-12:37:25.log
-rw-r--r-- 1 root root 40891 12月 28 12:32 root-2018-12-17-12:11:17.log
-rw-r--r-- 1 root root 32159 12月 28 10:38 root-2018-12-18-10:23:00.log
-rw-r--r-- 1 root root 228 12月 28 12:33 root-2018-12-19-12:33:01.log
-rw-r--r-- 1 root root 808 12月 28 12:32 root-2018-12-20-12:32:50.log
-rw-r--r-- 1 root root 0 12月 28 13:00 root-2018-12-22-23:21:34.log
-rw-r--r-- 1 root root 0 12月 28 13:00 root-2018-12-23-23:25:34.log
-rw-r--r-- 1 root root 207 12月 27 16:01 root-2018-12-27-16:01:56.log
-rw-r--r-- 1 root root 8246 12月 27 16:02 root-2018-12-27-16:02:12.log
-rw-r--r-- 1 root root 833 12月 27 16:03 root-2018-12-27-16:03:16.log
-rw-r--r-- 1 root root 4405 12月 28 00:00 root-2018-12-27-23:25:34.log
-rw-r--r-- 1 root root 1767 12月 28 10:05 root-2018-12-28-10:04:47.log
-rw-r--r-- 1 root root 52660 12月 28 10:41 root-2018-12-28-10:21:59.log
-rw-r--r-- 1 root root 228 12月 28 12:34 root-2018-12-28-12:34:48.log
[root@localhost .ssh]# ls -l|grep "$USER-"|awk '{print $9}'|awk -F"-" '{print $2$3$4}'
20181202
20181203
20181205
20181208
20181209
20181210
20181213
20181215
20181216
20181217
20181218
20181219
20181220
20181222
20181223
20181227
20181227
20181227
20181227
20181228
20181228
20181228
腳本內容如下:
[root@localhost .ssh]# cat asciinema_log_delete.sh
#!/bin/bash
echo -n "請輸入日期, 截止到該日期之前的日志目錄接下來將要被刪除:"
read date1
date2=$(echo ${date1}|awk -F"-" '{print $1$2$3}')
cd ~/.ssh/
for date3 in $(ls -l|grep "$USER-"|awk '{print $9}'|awk -F"-" '{print $2$3$4}')
do
a=$(echo ${date3}|cut -c 1-4)
b=$(echo ${date3}|cut -c 5-6)
c=$(echo ${date3}|cut -c 7-8)
date4=$(echo ${USER}-${a}-${b}-${c}-*.log)
if [ ${date3} -lt ${date2} ];then
rm -rf ${date4} && echo "deleted ${date4}"
else
echo "${date4} do not need to delete"
fi
done
添加腳本執行權限:
[root@localhost .ssh]# chmod 755 asciinema_log_delete.sh
比如現在要刪除2018-12-23之前的錄制文件:
[root@localhost .ssh]# sh asciinema_log_delete.sh
請輸入日期, 截止到該日期之前的日志目錄接下來將要被刪除:2018-12-23-23
deleted root-2018-12-03-12:10:44.log
deleted root-2018-12-05-12:41:36.log
deleted root-2018-12-08-10:15:43.log
deleted root-2018-12-09-12:36:11.log
deleted root-2018-12-10-12:34:37.log
deleted root-2018-12-13-12:41:54.log
deleted root-2018-12-15-16:03:01.log
deleted root-2018-12-16-12:37:25.log
deleted root-2018-12-17-12:11:17.log
deleted root-2018-12-18-10:23:00.log
deleted root-2018-12-19-12:33:01.log
deleted root-2018-12-20-12:32:50.log
deleted root-2018-12-22-23:21:34.log
root-2018-12-23-23:25:34.log do not need to delete
root-2018-12-27-16:01:56.log root-2018-12-27-16:02:12.log root-2018-12-27-16:03:16.log root-2018-12-27-23:25:34.log do not need to delete
root-2018-12-27-16:01:56.log root-2018-12-27-16:02:12.log root-2018-12-27-16:03:16.log root-2018-12-27-23:25:34.log do not need to delete
root-2018-12-27-16:01:56.log root-2018-12-27-16:02:12.log root-2018-12-27-16:03:16.log root-2018-12-27-23:25:34.log do not need to delete
root-2018-12-27-16:01:56.log root-2018-12-27-16:02:12.log root-2018-12-27-16:03:16.log root-2018-12-27-23:25:34.log do not need to delete
root-2018-12-28-10:04:47.log root-2018-12-28-10:21:59.log root-2018-12-28-12:34:48.log do not need to delete
root-2018-12-28-10:04:47.log root-2018-12-28-10:21:59.log root-2018-12-28-12:34:48.log do not need to delete
root-2018-12-28-10:04:47.log root-2018-12-28-10:21:59.log root-2018-12-28-12:34:48.log do not need to delete
[root@localhost .ssh]# ls -l
總用量 96
-rwxr-xr-x 1 root root 569 12月 28 14:06 asciinema_log_delete.sh
-rw-r--r-- 1 root root 395 12月 27 14:15 known_hosts
-rw-r--r-- 1 root root 0 12月 28 13:00 root-2018-12-23-23:25:34.log
-rw-r--r-- 1 root root 207 12月 27 16:01 root-2018-12-27-16:01:56.log
-rw-r--r-- 1 root root 8246 12月 27 16:02 root-2018-12-27-16:02:12.log
-rw-r--r-- 1 root root 833 12月 27 16:03 root-2018-12-27-16:03:16.log
-rw-r--r-- 1 root root 4405 12月 28 00:00 root-2018-12-27-23:25:34.log
-rw-r--r-- 1 root root 1767 12月 28 10:05 root-2018-12-28-10:04:47.log
-rw-r--r-- 1 root root 52660 12月 28 10:41 root-2018-12-28-10:21:59.log
-rw-r--r-- 1 root root 228 12月 28 12:34 root-2018-12-28-12:34:48.log
將這個asciinema_log_delete.sh處理腳本放在對應系統用戶的會話錄制文件所在的目錄下(~/.ssh), 定期進行處理即可!
[root@localhost ~]# ll /root/.ssh/asciinema_log_delete.sh
-rwxr-xr-x 1 root root 569 12月 28 14:06 /root/.ssh/asciinema_log_delete.sh
[root@localhost ~]# ll /home/bobo/.ssh/asciinema_log_delete.sh
-rwxr-xr-x 1 root root 569 12月 28 14:13 /home/bobo/.ssh/asciinema_log_delete.sh
[root@localhost ~]# ll /home/grace/.ssh/asciinema_log_delete.sh
-rwxr-xr-x 1 root root 569 12月 28 14:13 /home/grace/.ssh/asciinema_log_delete.sh
[root@localhost ~]# diff /root/.ssh/asciinema_log_delete.sh /home/bobo/.ssh/asciinema_log_delete.sh
[root@localhost ~]# diff /root/.ssh/asciinema_log_delete.sh /home/grace/.ssh/asciinema_log_delete.sh
