Increment all serial numbers at once in a named/bind dns zone file.
Just run the script in the current directory /etc/bind/namedb and all serial numbers will be incremented by one.
#!/usr/bin/ruby
Dir.glob("*.db") do |file_name|
new_zone = []
File.open(file_name) do |file|
while (line = file.gets)
line.chomp!
if line =~ /Serial/
line =~ /(\s*?)(\d+)(\s*?)(;.*)/
serial = $2.to_i
serial += 1
puts "#{file_name} serial line: #{$1} #{$2} #{$3} #{$4} -> #{$1}#{serial}#{$3}#{$4}"
line = "#{$1}#{serial}#{$3}#{$4}"
end
new_zone << line
end
end
new_zone << "\n"
File.open(file_name, "w") do |new_file|
new_file.write(new_zone.join("\n"));
end
end