Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to record and replay terminal sessions on Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to record and replay terminal conversation on Linux". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to record and replay terminal conversation on Linux".

Typically, Linux administrators use the history command to track which commands have been executed in previous sessions, but the limitation of the history command is that it does not store the output of the command. In some cases, we want to examine the command output from the previous session and want to compare it with the current session. In addition, in some cases, we are troubleshooting problems in the Linux production environment and want to save all terminal session activity for future reference, so the script command becomes convenient in this case.

Script is a command-line tool for capturing / recording your Linux server terminal session activity, which can be replayed later using the scriptreplay command. In this article, we will demonstrate how to install the script command-line tool and record the Linux server terminal session activity, and then we will see how to use the scriptreplay command to replay the recorded session.

Install script tools install script tools on RHEL 7 / CentOS 7

The script command is provided by the RPM package util-linux. If you don't have it installed on your CentOS 7 / RHEL 7 system, run the following yum to install it:

[root@linuxtechi ~] # yum install util-linux-y installs script tools on RHEL 8 / CentOS 8

Run the following dnf command to install the script tool on RHEL 8 / CentOS 8:

[root@linuxtechi ~] # dnf install util-linux-y installs script tools on Debian-based systems (Ubuntu / Linux Mint)

Run the following apt-get command to install the script tool:

Root@linuxtechi ~] # apt-get install util-linux-y how to use the script tool

Use the script command directly, type the script command on the terminal, and then press enter, which will begin to capture the current terminal session activity in a file named typescript.

[root@linuxtechi ~] # scriptScript started, file is typescript [root@linuxtechi ~] #

To stop recording session activity, type the exit command, and then press enter:

[root@linuxtechi ~] # exitexitScript done, file is typescript [root@linuxtechi ~] #

The syntax format of the script command:

~] # script {options} {file_name}

Different options that can be used in the script command:

Options-script-command

Let's start recording the Linux terminal session by executing the script command, and then execute things such as wline route-n free DF-h and free-h, as shown in the following example:

Script-examples-linux-server

As we saw above, the terminal session log is saved in the file typescript:

Now use the cat / vi command to view the contents of the typescript file

[root@linuxtechi ~] # ls-l typescript-rw-r--r--. 1 root root 1861 Jun 21 00:50 typescript [root@linuxtechi ~] #

Typescript-file-content-linux

The above confirms that all commands we executed on the terminal have been saved in the typescript file.

Use custom file names in the script command

Suppose we want to use a custom file name to execute the script command. You can specify the file name after the script command. In the following example, we are using a file named session-log- (current date and time) .txt.

[root@linuxtechi ~] # script sessions-log-$ (date +% d-%m-%Y-%T). TxtScript started, file is sessions-log-21-06-2019-01:37:39.txt [root@linuxtechi ~] #

Now run the command and type exit:

[root@linuxtechi ~] # exitexitScript done, file is sessions-log-21-06-2019-01:37:39.txt [root@linuxtechi ~] # additional commands output to script record file

Assuming that the script command has recorded the command output to a file named session-log.txt, and now we want to append the output of the new session command to that file, we can use the-an option in the script command.

[root@linuxtechi ~] # script-a sessions-log.txtScript started, file is sessions-log.txt [root@linuxtechi ~] # xfs_info / dev/mapper/centos-rootmeta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2746624 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0data = bsize=4096 blocks=10986496 Imaxpct=25 = sunit=0 swidth=0 blksnaming = version 2 bsize=4096 ascii-ci=0 ftype=1log = internal bsize=4096 blocks=5364, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime = none extsz=4096 blocks=0, rtextents=0 [root@linuxtechi ~] # exitexitScript done, file is sessions-log.txt [root@linuxtechi ~] #

To view the updated session records, use the cat session-log.txt command.

Capture commands output to script record files without shell interaction

Suppose we want to capture the output of the command to a session log file, using the-c option, the example is as follows:

[root@linuxtechi] # script-c "uptime & & hostname & & date" root-session.txtScript started, file is root-session.txt 01:57:40 up 2:30, 3 users, load average: 0.00,0.01, 0.05linuxtechiFri Jun 21 01:57:40 EDT 2019Script done, file is root-session.txt [root@linuxtechi ~] # run the script command in silent mode

To run the script command in silent mode, use the-Q option, which suppresses startup and completion messages for script, as shown in the following example:

[root@linuxtechi] # script-c "uptime & & date"-Q root-session.txt 02:01:10 up 2:33, 3 users, load average: 0.00,0.01, 0.05Fri Jun 21 02:01:10 EDT 2019 [root@linuxtechi] #

To record the timing information to a file and capture the command output to a separate file, this can be achieved by passing the timing file (- timing) in the script command, as shown in the following example:

Syntax format:

~] # script-t {file_name} [root@linuxtechi ~] # script-- timing=timing.txt session.logScript started, file is session.log [root@linuxtechi ~] # uptime 02:27:59 up 3:00, 3 users, load average: 0.00,0.01 [root@linuxtechi] # dateFri Jun 21 02:28:02 EDT 2019 [root@linuxtechi] # free-h total used free shared buff/cache availableMem: 3.9G 171M 2.0G 8.6m 1.7G 3.3GSwap: 3.9G 0B 3.9G [root@linuxtechi] # whoamiroot [root@linuxtechi ~] # exitexitScript done File is session.log [root@linuxtechi ~] # [root@linuxtechi ~] # ls-l session.log timing.txt-rw-r--r--. 1 root root 673 Jun 21 02:28 session.log-rw-r--r--. 1 root root 414 Jun 21 02:28 timing.txt [root@linuxtechi ~] # playback recorded Linux terminal session activity

Now, use the scriptreplay command to replay the recorded terminal session activity.

Note: scriptreplay is also provided by the RPM package util-linux. The scriptreplay command requires a timing file to work.

[root@linuxtechi] # scriptreplay-- timing=timing.txt session.log

The output of the above command will be as follows

Record the Linux terminal session activity of all users

On some business-critical Linux servers, we want to track the activities of all users, which can be done using the script command, putting the following in the / etc/profile file

[root@linuxtechi] # vi / etc/profile... If ["x$SESSION_RECORD" = "x"] thentimestamp=$ (date +% d-%m-%Y-%T) session_log=/var/log/session/session.$USER.$$.$timestampSESSION_RECORD=startedexport SESSION_RECORDscript-t-f-Q2 > ${session_log} .Tim ing $session_logexitfi …

Save the file and exit.

Create a session directory under the / var/log folder:

[root@linuxtechi ~] # mkdir / var/log/session

Assign permissions to the folder:

[root@linuxtechi ~] # chmod 777 / var/log/session/ [root@linuxtechi ~] #

Now, verify that the above code is valid. In the case where I am using the pkumar user, log in to the normal user to the Linux server:

~] # ssh root@linuxtechiroot@linuxtechi's password: [root@linuxtechi ~] $uptime 04:34:09 up 5:06, 3 users, load average: 0.00,0.01 [root@linuxtechi] $dateFri Jun 21 04:34:11 EDT 2019 [root@linuxtechi] $free-h total used free shared buff/cache availableMem: 3.9G 172M 2.0G 8.6m 1.7G 3.3GSwap: 3.9G 0B 3.9G [root@linuxtechi] $iduid=1001 (pkumar ) gid=1002 (pkumar) groups=1002 (pkumar) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 [root@linuxtechi ~] $whoamipkumar [root@linuxtechi ~] $exit Login as root and view user's linux terminal session activity [root@linuxtechi ~] # cd / var/log/session/ [root@linuxtechi session] # ls-l | grep pkumar-rw-rw-r--. 1 pkumar pkumar 870 Jun 21 04:34 session.pkumar.19785.21-06-2019-04 session.pkumar.19785.21-2019-04 ghetto 34 purl 05Mai RWMUR RWMUR. 1 pkumar pkumar 494 Jun 21 04:34 session.pkumar.19785.21-06-2019-04:34:05.timing [root@linuxtechi session] #

Session-output-file-linux

We can also use the scriptreplay command to replay the user's terminal session activity:

[root@linuxtechi session] # scriptreplay-- timing session.pkumar.19785.21-06-2019-04\: 34\: 05.timing session.pkumar.19785.21-06-2019-04\: 34\: 05 Thank you for reading. This is the content of "how to record and replay a terminal session on Linux". After the study of this article, I believe you have a deeper understanding of how to record and replay a terminal session on Linux. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report