Join the community today
Become a Member

Sysadmin SSH terminal session recording and playback with script/scriptreplay

Discussion in 'System Administration' started by eva2000, Aug 26, 2022.

  1. eva2000

    eva2000 Administrator Staff Member

    49,299
    11,296
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,575
    Local Time:
    11:33 AM
    Nginx 1.21.x
    MariaDB 10.x
    Thought I'd share this useful tip where you can record your SSH terminal session and commands and actually play them back later via script/scriptreplay commands Record your terminal with script and scriptreplay.

    I wrote a wrapper script called script-record.sh at GitHub - centminmod/scriptreplay: script and scriptreplay wrapper script which makes it easier to use script and scriptreplay commands.

    Installation & Updates


    Code (Text):
    curl -sL https://github.com/centminmod/scriptreplay/raw/master/script-record.sh -o /usr/local/bin/script-record
    chmod +x /usr/local/bin/script-record
    

    Usage help file
    • rec - enter your SESSION_NAME. If you do not pass a session name, the script will automatically name the session as session.
    • play - pass the full paths to where the cmds.gz and time.txt.gz files are saved. You can optionally pass a 3rd arugument for playback speed i.e. pass 2 for 2x playback speed.
    • play-nogz - pass the full paths to where the uncompressed non-gzip cmds and time.txt files are saved. You can optionally pass a 3rd arugument for playback speed i.e. pass 2 for 2x playback speed.
    • list - list all previously saved cmds.gz and time.txt.gz files.
    Code (Text):
    script-record
    
    Usage:
    
    /usr/local/bin/script-record rec SESSION_NAME
    /usr/local/bin/script-record play /path/to/cmds.gz /path/to/time.txt.gz
    /usr/local/bin/script-record play /path/to/cmds.gz /path/to/time.txt.gz 2
    /usr/local/bin/script-record play-nogz /path/to/cmds /path/to/time.txt
    /usr/local/bin/script-record play-nogz /path/to/cmds /path/to/time.txt 2
    /usr/local/bin/script-record list
    


    Record SSH Terminal Session



    Start an SSH terminal session recording
    Code (Text):
    ./script-record.sh rec cmds1
     your_ssh_commands
    

    To exit and save the recorded session, type exit command which will output the where the cmds.gz and time.txt.gz files are saved and also the exact command used for scriptreplay playback:
    Code (Text):
    exit
    
    files saved:
    /root/.script/2022-08-25/2022-08-25_23-01-40-cmds1/cmds.gz
    /root/.script/2022-08-25/2022-08-25_23-01-40-cmds1/time.txt.gz
    
    to replay 1x speed:
    ./script-record play /root/.script/2022-08-25/2022-08-25_23-01-40-cmds1/cmds.gz /root/.script/2022-08-25/2022-08-25_23-01-40-cmds1/time.txt.gz
    
    to replay 2x speed:
    ./script-record play /root/.script/2022-08-25/2022-08-25_23-01-40-cmds1/cmds.gz /root/.script/2022-08-25/2022-08-25_23-01-40-cmds1/time.txt.gz 2
    


    List



    List previously saved SSH terminal sessions
    Code (Text):
    script-record list
    saved files listing:
    
    /root/.script
    ├── /root/.script/2022-08-25
    │   ├── /root/.script/2022-08-25/2022-08-25_22-44-10-cmds1
    │   │   ├── /root/.script/2022-08-25/2022-08-25_22-44-10-cmds1/cmds.gz
    │   │   └── /root/.script/2022-08-25/2022-08-25_22-44-10-cmds1/time.txt.gz
    │   ├── /root/.script/2022-08-25/2022-08-25_23-01-40-cmds1
    │   │   ├── /root/.script/2022-08-25/2022-08-25_23-01-40-cmds1/cmds.gz
    │   │   └── /root/.script/2022-08-25/2022-08-25_23-01-40-cmds1/time.txt.gz
    │   ├── /root/.script/2022-08-25/2022-08-25_23-40-10-cmds1
    │   │   ├── /root/.script/2022-08-25/2022-08-25_23-40-10-cmds1/cmds.gz
    │   │   └── /root/.script/2022-08-25/2022-08-25_23-40-10-cmds1/time.txt.gz
    │   └── /root/.script/2022-08-25/2022-08-25_23-42-06-cmds1
    │       ├── /root/.script/2022-08-25/2022-08-25_23-42-06-cmds1/cmds.gz
    │       └── /root/.script/2022-08-25/2022-08-25_23-42-06-cmds1/time.txt.gz
    └── /root/.script/2022-08-26
        └── /root/.script/2022-08-26/2022-08-26_00-33-21-cmd1
            ├── /root/.script/2022-08-26/2022-08-26_00-33-21-cmd1/cmds.gz
            └── /root/.script/2022-08-26/2022-08-26_00-33-21-cmd1/time.txt.gz
    
    7 directories, 10 files
    


    Replay Recorded SSH Terminal Session



    Proper scriptreplay passing the previously saved cmds.gz and time.txt.gz files - single line command below:
    Code (Text):
    ./script-record.sh play /root/.script/2022-08-25/2022-08-25_23-42-06-cmds1/cmds.gz /root/.script/2022-08-25/2022-08-25_23-42-06-cmds1/time.txt.gz
    

    You can also speedup playback by passing a 3rd argument for playback speed i.e. 2x speed
    Code (Text):
    ./script-record.sh play /root/.script/2022-08-25/2022-08-25_23-42-06-cmds1/cmds.gz /root/.script/2022-08-25/2022-08-25_23-42-06-cmds1/time.txt.gz 2
    


    Example playback on Centmin Mod 130.00beta01 installed system on Alma Linux 8.6 test server.

    script-record-playback-01.gif

    Enjoy :D

     
    Last edited: Aug 27, 2022
  2. eva2000

    eva2000 Administrator Staff Member

    49,299
    11,296
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,575
    Local Time:
    11:33 AM
    Nginx 1.21.x
    MariaDB 10.x
    Thought I'd do a Centmin Mod 130.00beta01 installation on Alma Linux 8.6 and record the SSH terminal session using above script-record.sh script and replay it below :D