Skip to Content

Record and Replay Linux Commands

How to record and replay SSH sessions using the script and scriptreplay commands?

Install script and scriptreplay using the below command in CentOS/Ubuntu.

CentOS/RHEL/Amazon Linux

[email protected] ~]# yum install util-linux -y

Ubuntu/Debian

[email protected] ~]# apt install bsdutils -y

How to use the script command?

Run below command to view the information for the script command.

[email protected] ~]# script -help

1. Run the below commands to start and stop the recording. In the below example uptime command and output are saved into the type.log file.

[email protected] ~]# script -a type.log

[email protected] ~]# uptime

  08:36:47 up 6:58, 2 users, load average: 0.00, 0.02, 0.00

[email protected] ~]# exit

2. Run the below command to save single command into a file.

[email protected] ~]# script -c 'uptime' type.log

Script started, file is type6.log
08:35:26 up 6:57, 2 users, load average: 0.00, 0.02, 0.00
Script done, file is type6.log

3. Run the below command to save the session with the timing file to replay the script on the terminal.

[email protected] ~]# script --timing=time.txt type.log

[email protected] ~]# uptime
  08:49:00 up 7:10, 2 users, load average: 0.01, 0.05, 0.02

[email protected] ~]# exit

View the recorded file using the cat command.

[email protected] ~]# cat type.log

How to use the scriptreplay command?

Run the below command to view the information for the scriptreplay command.

[email protected] ~]# scriptreplay -help

Run below command to view the recording using timing information.

Time files and record files are needed to replay the scripts.

Refer below example to save time file and record file to replay the script using the scriptreplay command.

[email protected] ~]# script --timing=time.txt type.log

[email protected] ~]# uptime
  08:49:00 up 7:10, 2 users, load average: 0.01, 0.05, 0.02

[email protected] ~]# exit

Run the scriptreplay command to reply the recorded session. scriptreplay command will automatically display all the commands that are run on the terminal and will be played according to the information saved in the time file.

[email protected] ~]# scriptreplay --timing=time.txt type.log

[email protected] ~]# uptime
  08:49:00 up 7:10, 2 users, load average: 0.01, 0.05, 0.02

[email protected] ~]# exit

exit

[email protected] ~]# 

 

Related Articles