Your Linux Data Center Experts

A vimrc to do DNS serial number updates

" dns.vimrc
autocmd FileType dns map S :python updateDnsSerial()^M

python << EOF
def updateDnsSerial():
   import re, time, vim, string

   maxSearch = 20

   cb = vim.current.buffer
   foundSoa = 0
   for i in xrange(0, min(maxSearch, len(cb))):
      line = cb[i]
      if foundSoa:
         #  look for serial
         rx = re.match(r'^\s*(\d+).*', line)
         if not rx:
            print 'Unable to find Serial'
            return
         serial = rx.group(1)

         #  generate new serial
         now = time.time()
         today = time.strftime('%Y%m%d00', time.localtime(now))
         todayVal = long(today)
         serialVal = long(serial)
         if todayVal <= serialVal: todayVal = serialVal + 1

         #  update serial
         cb[i] = string.replace(line, serial, '%d' % todayVal)

         #  display update string
         print 'Updated serial from "%s" to "%d"' % ( serial, todayVal )
         break
      if re.match(r'^@\s+IN\s+SOA\s+', line): foundSoa = 1
EOF