-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhcpdcleanup.sh
115 lines (86 loc) · 2.91 KB
/
dhcpdcleanup.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o noglob
set -o pipefail
SELF="$(basename $0)"
DRYRUN=${DRYRUN:-"no"}
REMOVE=${REMOVE:-"no"}
SILENT=${SILENT:-"yes"}
OVERWRITE=${OVERWRITE:-"no"}
TRACE=${TRACE:-no}
[ "$TRACE" != "no" ] && set -x
usage () {
cat <<-EOU
Usage: ${SELF} FILE
Sanitize dhcpd.conf file
Environment (default):
DRYRUN=[yes|no] Don't touch the file (no)
OVERWRITE=[yes|no] Overwrite original file (no)
REMOVE=[yes|no] Don't correct the error, remove the line (no)
SILENT=[yes|no] Don't display catched lines (yes)
TRACE=[yes|no] Trace script (no)
Example:
OVERWRITE=yes ./${SELF} ./dhcpd.conf
EOU
}
fix_pattern () {
typeset pattern="$1"; shift
typeset ifile="$1"; shift
[ "$DRYRUN" != "yes" ] && {
sed --in-place --regexp-extended "$pattern" "$ifile"
} || return 0
}
remove_pattern () {
typeset pattern="$1"; shift
typeset ifile="$1"; shift
pattern="$(echo $pattern | awk -F'/' '{print $2}')"
[ "$DRYRUN" != "yes" ] && {
fix_pattern "/${pattern}/d" "$ifile"
}
}
fixit () {
typeset pattern="$1"; shift
typeset ifile="$1"; shift
[ "$REMOVE" == "yes" ] && {
remove_pattern "$pattern" "$ifile"
} || {
fix_pattern "$pattern" "$ifile"
}
}
get_pattern () {
typeset pattern="$1"; shift
typeset ifile="$1"; shift
typeset O=$(egrep --regexp="$pattern" "$ifile" | awk '{print $3}' | tr --delete ';')
[ "$SILENT" == "yes" ] && return 0
[ "${O}x" != "x" ] && echo "$O" || return 0
}
main () {
typeset FILENAME=${1:?usage;exit 1;}
case "$FILENAME" in
-h|--help) usage; exit 0;;
esac
typeset filedir=$(dirname "$FILENAME")
typeset ofile="$(mktemp --tmpdir="${filedir}" dhcpd.conf.tmp.XXXXXXX)"
cp "$FILENAME" "$ofile"
get_pattern "\s(..:){4}..;" "$ofile"
fixit 's/ethernet (..:..:..:..:..);/ethernet 00:\1;/' "$ofile"
get_pattern "\s(..:){6}..;" "$ofile"
fixit 's/\s..:((..:){5}..);/\s\1/' "$ofile"
get_pattern "ethernet ..:..:..:..:.:..;" "$ofile"
fixit 's/ethernet (..:..:..:..):(.):(..;)/ethernet \1:0\2:\3/' "$ofile"
get_pattern "ethernet ..:..:..:..:..:.;" "$ofile"
fixit 's/ethernet (..:..:..:..:..):(.;)/ethernet \1:0\2/' "$ofile"
get_pattern "ethernet ..:..:.:..:..:..;" "$ofile"
fixit 's/ethernet (..:..):(.):(..:..:..;)/ethernet \1:0\2:\3/' "$ofile"
get_pattern "ethernet ..:.:..:..:..:..;" "$ofile"
fixit 's/ethernet (..):(.):(..:..:..:..;)/ethernet \1:0\2:\3/' "$ofile"
get_pattern "ethernet .:..:..:..:..:..;" "$ofile"
fixit 's/ethernet (.:..:..:..:..:..;)/ethernet 0\1/' "$ofile"
get_pattern "(subnet.*[0-9]){" "$ofile"
fix_pattern 's/(subnet.*[0-9])\{/\1 \{/' "$ofile" # Make sure we don't delete the line
typeset OUTPUT="/dev/stdout"
[ "$OVERWRITE" == "yes" ] && OUTPUT="$FILENAME"
cat "$ofile" > "$OUTPUT" && rm "$ofile"
}
main $*