]>
git.ipfire.org Git - people/ms/dnsmasq.git/blob - contrib/reverse-dns/reverse_replace.sh
2 # $Id: reverse_replace.sh 18 2015-03-01 16:12:35Z jo $
4 # Usage e.g.: netstat -n -4 | reverse_replace.sh
5 # Parses stdin for IP4 addresses and replaces them
6 # with names retrieved by parsing the dnsmasq log.
7 # This currently only gives CNAMEs. But these
8 # usually tell ou more than the mones from reverse
11 # This has been tested on debian and asuswrt. Plese
12 # report successful tests on other platforms.
14 # Author: Joachim Zobel <jz-2014@heute-morgen.de>
15 # License: Consider this MIT style licensed. You can
16 # do as you ike, but you must not remove my name.
19 LOG
=/var
/log
/dnsmasq.log
22 # sed regex do match IPs
23 IP_regex
='[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
25 IP_private
='\(^127\.\)\|\(^192\.168\.\)\|\(^10\.\)\|\(^172\.1[6-9]\.\)\|\(^172\.2[0-9]\.\)\|\(^172\.3[0-1]\.\)'
27 #######################################################################
31 if type host > /dev
/null
2>&1; then
32 # echo "No need for nslookup, host is there"
36 #######################################################################
39 # Use shell variables for an (IP) lookup table
42 # Parse log into lookup table
43 local CMDS
="$( tail -"$MAX_LINES" "$LOG" | \
44 grep " is
$IP_regex" | \
45 sed "s
#.* \([^ ]*\) is \($IP_regex\).*#set_val \2 \1;#" )"
57 local _IP
=$
(echo $1 |
tr . _
)
58 local KEY
="__IP__$_IP"
64 local _IP
=$
(echo $1 |
tr . _
)
65 local KEY
="__IP__$_IP"
66 eval echo -n '${'"$KEY"'}'
73 local RTN
="$($HOST $IP | \
76 tail -1 | tr -d '\n' | \
78 if echo $RTN |
grep -q NXDOMAIN
; then
89 # Skip if it is not an IP
90 if ! echo $IP |
grep -q "^$IP_regex$"; then
95 # Do a dns lookup, if it is a local IP
96 if echo $IP |
grep -q $IP_private; then
101 local NAME
="$(get_val $IP)"
103 if [ -z "$NAME" ]; then
110 #######################################################################
115 for IP
in $
(echo "$LINE" | \
116 sed "s#\b\($IP_regex\)\b#\n\1\n#g" | \
119 NAME
=`reverse_dns $IP `
121 LINE
=`echo "$LINE" | sed "s#$IP#$NAME#" `