public
Last active

Record the terminal session and replay later

  • Download Gist
recterm.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
#!/bin/bash
# Uses script and scriptreplay to record and playback virtual terminal.
# Copyright (C) 2007 Hean Kuan Ong <mysurface@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# This format of script was created by Henrique Moody <henriquemoody@gmail.com>.
#
# Authors
# =======
# Hean Kuan Ong <mysurface@gmail.com>
# Henrique Moody <henriquemoody@gmail.com>
#
# Changelog
# =========
# 1.2.1 Remove temporary file when update fails.
# 1.2.0 Created a self updater.
# 1.1.0 Shortened long times in the session time file.
# 1.0.0 Updated script interface and help.
#
 
SCRIPT_NAME=$(basename "${0}")
SCRIPT_VERSION=$(sed -n 28p "${0}" | awk '{print $2}')
HELP_MESSAGE="Usage: ${SCRIPT_NAME} [OPTION] [SESSION]
Record and playback terminal session.
 
-h, --help Displays this help.
-r, --record Start to record a session.
-p, --play Start to play a session.
-v, --version Displays the version of the program.
-u, --update Self update ${SCRIPT_NAME}.
 
Report bugs to: henriquemoody@gmail.com."
 
for command in "curl" "script" "scriptreplay"
do
if [ ! -n $(which ${command}) ]
then
echo "${command} is not installed, please install."
exit 1
fi
done
 
case ${1} in
 
-h | --help)
 
echo "${HELP_MESSAGE}"
exit 0
 
;;
 
-V)
 
echo "${SCRIPT_VERSION}"
exit 0
 
;;
 
-v | --version)
 
echo "${SCRIPT_NAME} version ${SCRIPT_VERSION}"
echo
sed -n '2,19p' "${0}" | sed -r 's/^# ?//g'
exit 0
 
;;
 
-u | --update)
 
 
TEMPORARY=/tmp/recterm_$(date +%s)
curl -L git.io/recterm -o ${TEMPORARY}
chmod +x ${TEMPORARY}
HEAD_VERSION=$(${TEMPORARY} -V)
if [ "${HEAD_VERSION}" == "${SCRIPT_VERSION}" ]
then
echo "Nothing to update."
echo "The last version of ${SCRIPT_NAME} is ${HEAD_VERSION}."
rm -f ${TEMPORARY}
exit 0
fi
 
if [ ! -w "${0}" ]
then
echo "You don't have permission to update ${SCRIPT_NAME}." 1>&2
rm -f ${TEMPORARY}
exit 3
fi
 
mv ${TEMPORARY} "${0}"
echo "Successfully updated of ${SCRIPT_VERSION} to ${HEAD_VERSION}"
exit 0
 
;;
 
-r | --record)
 
if [ ! -n "${2}" ]
then
read -p "What is your session name? " NAME
else
NAME="${2}"
fi
 
echo "Script recording will start soon, type exit to end the recording."
echo
script -q -t 2> "${NAME}.timing" "${NAME}.session"
sed -r 's/^[1-9][0-9]*\.[0-9]+/1.000000/g' -i "${NAME}.timing"
tar -czf "${NAME}" "${NAME}.timing" "${NAME}.session"
rm "${NAME}.timing" "${NAME}.session"
echo "Salved \"${NAME}\" session."
exit 0
 
;;
 
-p | --play)
 
if [ ! -n "${2}" ]
then
read -p "What session you wanna play? " NAME
else
NAME="${2}"
fi
 
tar -zxf "${NAME}"
scriptreplay "${NAME}.timing" "${NAME}.session"
rm "${NAME}.timing" "${NAME}.session"
echo "That is the end of the script play."
echo
exit 0
 
;;
 
*)
 
echo "${HELP_MESSAGE}" 1>&2
exit 2
 
;;
 
esac
$ cd /usr/local/bin
$ sudo curl -LO git.io/recterm
$ sudo chmod +x recterm
$ recterm --version

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.