Record command lines / terminal sessions on UNIX shell

Most of the IT professionals often need to record whatever is displayed on their windows screen using different video recording tools. However, when comes to different UNIX (LINUX, Solaris, AIX etc…) environments, sometimes it is not possible to just rely on history command output from terminal session. As history doesn’t record terminal session output of CLIs that we executed. (I am not referring to putty sessions which can also be recorded from windows using free video recording tools).

This post tries to dig into one of the known ways, but often ignored, for recording terminal session or command lines on UNIX shell.

# script test.txt
Script started, file is test.txt
# echo Hi
Hi
# echo This is a script recording.
This is a script recording.
# exit

Above ‘script’ CLI when invoked starts shell recording into test.txt file. Now ‘test.txt’ file contains all the commands or CLIs along with their output that we executed in terminal session. Below is output of ‘cat’ CLI.

# cat test.txt
Script started on Tue Dec 15 15:48:13 2009
# echo Hi
Hi
# echo This is a script recording.
This is a script recording.
# exit

Script done on Tue Dec 15 15:48:39 2009
#

However, if default shell is other than ‘sh’ then “test.txt” might contain ^M characters as below.

# vi test.txt
Script started on Tue Dec 15 15:48:13 2009
^[[?1034h^[[1m^[[31m # ^[(B^[[mecho Hi^M
Hi^M
^[[1m^[[31m # ^[(B^[[mecho This is a script recodr^H^[[K^H^[[Krding.^M
This is a script recording.^M
^[[1m^[[31m # ^[(B^[[mexit^M

Script done on Tue Dec 15 15:48:39 2009
#

Hence, ‘script’ CLI works best in environments when there is little manipulation of STDOUT. However, this can easily filtered out using ‘sed’.

In next post, we will see how to use script CLI to verbosely log all terminal session activities by default.

Reblog this post [with Zemanta]

By scimandar

3 comments:

  1. Hi Mandar,

    For bash shell if you use script to record the outpt. If the text is formatted then how to avoid the special characters in the file?

    -Amol Bhor

    ReplyDelete
  2. You can use 'sh' instead of 'bash' or any other shell....
    Following CLI should work.

    # SHELL=/usr/bin/sh script record.txt

    ReplyDelete
  3. Hey, you should try http://shelr.tv/

    I'ts like script on steroids. Allows to publish records online etc etc.

    ReplyDelete

Your valuable comments are welcome. (Comments will be moderated.)

Instagram