Skip to content

Instantly share code, notes, and snippets.

@henriquemoody
Created May 8, 2012 21:41
Show Gist options
  • Save henriquemoody/2639596 to your computer and use it in GitHub Desktop.
Save henriquemoody/2639596 to your computer and use it in GitHub Desktop.
Record the terminal session and replay later
#!/bin/bash
# Uses script and scriptreplay to record and playback virtual terminal.
# Copyright (C) 2007 Hean Kuan Ong <[email protected]>
#
# 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 <[email protected]>.
#
# Authors
# =======
# Hean Kuan Ong <[email protected]>
# Henrique Moody <[email protected]>
#
# 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: [email protected]."
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
@henriquemoody
Copy link
Author

$ cd /usr/local/bin
$ sudo curl -LO git.io/recterm
$ sudo chmod +x recterm
$ recterm --version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment