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/71] New version of contrib/reverse-dns
7 contrib/reverse-dns/README | 22 +++---
8 contrib/reverse-dns/reverse_replace.sh | 131 ++++++++++++++++++++++++++++-----
9 2 files changed, 125 insertions(+), 28 deletions(-)
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
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
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
28 +to see what it does. It needs
31 log-facility=/var/log/dnsmasq.log
35 -netstat-nat -n -4 | reverse_replace.sh
37 -I get retranslated output.
41 +in the dnsmasq configuration.
43 +The script runs on debian (with ash installed) and on busybox.
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
51 -# $Id: reverse_replace.sh 4 2015-02-17 20:14:59Z jo $
53 +# $Id: reverse_replace.sh 18 2015-03-01 16:12:35Z jo $
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
63 +# This has been tested on debian and asuswrt. Plese
64 +# report successful tests on other platforms.
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.
71 -DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
72 -DNS=$DIR/reverse_dns.sh
73 +LOG=/var/log/dnsmasq.log
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\}'
80 +IP_private='\(^127\.\)\|\(^192\.168\.\)\|\(^10\.\)\|\(^172\.1[6-9]\.\)\|\(^172\.2[0-9]\.\)\|\(^172\.3[0-1]\.\)'
83 - if grep --quiet $IP_regex <<< "$LINE"; then
84 - IPs=`sed "s#.*\b\($IP_regex\)\b.*#\1 #g" <<< "$LINE"`
86 - for IP in "${IPs[@]}"
89 - # echo "$NAME is $IP";
90 - LINE="${LINE/$IP/$NAME}"
92 +#######################################################################
96 +if type host > /dev/null 2>&1; then
97 + # echo "No need for nslookup, host is there"
101 +#######################################################################
104 +# Use shell variables for an (IP) lookup table
105 +create_lookup_table()
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;#" )"
122 + local _IP=$(echo $1 | tr . _)
123 + local KEY="__IP__$_IP"
129 + local _IP=$(echo $1 | tr . _)
130 + local KEY="__IP__$_IP"
131 + eval echo -n '${'"$KEY"'}'
138 + local RTN="$($HOST $IP | \
139 + sed 's#\s\+#\n#g' | \
141 + tail -1 | tr -d '\n' | \
143 + if echo $RTN | grep -q NXDOMAIN; then
154 + # Skip if it is not an IP
155 + if ! echo $IP | grep -q "^$IP_regex$"; then
160 + # Do a dns lookup, if it is a local IP
161 + if echo $IP | grep -q $IP_private; then
166 + local NAME="$(get_val $IP)"
168 + if [ -z "$NAME" ]; then
175 +#######################################################################
180 + for IP in $(echo "$LINE" | \
181 + sed "s#\b\($IP_regex\)\b#\n\1\n#g" | \
184 + NAME=`reverse_dns $IP `
186 + LINE=`echo "$LINE" | sed "s#$IP#$NAME#" `