linux下终端录制

目前linux终端回放工具常见的就是asciinema和script了, asciinema使用更加简单方便

安装asciinema

1
2
3
4
5
6
7
8
9
[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帮助信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[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 #显示版本号

使用案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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 比起 script 来说简单的太多了, 仅仅需要打开一个终端窗口运行asciinema rec 命令将回话上传到asciinema.org网站这个需要谨慎操作,建议存放到本地 ,运行 asciinema rec local-file 将会话保存到服务器本地local-file文件即可!

将终端会话录制下来, 并上传到asciinema.org网站 (“asciinema rec”)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[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" 才能播放录制的会话内容.

asciinema play命令参数解释:

1
2
3
4
5
6
7
8
9
10
11
12
13
[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
2
3
4
5
6
7
8
9
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 命令帮助参数解释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[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> 表示记录的终端非活动时间限制为最大秒数
-y, --yes 表示对所有提示回答”是”(例如上传确认)
-q, --quiet 表示保持安静,压制所有通知/警告(暗示-y). 这样在进入或退出asciinema终端录制过程中就不会有任何提示信息了!!!!!!!!!!!

1
2
3
4
5
6
7
8
9
[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

##追加录制
** 低版本不支持追加**

1
2
3
4
5
6
7
8
9
[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

覆盖录制

1
2
3
4
5
6
7
8
[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

示例4: 录制终端会话, 录制中终端空闲时间(即非活动时间)限制为0.5秒 (即最大不超过0.5秒)

1
[root@localhost ~]# asciinema rec /opt/operation/test3.log -i 0.5 

示例5: 录制终端会话, 录制过程中保持安静,压制所有通知/警告. 即录制过程中屏蔽所有提示信息

1
[root@localhost ~]# asciinema rec /opt/operation/test9.log -q

也就是说, 在录制会话过程中, 只要添加了-q参数, 在进入录制或退出录制时, 都没有提示信息!

使用asciinema做审计

可以利用asciinema的终端会话录制功能进行linux服务器系统操作审计, 在对应系统账号下添加asciinema终端会话录制的设置,使得每次登录对应系统账号下的时候, 自动进入asciinema终端会话的录制过程中. 部署方法记录如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[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/

要保证终端会话录制保存到本地服务器上的路径存在
在linux系统的每个用户的家目录下的.bash_profile添加下面一段 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[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

当如上设置好对应系统账号的asciinema终端会话录制功能后, 由于服务器登录频繁, 操作频繁, 过了一段时间后,会发现对应账号的~/.ssh 目录下会产生很多大量的会话文件, 记录多的话, 可能会造成磁盘压力.这就需要写一个脚本, 用于删除这些会话文件, 由于每台服务器的操作频率不一样, 可能有的机器录制的文件多, 有的机器录制的文件少, 最好别统一定时删除. 最好是写一个脚本, 用于自己自定义删除多久的会话文件.

清理日志脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[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