digcomp.pl revision a3128c1995310262648e575a9ff148d5741fd167
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews#!/usr/bin/perl
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington#
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence# Copyright (C) 2004, 2007, 2012 Internet Systems Consortium, Inc. ("ISC")
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews# Copyright (C) 2000, 2001 Internet Software Consortium.
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews#
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews# Permission to use, copy, modify, and/or distribute this software for any
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence# purpose with or without fee is hereby granted, provided that the above
15a44745412679c30a6d022733925af70a38b715David Lawrence# copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence#
15a44745412679c30a6d022733925af70a38b715David Lawrence# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15a44745412679c30a6d022733925af70a38b715David Lawrence# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15a44745412679c30a6d022733925af70a38b715David Lawrence# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15a44745412679c30a6d022733925af70a38b715David Lawrence# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15a44745412679c30a6d022733925af70a38b715David Lawrence# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews# PERFORMANCE OF THIS SOFTWARE.
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington# $Id: digcomp.pl,v 1.14 2007/06/19 23:47:00 tbox Exp $
cd30495b0ca83a04d4d8f56728e38f93769aee98James Brister
cd30495b0ca83a04d4d8f56728e38f93769aee98James Brister# Compare two files, each with the output from dig, for differences.
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews# Ignore "unimportant" differences, like ordering of NS lines, TTL's,
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews# etc...
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews$file1 = $ARGV[0];
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews$file2 = $ARGV[1];
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff$count = 0;
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff$firstname = "";
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff$status = 0;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence$rcode1 = "none";
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews$rcode2 = "none";
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrewsopen(FILE1, $file1) || die("open: $file1: $!\n");
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrewswhile (<FILE1>) {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews chomp;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews if (/^;.+status:\s+(\S+).+$/) {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $rcode1 = $1;
cd30495b0ca83a04d4d8f56728e38f93769aee98James Brister }
cd30495b0ca83a04d4d8f56728e38f93769aee98James Brister next if (/^;/);
63cef8bde8b92aeb30ccdcf21d4e44c9be9cc6e3Andreas Gustafsson if (/^(\S+)\s+\S+\s+(\S+)\s+(\S+)\s+(.+)$/) {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $name = $1;
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington $class = $2;
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington $type = $3;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $value = $4;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews if ($type eq "SOA") {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $firstname = $name if ($firstname eq "");
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if ($name eq $firstname) {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $name = "$name$count";
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff $count++;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews }
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if ($entry{"$name ; $class.$type ; $value"} ne "") {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $line = $entry{"$name ; $class.$type ; $value"};
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews print("Duplicate entry in $file1:\n> $_\n< $line\n");
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff } else {
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence $entry{"$name ; $class.$type ; $value"} = $_;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews }
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews }
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews}
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrewsclose(FILE1);
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews$printed = 0;
cd30495b0ca83a04d4d8f56728e38f93769aee98James Brister
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrewsopen(FILE2, $file2) || die("open: $file2: $!\n");
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellingtonwhile (<FILE2>) {
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington chomp;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews if (/^;.+status:\s+(\S+).+$/) {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $rcode2 = $1;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff }
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence next if (/^;/);
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews if (/^(\S+)\s+\S+\s+(\S+)\s+(\S+)\s+(.+)$/) {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $name = $1;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $class = $2;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $type = $3;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $value = $4;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews if (($name eq $firstname) && ($type eq "SOA")) {
cd30495b0ca83a04d4d8f56728e38f93769aee98James Brister $count--;
cd30495b0ca83a04d4d8f56728e38f93769aee98James Brister $name = "$name$count";
63cef8bde8b92aeb30ccdcf21d4e44c9be9cc6e3Andreas Gustafsson }
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews if ($entry{"$name ; $class.$type ; $value"} ne "") {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence $entry{"$name ; $class.$type ; $value"} = "";
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence } else {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews print("Only in $file2 (missing from $file1):\n")
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if ($printed == 0);
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews print("> $_\n");
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff $printed++;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $status = 1;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews }
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews }
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffclose(FILE2);
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews$printed = 0;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrenceforeach $key (keys(%entry)) {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews if ($entry{$key} ne "") {
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews print("Only in $file1 (missing from $file2):\n")
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews if ($printed == 0);
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews print("< $entry{$key}\n");
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews $status = 1;
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $printed++;
cd30495b0ca83a04d4d8f56728e38f93769aee98James Brister }
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews}
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrewsif ($rcode1 ne $rcode2) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff print("< status: $rcode1\n");
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews print("> status: $rcode2\n");
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews $status = 1;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrewsexit($status);
db725ebe2b5d913949246b02573df01bd29f5412Mark Andrews