akiyoshi83
5/19/2014 - 3:31 PM

tiny dns by twisted

tiny dns by twisted

#----------------------------------------
# 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

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') 
]