]> git.ipfire.org Git - ipfire-2.x.git/blob - src/patches/dnsmasq/0056-New-version-of-contrib-reverse-dns.patch
f6f787366d9c500bbe41c3f539226cbec18ea3f0
[ipfire-2.x.git] / src / patches / dnsmasq / 0056-New-version-of-contrib-reverse-dns.patch
1 From 4c960fa90a975d20f75a1ecabd217247f1922c8f Mon Sep 17 00:00:00 2001
2 From: Simon Kelley <simon@thekelleys.org.uk>
3 Date: Wed, 4 Mar 2015 20:32:26 +0000
4 Subject: [PATCH 56/87] New version of contrib/reverse-dns
5
6 ---
7 contrib/reverse-dns/README | 22 +++---
8 contrib/reverse-dns/reverse_replace.sh | 131 ++++++++++++++++++++++++++++-----
9 2 files changed, 125 insertions(+), 28 deletions(-)
10
11 diff --git a/contrib/reverse-dns/README b/contrib/reverse-dns/README
12 index f87eb77c4c22..2ec4df1f957e 100644
13 --- a/contrib/reverse-dns/README
14 +++ b/contrib/reverse-dns/README
15 @@ -1,18 +1,18 @@
16 -Hi.
17 +The script reads stdin and replaces all IP addresses with names before
18 +outputting it again. IPs from private networks are reverse looked up
19 +via dns. Other IP adresses are searched for in the dnsmasq query log.
20 +This gives names (CNAMEs if I understand DNS correctly) that are closer
21 +to the name the client originally asked for then the names obtained by
22 +reverse lookup. Just run
23
24 -To translate my routers netstat-nat output into names that actually talk
25 -to me I have started writing to simple shell scripts. They require
26 +netstat -n -4 | ./reverse_replace.sh
27 +
28 +to see what it does. It needs
29
30 log-queries
31 log-facility=/var/log/dnsmasq.log
32
33 -to be set. With
34 -
35 -netstat-nat -n -4 | reverse_replace.sh
36 -
37 -I get retranslated output.
38 -
39 -Sincerely,
40 -Joachim
41 +in the dnsmasq configuration.
42
43 +The script runs on debian (with ash installed) and on busybox.
44
45 diff --git a/contrib/reverse-dns/reverse_replace.sh b/contrib/reverse-dns/reverse_replace.sh
46 index a11c164b7f19..5b4aebd71456 100644
47 --- a/contrib/reverse-dns/reverse_replace.sh
48 +++ b/contrib/reverse-dns/reverse_replace.sh
49 @@ -1,28 +1,125 @@
50 -#!/bin/bash
51 -# $Id: reverse_replace.sh 4 2015-02-17 20:14:59Z jo $
52 +#!/bin/ash
53 +# $Id: reverse_replace.sh 18 2015-03-01 16:12:35Z jo $
54 #
55 # Usage e.g.: netstat -n -4 | reverse_replace.sh
56 # Parses stdin for IP4 addresses and replaces them
57 -# with names retrieved by reverse_dns.sh
58 +# with names retrieved by parsing the dnsmasq log.
59 +# This currently only gives CNAMEs. But these
60 +# usually tell ou more than the mones from reverse
61 +# lookups.
62 +#
63 +# This has been tested on debian and asuswrt. Plese
64 +# report successful tests on other platforms.
65 +#
66 +# Author: Joachim Zobel <jz-2014@heute-morgen.de>
67 +# License: Consider this MIT style licensed. You can
68 +# do as you ike, but you must not remove my name.
69 #
70
71 -DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
72 -DNS=$DIR/reverse_dns.sh
73 +LOG=/var/log/dnsmasq.log
74 +MAX_LINES=15000
75
76 -# sed regex
77 +# sed regex do match IPs
78 IP_regex='[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
79 +# private IP ranges
80 +IP_private='\(^127\.\)\|\(^192\.168\.\)\|\(^10\.\)\|\(^172\.1[6-9]\.\)\|\(^172\.2[0-9]\.\)\|\(^172\.3[0-1]\.\)'
81
82 -while read LINE; do
83 - if grep --quiet $IP_regex <<< "$LINE"; then
84 - IPs=`sed "s#.*\b\($IP_regex\)\b.*#\1 #g" <<< "$LINE"`
85 - IPs=($IPs)
86 - for IP in "${IPs[@]}"
87 - do
88 - NAME=`$DNS $IP`
89 - # echo "$NAME is $IP";
90 - LINE="${LINE/$IP/$NAME}"
91 - done
92 +#######################################################################
93 +# Find Commands
94 +
95 +HOST=nslookup
96 +if type host > /dev/null 2>&1; then
97 + # echo "No need for nslookup, host is there"
98 + HOST=host
99 +fi
100 +
101 +#######################################################################
102 +# Functions
103 +
104 +# Use shell variables for an (IP) lookup table
105 +create_lookup_table()
106 +{
107 + # Parse log into lookup table
108 + local CMDS="$( tail -"$MAX_LINES" "$LOG" | \
109 + grep " is $IP_regex" | \
110 + sed "s#.* \([^ ]*\) is \($IP_regex\).*#set_val \2 \1;#" )"
111 +
112 + local IFS='
113 +'
114 + for CMD in $CMDS
115 + do
116 + eval $CMD
117 + done
118 +}
119 +
120 +set_val()
121 +{
122 + local _IP=$(echo $1 | tr . _)
123 + local KEY="__IP__$_IP"
124 + eval "$KEY"=$2
125 +}
126 +
127 +get_val()
128 +{
129 + local _IP=$(echo $1 | tr . _)
130 + local KEY="__IP__$_IP"
131 + eval echo -n '${'"$KEY"'}'
132 +}
133 +
134 +dns_lookup()
135 +{
136 + local IP=$1
137 +
138 + local RTN="$($HOST $IP | \
139 + sed 's#\s\+#\n#g' | \
140 + grep -v '^$' | \
141 + tail -1 | tr -d '\n' | \
142 + sed 's#\.$##')"
143 + if echo $RTN | grep -q NXDOMAIN; then
144 + echo -n $IP
145 + else
146 + echo -n "$RTN"
147 + fi
148 +}
149 +
150 +reverse_dns()
151 +{
152 + local IP=$1
153 +
154 + # Skip if it is not an IP
155 + if ! echo $IP | grep -q "^$IP_regex$"; then
156 + echo -n $IP
157 + return
158 + fi
159 +
160 + # Do a dns lookup, if it is a local IP
161 + if echo $IP | grep -q $IP_private; then
162 + dns_lookup $IP
163 + return
164 fi
165 +
166 + local NAME="$(get_val $IP)"
167 +
168 + if [ -z "$NAME" ]; then
169 + echo -n $IP
170 + else
171 + echo -n $NAME
172 + fi
173 +}
174 +
175 +#######################################################################
176 +# Main
177 +create_lookup_table
178 +
179 +while read LINE; do
180 + for IP in $(echo "$LINE" | \
181 + sed "s#\b\($IP_regex\)\b#\n\1\n#g" | \
182 + grep $IP_regex)
183 + do
184 + NAME=`reverse_dns $IP `
185 + # echo "$NAME $IP"
186 + LINE=`echo "$LINE" | sed "s#$IP#$NAME#" `
187 + done
188 echo $LINE
189 -done < /dev/stdin
190 +done
191
192 --
193 2.1.0
194