Auto increment the serial number of a zone file after each edit with vim

Emacs functionality with Vim

Long story short,

I was missing a functionality which i have with Emacs when editing BIND zone files.

By adding the following lines to the zone file header, Emacs will take care of changing the serial number for you.

;; -*- zone -*-
;;
;;  Zone file for example.org
;;
example.org 86400 IN SOA example.org. hostmaster.example.org. (
                                  2006033100   ; Serial YYYYMMDDXX
                                  10800        ; Refresh
                                  3600         ; Retry
                                  3600000      ; Expire
                                  86400 )      ; minimum
     IN      NS      ns1.example.org.
     IN      NS      ns2.example.org.

...

The “Auto Increment” Function

"~/.vim/plugin/soa.vim
"--- .../plugins/named.vim ----
function! UPDSERIAL(date, num)
if (strftime("%Y%m%d") == a:date)
return a:date . a:num+1
endif
return strftime("%Y%m%d") . '01'
endfunction

command Soa :%s/\(2[0-9]\{7}\)\([0-9]\{2}\)\(\s*;\s*Serial\)/\=UPDSERIAL(submatch(1), submatch(2)) . submatch(3)/gc
"---- eof ---------------------

Testing

A example zone file, Open a zone file and type “Soa”

$TTL    86400
@       IN      SOA     localhost. root.localhost. (
                      201408022         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                          86400 )       ; Negative Cache TTL
;
@       IN      NS      localhost.

Please not that you MUST have the serial number in the format of:

YYYYMMDD## ; Serial

The auto-increment function will look for this specific pattern.

The autocmd part

Open you vimrc file and add the following line to it,

"~/.vimrc
autocmd BufWritePre /var/named/chroot/var/named/*.zone,/var/named/chroot/var/named/*.revzone Soa
  •  This command will run a autocmd (which calls to the Soa function) each time before (BufWritePre) we write the file to the disk.
  • Also the command with work only on *.zone and *.revzone files under the path “/var/named/chroot/var/named”

Resources

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *