-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-record.sh
More file actions
executable file
·160 lines (146 loc) · 4 KB
/
script-record.sh
File metadata and controls
executable file
·160 lines (146 loc) · 4 KB
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
155
156
157
158
159
160
#!/bin/bash
###############################################################################
# linux script recording & replay wrapper
# written by George Liu <https://centminmod.com>
###############################################################################
VER=0.2
DEBUG='n'
CPUS=$(nproc)
if [ -f /usr/bin/yum ] && [[ ! -f /usr/bin/scriptreplay || ! -f /usr/bin/script ]]; then
sudo yum -q -y install util-linux
elif [ -f /usr/bin/apt ] && [[ ! -f /usr/bin/scriptreplay || ! -f /usr/bin/script ]]; then
sudo apt install util-linux
fi
if [[ -f /usr/bin/yum && ! -f /usr/bin/tree ]]; then
sudo yum -q -y install tree
elif [[ -f /usr/bin/apt && ! -f /usr/bin/tree ]]; then
sudo apt install tree
fi
if [[ -f /usr/bin/yum && ! -f /usr/bin/pigz && ! -f /usr/local/bin/pigz ]]; then
sudo yum -q -y install pigz
elif [[ -f /usr/bin/apt && ! -f /usr/bin/pigz && ! -f /usr/local/bin/pigz ]]; then
sudo apt install pigz
fi
if [[ -f /usr/local/bin/pigz && "$CPUS" -gt '1' ]]; then
COMPRESSBIN='/usr/local/bin/pigz'
elif [[ -f /usr/bin/pigz && "$CPUS" -gt '1' ]]; then
COMPRESSBIN='/usr/bin/pigz'
else
COMPRESSBIN='/usr/bin/gzip'
fi
if [ -f /usr/bin/pzcat ]; then
ZCATBIN='/usr/bin/pzcat'
elif [ -f /usr/bin/zcat ]; then
ZCATBIN='/usr/bin/zcat'
fi
disable_motd() {
if [ -f /etc/profile.d/dmotd.sh ]; then
# temp disable Centmin Mod dmotd.sh
mv /etc/profile.d/dmotd.sh /etc/profile.d/dmotd.sh-disabled
if [[ "$DEBUG" = [yY] ]]; then
ls -lAh /etc/profile.d/dmotd.sh*
fi
fi
}
reenable_motd() {
if [ -f /etc/profile.d/dmotd.sh-disabled ]; then
# temp disable Centmin Mod dmotd.sh
mv /etc/profile.d/dmotd.sh-disabled /etc/profile.d/dmotd.sh
if [[ "$DEBUG" = [yY] ]]; then
ls -lAh /etc/profile.d/dmotd.sh*
fi
fi
}
record() {
SESSION_NAME="$1"
FILEPREFIX="$HOME/.script/$(date '+%Y-%m-%d')/$(date '+%Y-%m-%d_%H-%M-%S')-${SESSION_NAME:-session}"
mkdir -p $FILEPREFIX
disable_motd
/usr/bin/script -t -f -q 2>"${FILEPREFIX}/time.txt" "${FILEPREFIX}/cmds"
$COMPRESSBIN "${FILEPREFIX}/time.txt"
$COMPRESSBIN "${FILEPREFIX}/cmds"
echo
echo "files saved:"
echo -e "${FILEPREFIX}/cmds.gz\n${FILEPREFIX}/time.txt.gz\n"
echo "to replay 1x speed:"
echo "$0 play ${FILEPREFIX}/cmds.gz ${FILEPREFIX}/time.txt.gz"
echo
echo "to replay 2x speed:"
echo "$0 play ${FILEPREFIX}/cmds.gz ${FILEPREFIX}/time.txt.gz 2"
}
replay() {
CMD_FILE="$1"
TIME_FILE="$2"
SPEED="$3"
if [[ -f "$CMD_FILE" && -f "$TIME_FILE" ]]; then
if [ "$3" ]; then
scriptreplay -t <("$ZCATBIN" "$TIME_FILE") <("$ZCATBIN" "$CMD_FILE") "$SPEED"
else
scriptreplay -t <("$ZCATBIN" "$TIME_FILE") <("$ZCATBIN" "$CMD_FILE")
fi
else
echo
echo "error: required file path(s) to cmds.gz or time.txt.gz do not exist"
echo
echo "existing files detected:"
/usr/bin/tree --charset utf8 --sort=ctime -n -f "$HOME/.script/"
echo
help
fi
}
replay_nogz() {
CMD_FILE="$1"
TIME_FILE="$2"
SPEED="$3"
if [[ -f "$CMD_FILE" && -f "$TIME_FILE" ]]; then
if [ "$3" ]; then
scriptreplay -t "$TIME_FILE" "$CMD_FILE" "$SPEED"
else
scriptreplay -t "$TIME_FILE" "$CMD_FILE"
fi
else
echo
echo "error: required file path(s) to cmds.gz or time.txt.gz do not exist"
echo
echo "existing files detected:"
/usr/bin/tree --charset utf8 --sort=ctime -n -f "$HOME/.script/"
echo
help
fi
}
list_files() {
echo "saved files listing:"
echo
/usr/bin/tree --charset utf8 --sort=ctime -n -f "$HOME/.script/"
echo
}
help() {
echo
echo "Usage:"
echo
echo "$0 rec SESSION_NAME"
echo "$0 play /path/to/cmds.gz /path/to/time.txt.gz"
echo "$0 play /path/to/cmds.gz /path/to/time.txt.gz 2"
echo "$0 play-nogz /path/to/cmds /path/to/time.txt"
echo "$0 play-nogz /path/to/cmds /path/to/time.txt 2"
echo "$0 list"
echo
}
trap reenable_motd EXIT
case "$1" in
rec )
record "$2"
;;
play )
replay "$2" "$3" "$4"
;;
play-nogz )
replay_nogz "$2" "$3" "$4"
;;
list )
list_files
;;
* )
help
;;
esac