]> git.ipfire.org Git - people/ms/dnsmasq.git/blame - contrib/reverse-dns/reverse_dns.sh
Fix last commit to not crash if uid changing not configured.
[people/ms/dnsmasq.git] / contrib / reverse-dns / reverse_dns.sh
CommitLineData
47b9ac59
JZ
1#!/bin/bash
2# $Id: reverse_dns.sh 4 2015-02-17 20:14:59Z jo $
3#
4# Usage: reverse_dns.sh IP
5# Uses the dnsmasq query log to lookup the name
6# that was last queried to return the given IP.
7#
8
9IP=$1
10qmIP=`echo $IP | sed 's#\.#\\.#g'`
11LOG=/var/log/dnsmasq.log
12
13IP_regex='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
14
15if ! [[ $IP =~ $IP_regex ]]; then
16 echo -n $IP
17 exit
18fi
19
20NAME=`tac $LOG | \
21 grep " is $IP" | head -1 | \
22 sed "s#.* \([^ ]*\) is $qmIP.*#\1#" `
23
24if [ -z "$NAME" ]; then
25 echo -n $IP
26else
27 echo -n $NAME
28fi
29