Skip to content

Instantly share code, notes, and snippets.

@akiyoshi83
Created May 19, 2014 15:31
Show Gist options
  • Save akiyoshi83/4ec5aaa152e2d0e9a3e8 to your computer and use it in GitHub Desktop.
Save akiyoshi83/4ec5aaa152e2d0e9a3e8 to your computer and use it in GitHub Desktop.
tiny dns by twisted
zone = [
SOA(
'example.com',
mname = 'ns1.example.com',
serial = 2014051901,
refresh = '1H',
retry = '1H',
expire = '1H',
minimum = '1H'
),
# NS Record
NS('example.com', 'ns1.example.com'),
# A Record
A('example.com', '10.0.0.10'),
# CNAME Record
CNAME('www.example.com', 'example.com')
]
#----------------------------------------
# tiny dns by twisted
#
# please `yum install python-twisted`
#----------------------------------------
user=root
cmd=$1
zone=$2
curdir=`dirname $0`
pidfile="$curdir/tdns.pid"
usage()
{
echo "sh tdns.sh {start|stop|restart|status} [ZONEFIEL]"
echo "if ZONEFILE omitted, use default.zone."
}
start()
{
if [ -f "$pidfile" ]; then
echo "already running..."
return 1
fi
sudo -u $user twistd --pidfile=$pidfile --syslog dns --recursive --cache --pyzone $zone
sleep 1
echo "start tdns pid=`cat $pidfile`"
return 0
}
stop()
{
if [ ! -f "$pidfile" ]; then
echo "not running..."
return 1
fi
echo "stop tdns pid=`cat $pidfile`"
sudo -u $user kill `cat $pidfile`
sleep 1
}
status()
{
if [ -f "$pidfile" ]; then
echo "running..."
return 0
else
echo "not running..."
return 1
fi
}
[ -z "$zone" ] && zone="$curdir/default.zone"
[ ! -f "$zone" ] && echo "$zone is not found." && exit 1
case $cmd in
"start" )
start
;;
"stop" )
stop
;;
"restart" )
stop
start
;;
"status" )
status
;;
* )
usage
;;
esac
@akiyoshi83
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment