LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-25-2011, 11:05 AM   #1
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
script for managing SOA serial in zone files


Here's a little tool that does this:
  • without arguments, updates the SOA serial in a zonefile to the current date. If the date was already updated, just updates the revision number (incrementing up to 99, and then again 01). Uses RFC 1912* recommended format.
  • with $1 == <two digit number>, auto updates (if necessary) just the date part and uses your provided revision number.
  • with $1 == <eight digit number>, uses that as a date (no validation of any kind), and just auto update the revision number
  • with $1 == <full serial>, will just replace whatever the serial is with the provided serial, without any validation

* YYYYMMDDRR (4-digit year, 2-digit month, 2-digit day of month, 2-digit revision number)

This script + keeping SOA/NS/MX/CNAME RRs in a common file $included from other files with $ORIGIN and A/PTR/TXT RRs, made everything way easier to manage, enabling me to script some zone switchers, automatic failover/redirection of DNS on WAN changes, etc, etc. I think this stuff might be cool to integrate with something like this script and make nice CLI toolset for bind. Looking forward to implement it.

Please report errors, etc. Hope you find it useful.

code in pastebin

Code:
#!/bin/bash

# globals-------------------------------------------------------------------
script_name="${0##*/}"
script_dir=$(readlink -f "${0%/*}")
script_version=1

# this can be modded to be supplied from command line
named_dir='/var/named'
serialfile="ufis-common.txt"

# main----------------------------------------------------------------------
[[ "$1" ]] && {
    (( ${#1} == 10 )) && newserial=$1
    (( ${#1} == 8 )) && date=$1
    (( ${#1} == 2 )) && revision=$1
    ! [[ "$1" =~ ^[0-9]+$ ]] || [[ -z "$newserial$date$revision" ]] && echo "$script_name: ERROR: can't do anything with $1" && exit 1
}   

curserial="$( sed -ne 's/^.*\([0-9]\{10\}\).*/\1/g; /^[0-9]*$/p' "$named_dir/$serialfile" )"

# if we haven't been passed a serial lets build one
[[ -z "$newserial" ]] && {
# if we don't have a date get current
    [[ -z "$date" ]] && date="$(date +%Y%m%d)"
# if we don't have revision number get next from the current, if current=99 or date<>today it'll be "01"
    [[ -z "$contador" ]] && {
        revision="01"
        (( ${curserial:0:8} == $date )) && {
            revision=$( printf "%02d" $(( 10#${curserial:8:2}+1 )) )
            (( 10#$revision == 100 )) && echo "$script_name: warning: revision counter looped to 01" && revision="01"
        }
    }
    newserial=$date$revision
}

# replace the serial
# some sanity checks here won't hurt
# this is a minimal one
(( ${#newserial} == 10 )) && {
    echo "$script_name: new serial number $newserial"
    sed -i "s/\(^.*\)$curserial\(.*$\)/\1$newserial\2/" "$named_dir/$serialfile"
}

Last edited by Juako; 02-26-2011 at 09:59 PM. Reason: spelling error in the code
 
Old 02-26-2011, 04:26 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
The forum here really isn't the best place for things like this, they tend to sink without trace. But it's a useful script potentially. What use did you have for this in your practises? Personally I'd recommend just using nsupdate to manipulate the zone files, and then it will deal with incrementing the serial on your behalf. I don't really pay attention to what the serial is - date encodings etc... just as long as you are consistent in how you update the file, all slaves will be in line etc, and there's nothing to care about.

BTW, there's a syntax bug on line 17. Double negative.

Last edited by acid_kewpie; 02-26-2011 at 04:27 AM.
 
Old 02-26-2011, 05:17 AM   #3
Juako
Member
 
Registered: Mar 2010
Posts: 202

Original Poster
Rep: Reputation: 84
Well i guess as long it's indexed in some some search engine "someone" looking for this will get to find it :P, i'm a novice in bind and hadn't heard of nsupdate, i'm checking its man now and looks cool, will try it out. That said i got used to tune zone files by hand, and have some includes (as i mentioned in the OP) which really makes all this very easy.

I just have two direct zones and some reverse zones, don't know how my scheme would scale, ymmv i guess. My main use for the script is in other scripts that poll my router to check what WAN routes are active and if changes are to be made they bring up the relevant zone files, update the serial and restart bind.

Couldn't find the error you mention in line 17, it seems it's just a closing brace. Could you expand on that?

Code:
$ sed -n '17p' soa-update-serial 
}

Last edited by Juako; 02-26-2011 at 05:39 AM.
 
Old 02-26-2011, 05:40 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
You said 'can't do nothing'...double negative.
 
Old 02-26-2011, 05:45 AM   #5
Juako
Member
 
Registered: Mar 2010
Posts: 202

Original Poster
Rep: Reputation: 84
Ahh hahaha i see, so it's a spelling error, not a bug. Thanks for noticing, we do in spanish say it as a double negative, "no puedo hacer nada". Seems indeed not a valid construction in english.

Last edited by Juako; 02-26-2011 at 10:06 PM.
 
  


Reply

Tags
bind, script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Purpose of SOA serial number? centosfan Linux - Server 2 02-19-2011 09:19 PM
Problem creating second zone: "Copying <0> files to the zone." AlucardZero Solaris / OpenSolaris 0 07-20-2010 03:12 PM
Run script in EST time zone but server is in GMT zone jeesun Linux - Networking 1 04-04-2010 01:37 PM
LXer: Recent SOA announcements and SOA developer resources LXer Syndicated Linux News 0 10-22-2006 07:03 PM
BIND - SOA record not at top of zone granny Linux - Networking 3 10-29-2004 09:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration