Linux终端回话记录和回放工具 - asciinema使用总结

时间:2023-03-09 04:09:00
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版本共存)
[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登录个人账号, 可以管理录制的终端会话, 效果如下:

Linux终端回话记录和回放工具 - asciinema使用总结

三. 使用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