-
Notifications
You must be signed in to change notification settings - Fork 0
/
linksys-config-repack.sh
executable file
·94 lines (76 loc) · 2.09 KB
/
linksys-config-repack.sh
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
#!/bin/bash
# linksys-config-repack 2017-05-16
# This tool repacks/modifies a Linksys backup file
# and allows to change some config settings not available via GUI
#
# Usage example:
# ./linksys-config-repack.sh -f ./backup.cfg -o ./backup_new.cfg -s "s:rip_interface_wan=1:rip_interface_wan=0:"
#
# (C) 2017 Rustam Tsurik
mk_tmp_dirs() {
TMP_DIR=$(mktemp -d ${TMPDIR:-/var/tmp}/linksys-config-repack-XXXXXXXXXX)
trap "rm -rf $CHROOT_DIR" EXIT TERM INT
}
backup_unpack() {
printf >&2 'Unpacking the backup... '
# skip the header and unpack
dd if="${BACKUP_FILENAME}" skip=12 bs=1 2>/dev/null | tar -xz -C "${TMP_DIR}"
[ -f "${TMP_DIR}/tmp/syscfg.tmp" ] || exit 1; # need a proper error reporting here?
printf >&2 '\t [done]\n'
}
backup_repack() {
printf >&2 'Repacking... '
REPACKED_PAYLOAD="${TMP_DIR}/payload.tar.gz"
REPACKED_FILE="${TMP_DIR}/repacked_backup.data"
# create a payload, the just a regular tar.gz archive
tar -c -C "${TMP_DIR}/" tmp var | gzip --best > "${REPACKED_PAYLOAD}"
PAYLOAD_SIZE=$(wc -c "${REPACKED_PAYLOAD}" | awk '{print $1}')
# header + payload
printf "0x0002\x0a" > "${REPACKED_FILE}"
printf "${PAYLOAD_SIZE}\x0a" >> "${REPACKED_FILE}"
cat "${REPACKED_PAYLOAD}" >> "${REPACKED_FILE}"
if [ -z $OUTPUT_FILENAME ] ; then
cat "$REPACKED_FILE"
else
mv "$REPACKED_FILE" "$OUTPUT_FILENAME"
fi
printf >&2 '\t\t\t [done]\n'
}
backup_not_found() {
printf >&2 "%s: backup file not found\n" "$0"
exit 1;
}
edit_configs() {
printf >&2 "Updating tmp/syscfg.tmp"
sed -i "$TMP_DIR/tmp/syscfg.tmp" -e "$SED_RULE"
printf >&2 "\t\t [done]\n"
}
usage() {
printf >&2 '%s: [-f filename] [-o output filename] [-s sed rule]\n' "$0"
exit 1;
}
# initialize variables
BACKUP_FILENAME=""
OUTPUT_FILENAME=""
SED_RULE=""
while getopts "hf:o:s:" opt; do
case $opt in
f)
BACKUP_FILENAME=$OPTARG
;;
o) OUTPUT_FILENAME=$OPTARG
;;
s) SED_RULE=$OPTARG
;;
*)
usage
;;
esac
done
[ -z $BACKUP_FILENAME ] && usage
[ -z $OUTPUT_FILENAME ] && usage
[ -f $BACKUP_FILENAME ] || backup_not_found
mk_tmp_dirs
backup_unpack
[ -z $SED_RULE ] || edit_configs
backup_repack