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
root@centos ~]# yum install util-linux -y
Ubuntu/Debian
root@ubuntu ~]# apt install bsdutils -y
How to use the script command?
Run below command to view the information for the script command.
root@linux ~]# 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.
root@linux ~]# script -a type.log
root@linux ~]# uptime
08:36:47 up 6:58, 2 users, load average: 0.00, 0.02, 0.00
root@linux ~]# exit
2. Run the below command to save single command into a file.
root@linux ~]# 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.
root@linux ~]# script --timing=time.txt type.log
root@linux ~]# uptime
08:49:00 up 7:10, 2 users, load average: 0.01, 0.05, 0.02
root@linux ~]# exit
View the recorded file using the cat command.
root@linux ~]# cat type.log
How to use the scriptreplay command?
Run the below command to view the information for the scriptreplay command.
root@linux ~]# scriptreplay -help
Run below command to view the recording using timing information.
Refer below example to save time file and record file to replay the script using the scriptreplay command.
root@linux ~]# script --timing=time.txt type.log
root@linux ~]# uptime
08:49:00 up 7:10, 2 users, load average: 0.01, 0.05, 0.02
root@linux ~]# 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.
root@linux ~]# scriptreplay --timing=time.txt type.log
root@linux ~]# uptime
08:49:00 up 7:10, 2 users, load average: 0.01, 0.05, 0.02
root@linux ~]# exit
exit
root@linux ~]#
Related Articles