/services/rdns.cgi
#!/usr/bin/env bash
# rdns - Reverse DNS CGI
# Author: Sean B. Palmer, inamidst.com
case $QUERY_STRING in
ip=*) IP=${QUERY_STRING#ip=};;
*) IP=${PATH_INFO#/};;
esac
function header() {
echo Content-Type: $1
echo
}
function doHomepage() {
header text/html
cat <<EOF
<h1>Reverse DNS</h1>
<form method="GET" action="rdns">
<div>
<input type="text" name="ip" length="35" />
<input type="submit" />
</div>
</form>
<p>This script enables you to look up the DNS for any given
IP. See also: <a href="/inside/services/rdns">source</a>.</p>
<address><a href="http://inamidst.com/sbp/"
>Sean B. Palmer</a></address>
EOF
}
function doRedirect() {
echo Status: 301
echo Content-Type: text/html
echo Location: "http://inamidst.com/services/rdns/$IP"
echo
echo '<a href="rdns/'"$IP"'">redirect</a>'
}
function doReverseDNS() {
header text/plain
dig -x "$IP" | \
grep -iA 1 'ANSWER SECTION' | tail -n 1 | \
sed 's/ / /g' | cut -f 5 | sed 's/\.$//'
}
if [ "$QUERY_STRING" ]
then doRedirect
elif [ "$IP" ]
then doReverseDNS
else doHomepage
fi