1. 程式人生 > >Linux終端回話記錄和回放工具 - asciinema使用總結

Linux終端回話記錄和回放工具 - asciinema使用總結

 

目前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版本共存)
[[email protected] ~]# python -V
Python 2.6.6

[
[email protected]
~]# yum -y install xz epel-release zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel [[email protected] ~]# cd /usr/local/src/ [[email protected] src]# wget https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz [[email protected] src]# tar -vxf Python-3.5.3.tar.xz [[email protected] src]# cd Python-3.5.3 [[email protected] Python-3.5.3]# ./configure --prefix=/usr/local/python3 [[email protected] Python-3.5.3]# make && make install 從 Python 3.4 開始就已經自帶了pip和easy_install(setuptools 包帶的命令) 包管理命令,可以在 /usr/local/python3/bin/ 目錄下看到這些安裝的擴充套件包: [[email protected] Python-3.5.3]# /usr/local/python3/bin/python3 -V Python 3.5.3 [[email protected] Python-3.5.3]# /usr/local/python3/bin/pip3 -V 建立軟連結 [[email protected] ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3 [[email protected] ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 檢視版本 [[email protected] ~]# python -V Python 2.6.6 [[email protected] ~]# python3 -V Python 3.5.3 [[email protected] ~]# pip3 -V pip 9.0.1 from /usr/local/python3/lib/python3.5/site-packages (python 3.5) 2) 安裝asciinema [[email protected] ~]# pip3 install asciinema [[email protected] ~]# find / -name asciinema /usr/local/python3/lib/python3.5/site-packages/asciinema /usr/local/python3/bin/asciinema [[email protected] ~]# ln -s /usr/local/python3/bin/asciinema /usr/bin/asciinema [[email protected] ~]# asciinema --version asciinema 2.0.1 檢視asciinema幫助資訊 [[email protected] ~]# 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 無法回放) !