-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind_reverse_generator
More file actions
executable file
·177 lines (149 loc) · 3.93 KB
/
bind_reverse_generator
File metadata and controls
executable file
·177 lines (149 loc) · 3.93 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/ksh
##Potaje6
##Script used in a much simpler way in my old times working in the barcelona public transport project generating the zones for each environment
##now expanded with some features to adapt it to more situations
##This generates reverse zones very raw, check everything before putting it to production or restarting bind, headings and configs might need some tweaks, the records part can be ignored, never gave me any problem
##Feel free to add some constraints to dont process CNAMEs more effectively
SERIAL=$(echo $(date +%Y%m%d)01)
#DOMAIN=$1
MASTERS=dns
OCTET=3
DOMAIN=area51.gov
EXEC_DIR=$(dirname $0)
EXEC_NAME=$(basename $0)
EXEC_FULL=${EXEC_DIR}/${EXEC_NAME}
EXEC_TEMP_DIR=${EXEC_DIR}/${EXEC_NAME}.dir
TEMPFILE=${EXEC_FULL}.tmp
#TEMPFILE2=${EXEC_FULL}.tmp2
clean(){
rm -f $EXEC_TEMP_DIR/*
rmdir $EXEC_TEMP_DIR
}
print_uso(){
cat <<EOF
Script to generate reverse zones from bind direct zones
Usage:
./bind_reverse_generator [-d domain ] [-m masters] [-o octets] file
-d domain. Default: $DOMAIN
-m name of the master servers, for example, localdns would be printed as localdns1 and localdns2 in the authoritative servers. Default $MASTERS
-o the number of octets that you want to be cutted, 3 = splitted in zones with just the last octet in each. Default $OCTET
--help show this message
EOF
exit
}
gen_heading(){
cat <<EOF
;/var/named/db.$1.in-addr.arpa.zone
;\$TTL 14400
\$TTL 300
@ IN SOA ${MASTERS}1.${DOMAIN}. ${MASTERS}2.${DOMAIN}. (
$SERIAL
300
7200
1209600
86400 )
;
EOF
}
if [ $# -eq 0 ]; then
print_uso
fi
while [ $# -gt 1 ];
do
case $1 in
"-d") DOMAIN=$2
shift
shift
;;
"-m") MASTERS=$2
shift
shift
;;
"-o") OCTET=$2
shift
shift
;;
"--help") print_uso
;;
*)
shift
esac
done
if [ "$1" == "-h" ] || [ "$1" == "--help" ];then
print_uso
fi
if [ "$OCTET" -gt 3 ] || [ "$OCTET" -lt 1 ]; then
echo "Invalid octet selection, must be between 1 and 3"
print_uso
fi
##Spaghuetti code because fuck optimization and absurd complex stuff
octet1(){
FIELDS=1
}
generator1(){
awk -v DOM="$DOMAIN" {'print $(NF-2)"."$(NF-1)"."$NF"\tIN\tPTR\t"$1"."DOM"."'}
}
reverse1(){
awk -F "." {'print $1'}
}
octet2(){
FIELDS=1,2
}
generator2(){
awk -v DOM="$DOMAIN" {'print $(NF-1)"."$NF"\tIN\tPTR\t"$1"."DOM"."'}
}
reverse2(){
awk -F "." {'print $2"."$1'}
}
octet3(){
FIELDS=1,2,3
}
generator3(){
awk -v DOM="$DOMAIN" {'print $NF"\tIN\tPTR\t"$1"."DOM"."'}
}
reverse3(){
awk -F "." {'print $3"."$2"."$1'}
}
octet$OCTET
ZONE_FILE=$1
test -f $ZONE_FILE || (echo "$ZONE_FILE doesn't exist, exiting..."; exit)
##And this my friends, is what i call a autentica puta mierda, but it works
#If zones dont have the format "$HOST IN A $IP" "grep -f 4" should be changed to 3
cat $ZONE_FILE \
| grep -v ^\; \
| tr -s "\t" " " \
| cut -f 4 -d " " \
| grep ^[0-9] \
| grep \\. \
| cut -f $FIELDS -d . \
| sort -u \
| while read range ; do REVERSED_RANGE=$(echo $range | cut -f $FIELDS -d . | reverse$OCTET);
gen_heading $range \
| tee ${REVERSED_RANGE}.in-addr.arpa.zone.db; grep $range $ZONE_FILE \
| sed 's/\./ /g' > $TEMPFILE; cat $TEMPFILE \
| generator${OCTET} \
| grep -v ^\; \
| tee -a ${REVERSED_RANGE}.in-addr.arpa.zone.db; done
##Lazyness protocol ENGAGED
#If zones dont have the format "$HOST IN A $IP" "grep -f 4" should be changed to 3
echo
echo "##this is a sample of what would look like the config for the zones generated in your master, tweak it to your environment"
echo
cat $ZONE_FILE \
| grep -v ^\; \
| tr -s "\t" " " \
| cut -f 4 -d " " \
| grep ^[0-9] \
| grep \\. \
| cut -f $FIELDS -d . \
| sort -u \
| reverse$OCTET \
| while read range; do
cat <<EOF
zone "$range.in-addr.arpa" in {
type master;
file "$range.in-addr.arpa.zone.db";
};
EOF
done
test -f $TEMPFILE && rm $TEMPFILE