How to implement a terminal recording playback function like Jumpserver Koko? This article introduces an artifact

Asciinema is an open source, free terminal recording tool that saves any input or output from the command line in a file with the time added and provides a way to play it back on the terminal or in a Web browser

Asciinema is text-based for recording and playback, and has many advantages over traditional video, such as smaller recorded files and the ability to pause and copy text during playback

Asciinema also provides a website where you can upload recordings to asciinema.org for display if you wish, and where you can find many interesting terminal videos

Asciinema consists of three sub-projects:

  1. Asciinema: Command-line based terminal session logger
  2. Asciinema.org: site that provides apis for uploading videos and presentations
  3. Javascript Player: A JAVASCRIPT player for playing videos on the Web

Asciinema is easy to install and use, so take a look

The installation

Asciinema is developed for Python and can be installed directly via apt-get, yum, or PIP

# apt-get install asciinema
Copy the code

View the version after the installation is complete

# asciinema --versionAsciinema 2.0.2Copy the code

Asciinema has two versions, v1 and V2, which are quite different. Coffee uses V2, and all the following content is based on V2 demo

Asciinema has 5 parameters: record: rec, play: play, view recorded content in file format: cat, upload file to asciinema.org website: upload, asciinema.org account authentication: auth. This article describes the use of rec and play

The recording

# asciinema rec ops-coffee.cast
Copy the code

There are several parameters you can use:

— Stdin indicates that standard input recording is enabled, which means that normally the input password of Linux will not be displayed. If this option is enabled, it can record the password output from the keyboard, but it is not supported yet

Append adds recording to an existing file

— RAW Saves the original STDOUT output without timing information

–overwrite Overwrites the file if it already exists

-c Specifies the command to record. The default is $SHELL

-e List of environment variables to capture. The default values are SHELL,TERM

-t followed by a number to specify the video title

-i follows a number to set the maximum idle time for recording

-y Enter yes for all prompts

-q Silent mode. When this parameter is added, there is no prompt for entering or exiting recording

Enter Exit or press CTRL +D to exit recording

play

# asciinema play ops-coffee.cast 
Copy the code

There are two parameters you can use:

-s is followed by a number, indicating the number of times the speed at which the video is played

-i, followed by a number, indicates the maximum number of seconds of idle time during video playing

In the process of playing you can control the pause or play through the space bar, you can also use CTRL + C combination key to exit the play, when you press the space bar pause, you can pass. To show what is to be played frame by frame

file

Asciinema’s recommended file suffix is.cast. Linux doesn’t care about file suffixes, so you can use whatever you want

# cat ops-coffee.cast
{"version": 2."width": 237, "height": 55."timestamp": 1572646909, "env": {"SHELL": "/bin/bash"."TERM": "linux"}, "title": "ops-coffee"}
[0.010014, "o"."root@onlinegame:~# "]
[1.296458, "o"."exit"]
[1.976439, "o"."\r\n"]
[1.976532, "o"."exit\r\n"]
Copy the code

The cast file consists of two main parts, a dictionary on the first line, called header

{
    "version": 2."width": 237,
    "height": 55."timestamp": 1572646909,
    "env": {
        "SHELL": "/bin/bash"."TERM": "linux"
    },
    "title": "ops-coffee"
}
Copy the code

Header is simple, the meaning of the fields are: version version, width and height respectively represent the width and height of the recording window, timestamp timestamp of the beginning of recording, the -e parameter setting specified when env recording, and the -t parameter setting specified when title recording

What follows is fixed format content, which is actually IO stream information

[0.010014, "o"."root@onlinegame:~# "]
Copy the code

Each line is a three-part list

The first part is a floating-point number that represents the time taken for the input/output line

The second part seems to be a fixed string, did not find the description to do what

The third part is the specific input and output content

This file format is designed or very elegant, the beginning of the header, behind the concrete content, if midway because any accidental cause video end, also won’t lost the whole video, but also can add video to append this is very useful when need long time to stop, more important is can be read streaming, rarely take up memory, No need to store the entire video file in memory, more friendly for long recording playback

Automatically records audit logs

If you’ve ever been through a rigorous IT audit or worked with a Fortress computer, you know that operations need to be documented and audited. If you’ve ever been blamed for not knowing who did what that caused data to be deleted, you know how important IT is to document operations. A simple example of how asciinema can be useful

What if you want to be able to record all devUser operations on a Linux server for subsequent audit purposes?

It’s as simple as adding a.bash_profile file to the devuser’s home directory as follows:

$ cat ~/.bash_profile 
export LC_ALL=en_US.UTF-8
/usr/local/bin/asciinema rec /tmp/$USER-$(date +%Y%m%d%H%M%S).log -q
Copy the code

Asciinema needs a UTF-8 native locale to run. Check the output of locale command. Export LC_ALL=en_US.

The -q parameter is added to the rec command for recording, so that there is no prompt for asciinema on entry or exit, making it easy to use

Each time the devuser logs in, a video is automatically enabled. If you need to audit or check the video, you only need to play back the video

You might say that the history command also records user actions, so what’s the advantage of Asciinema? Asciinema records both user input and system output. History records only executed commands, while Asciinema records the results of executed commands


Related articles recommended reading:

  • Django implements WebSSH operations Kubernetes Pod
  • Django implements WebSSH for physical or virtual machines