前言:
asciinema這個錄屏軟件前面有一點研究,但它的部署安裝比較麻煩,雖然此軟件的安裝部署方式是很多的,比如yum安,apt,brew,docker,pip,rust編譯,docker等等,但唯獨缺少二進制的安裝方式。
前面關于asciinema的簡單介紹:
Linux|centos7|終端錄屏神器asciinema的部署和初步使用-CSDN博客
究其原因,可能是因為該軟件的設計初衷是希望各位用戶將自己的錄屏內容聯網發送到官網,想做成一個互聯網的產品吧。
總的來說,部署asciinema基本是需要Python3的環境,而Python在linux內是一個非常重要的依賴,比如,yum命令依賴于Python2.7 而這樣嚴苛的部署依賴是影響此軟件的推廣使用的
因此,這幾天研究了一下asciinema的部署,發現rust編譯后產生的asciinema可以以一個二進制形式的程序運行,雖然該二進制的asciinema運行仍然需要高版本的glibc作為依賴,但已經在可接受的范圍內了;這大大提高了asciinema的易用性,并且該軟件的錄制效果非常好,長時間運行錄屏的錄像產物磁盤空間占用并不高,并且可以在播放的時候調節播放速度,這些都是非常nice的地方。
那么,此軟件配合shell腳本或者開發框架可以做到運維審計功能;其次還有asciinema2gif這個軟件,此軟件可以將asciinema生成的錄屏文件轉換成GIF格式,使得播放不在是一個讓人困擾的問題(asciinema生成的錄屏文件必須使用asciinema才可以播放,比較的讓人煩躁,不是嗎?)
下面將就asciinema的編譯和利用編譯出來的可執行文件做到在其它的服務器上免編譯,隨時隨地使用做一個盡量詳盡的介紹
一,
asciinema的編譯
這個軟件的編譯比較特別,需要使用rust編譯器來進行編譯,全程基本都是需要聯網操作,對網速的要求是比較高的
asciinema編譯安裝包的下載地址:
https://codeload.github.com/asciinema/asciinema/zip/refs/heads/develop
rust編譯環境的快速離線部署:Linux|centos7| rust語言的編譯開發環境快速部署-CSDN博客
rust編譯環境安裝部署好后,將asciinema也上傳到服務器上,任意路徑,解壓后,進入解壓目錄,執行編譯即可:
cd asciinema
cargo build --release
注意,這個是比較慢的,基本所有的依賴包什么的都是從國外的網站自動下載,非常耗時和考驗網速
編譯完成后,可以看到asciinema的源碼包內文件如下:
[root@node2 asciinema-develop]# ls -alh
total 160K
drwxr-xr-x 9 root root 306 Feb 20 23:39 .
dr-xr-x---. 9 root root 4.0K Feb 23 05:57 ..
drwxr-xr-x 2 root root 83 Feb 20 17:42 assets
drwxr-xr-x 2 root root 25 Feb 20 17:42 .cargo
-rw-r--r-- 1 root root 70K Feb 20 17:42 Cargo.lock
-rw-r--r-- 1 root root 1.5K Feb 20 17:42 Cargo.toml
-rw-r--r-- 1 root root 9.8K Feb 20 17:42 CHANGELOG.md
-rw-r--r-- 1 root root 3.2K Feb 20 17:42 CODE_OF_CONDUCT.md
-rw-r--r-- 1 root root 3.8K Feb 20 17:42 CONTRIBUTING.md
drwxr-xr-x 2 root root 52 Feb 20 17:42 doc
-rw-r--r-- 1 root root 586 Feb 20 17:42 Dockerfile
-rw-r--r-- 1 root root 1.5K Feb 20 17:42 flake.lock
-rw-r--r-- 1 root root 603 Feb 20 17:42 flake.nix
drwxr-xr-x 4 root root 67 Feb 20 17:42 .github
-rw-r--r-- 1 root root 23 Feb 20 17:42 .gitignore
-rw-r--r-- 1 root root 35K Feb 20 17:42 LICENSE
-rw-r--r-- 1 root root 4.2K Feb 20 17:42 README.md
drwxr-xr-x 6 root root 262 Feb 20 17:42 src
drwxr-xr-x 3 root root 65 Feb 20 23:40 target
drwxr-xr-x 4 root root 74 Feb 20 17:42 tests
asciinema的可執行二進制文件在target/release目錄下,算是動態編譯了,進一步查看asciinema的運行依賴庫,可以看到是有依賴glibc-2.18的,也就是說缺少這個依賴庫:
[root@node2 asciinema-develop]# ls -alh target/release/asciinema
-rwxr-xr-x 2 root root 21M Feb 20 23:40 target/release/asciinema
[root@node2 asciinema-develop]# file !$
file target/release/asciinema
target/release/asciinema: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), BuildID[sha1]=0a4b2f36c547e6e6729353a449deda469e7233e5, for GNU/Linux 2.6.32, not stripped[root@node1 ~]# ldd asciinema
ldd: warning: you do not have execution permission for `./asciinema'
./asciinema: /lib64/libc.so.6: version `GLIBC_2.18' not found (required by ./asciinema)linux-vdso.so.1 => (0x00007ffcf95ce000)libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f119e43e000)libutil.so.1 => /lib64/libutil.so.1 (0x00007f119e23b000)librt.so.1 => /lib64/librt.so.1 (0x00007f119e033000)libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f119de17000)libm.so.6 => /lib64/libm.so.6 (0x00007f119db15000)libdl.so.2 => /lib64/libdl.so.2 (0x00007f119d911000)libc.so.6 => /lib64/libc.so.6 (0x00007f119d543000)/lib64/ld-linux-x86-64.so.2 (0x00007f119f230000)
🆗,現在asciinema這個可執行程序可以拿到任意一臺服務器上去使用了,就像一個二進制文件一樣的了,只是前提需要有GLIBC_2.18這個依賴庫了
因此,需要編譯安裝一個GLIBC_2.18,下面是編譯安裝的方法:
curl -O http://ftp.gnu.org/gnu/glibc/glibc-2.18.tar.gz
###下載這個安裝包后,解壓,進入解壓目錄,直接執行以下命令
mkdir build
cd build
../configure --prefix=/usr
make
make install
說明:
該版本的GLIBC_2.18編譯不需要高版本的gcc編譯器支持,系統默認的4.8版本gcc就可以了,不會有什么報錯,基本一路暢通,如果是GLIBC_2.23,才可能會報錯
在centos7.7.1908以及centos7.0? centos7.5都編譯通過,這個比較簡單,只是編譯前別忘了yum install gcc gcc-c++ zlib zlib-devel openssl openssl-devel -y? 了
[root@node1 ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
[root@node1 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
編譯成功后,該版本的glibc會自動鏈接好/lib64/libc.so.6 這個文件:
[root@node1 ~]# ls -al /lib64/libc.so.6
lrwxrwxrwx. 1 root root 12 Jun 18 2023 /lib64/libc.so.6 -> libc-2.18.so
此時asciinema就可以非常方便的使用了!!!! 在任意的centos7服務器上都可以使用,只要有
GLIBC_2.23
二、
asciinema的使用
asciinema這個可執行文件移動到系統路徑下,比如/usr/local/bin/目錄下,可以改名,改成任意的名字都可以,比如,我這里改成了audit這個名字,當然,別忘了給這個可執行文件賦予執行權限
[root@node2 asciinema-develop]# ls -al /usr/local/bin/audit
-rwxr-xr-x 1 root root 21731640 Feb 22 07:09 /usr/local/bin/audit
[root@node2 asciinema-develop]# file !$
file /usr/local/bin/audit
/usr/local/bin/audit: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), BuildID[sha1]=0a4b2f36c547e6e6729353a449deda469e7233e5, for GNU/Linux 2.6.32, not stripped
1、
基本的錄屏
rec + 任意的文件名,我這里用的是3333,可以看到開始錄屏了(建議還是起一個有意義的名字,不要像我一樣太隨便)
[root@node2 ~]# audit rec 3333
::: Recording session started, writing to 3333
::: Press <ctrl+d> or type 'exit' to end
輸入CTRL+d 或者exit 退出錄屏,觀察錄屏文件,可以看到該文件是一個文本文件,記錄了Linux內的命令行動作:
[root@node2 ~]# file 3333
3333: ASCII text
[root@node2 ~]# cat 3333
{"version":2,"width":332,"height":80,"timestamp":1708640677,"env":{"SHELL":"/bin/bash","TERM":"xterm"}}
[0.012133, "o", "\u001b]0;root@node2:~\u0007\u001b[?1034h[root@node2 ~]# "]
[53.375933, "o", "d"]
[53.550371, "o", "d"]
[53.719655, "o", "d"]
[53.877701, "o", "d"]
[54.186268, "o", "\r\n"]
[54.186629, "o", "bash: dddd: command not found\r\n"]
[54.186836, "o", "\u001b]0;root@node2:~\u0007[root@node2 ~]# "]
[55.948774, "o", "exit\r\n"]
2、
靜默錄屏和追加錄屏
這里的靜默指的是不提示開始錄屏,也就是不顯示如下:
::: Recording session started, writing to 3333
::: Press <ctrl+d> or type 'exit' to end
加參數-aq? -a是追加的意思,也就是接著上次的錄屏文件繼續錄制屏幕,-q是靜默的意思,以上提示沒有
?
[root@node2 ~]# audit rec -aq 3333
3、
播放錄屏文件,并提高播放速度:
參數play + -s 任意數字,可以整數也可以小數,這個速度沒有上限,隨便多少速度都可以,不過我這里100的速度,很快就播放完了
[root@node2 ~]# audit play 3333 -s 100
::: Replaying session from 3333
[root@node2 ~]# dddd
bash: dddd: command not found
[root@node2 ~]# exit
[root@node2 ~]# chmod a+x asciinema
[root@node2 ~]# exit
::: Playback ended
三、
asciinema的使用范圍
可以將該程序寫入腳本內,配合腳本就可以自動化的運維審計了,其次,運維的操作分享,比如,某些軟件的部署過程,可以分享給同事了,最后,利用asciinema可以生成可可復制文字的GIF圖片。