02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer#!/usr/bin/perl
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer#
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# Copyright (C) 2000, 2001, 2004, 2007, 2012, 2013, 2016 Internet Systems Consortium, Inc. ("ISC")
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# file, You can obtain one at http://mozilla.org/MPL/2.0/.
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater# $Id: digcomp.pl,v 1.14 2007/06/19 23:47:00 tbox Exp $
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer# Compare two files, each with the output from dig, for differences.
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer# Ignore "unimportant" differences, like ordering of NS lines, TTL's,
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer# etc...
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews$lc = 0;
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrewsif ($ARGV[0] eq "--lc") {
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews $lc = 1;
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews shift;
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews}
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer$file1 = $ARGV[0];
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer$file2 = $ARGV[1];
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer$count = 0;
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer$firstname = "";
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer$status = 0;
1cc90f4ab48944d49241a7ee64526bff1ebbddc5Brian Wellington$rcode1 = "none";
1cc90f4ab48944d49241a7ee64526bff1ebbddc5Brian Wellington$rcode2 = "none";
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafssonopen(FILE1, $file1) || die("open: $file1: $!\n");
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyerwhile (<FILE1>) {
8d0f8e8c2e4d8ff5bef14b7e7ae7b73bbc5743eeMark Andrews ~ s/\r\n//g;
45fd95544cd650a8e6a0fc39b656d1109b811ac0Evan Hunt ~ s/\n//g;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if (/^;.+status:\s+(\S+).+$/) {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $rcode1 = $1;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson }
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson next if (/^;/);
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if (/^(\S+)\s+\S+\s+(\S+)\s+(\S+)\s+(.+)$/) {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $name = $1;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $class = $2;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $type = $3;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $value = $4;
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews if ($lc) {
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews $name = lc($name);
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews $value = lc($value);
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews }
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if ($type eq "SOA") {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $firstname = $name if ($firstname eq "");
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if ($name eq $firstname) {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $name = "$name$count";
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $count++;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson }
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson }
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if ($entry{"$name ; $class.$type ; $value"} ne "") {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $line = $entry{"$name ; $class.$type ; $value"};
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson print("Duplicate entry in $file1:\n> $_\n< $line\n");
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson } else {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $entry{"$name ; $class.$type ; $value"} = $_;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson }
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson }
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer}
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafssonclose(FILE1);
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer$printed = 0;
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafssonopen(FILE2, $file2) || die("open: $file2: $!\n");
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyerwhile (<FILE2>) {
8d0f8e8c2e4d8ff5bef14b7e7ae7b73bbc5743eeMark Andrews ~ s/\r\n//g;
45fd95544cd650a8e6a0fc39b656d1109b811ac0Evan Hunt ~ s/\n//g;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if (/^;.+status:\s+(\S+).+$/) {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $rcode2 = $1;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson }
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson next if (/^;/);
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if (/^(\S+)\s+\S+\s+(\S+)\s+(\S+)\s+(.+)$/) {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $name = $1;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $class = $2;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $type = $3;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $value = $4;
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews if ($lc) {
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews $name = lc($name);
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews $value = lc($value);
cb69994ff89179166d5e21ccc7a34223b51676c0Mark Andrews }
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if (($name eq $firstname) && ($type eq "SOA")) {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $count--;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $name = "$name$count";
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson }
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if ($entry{"$name ; $class.$type ; $value"} ne "") {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $entry{"$name ; $class.$type ; $value"} = "";
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson } else {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson print("Only in $file2 (missing from $file1):\n")
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if ($printed == 0);
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson print("> $_\n");
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $printed++;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $status = 1;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson }
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson }
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer}
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafssonclose(FILE2);
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer$printed = 0;
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyerforeach $key (keys(%entry)) {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if ($entry{$key} ne "") {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson print("Only in $file1 (missing from $file2):\n")
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson if ($printed == 0);
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson print("< $entry{$key}\n");
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $status = 1;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson $printed++;
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson }
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer}
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyer
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafssonif ($rcode1 ne $rcode2) {
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson print("< status: $rcode1\n");
ed0a75d4a494e5ebc4ab50d6b9e6fb9dfe3681f4Andreas Gustafsson print("> status: $rcode2\n");
5709b77c8c1dd0b9fac76696a37bafff0a96c591David Lawrence $status = 1;
1cc90f4ab48944d49241a7ee64526bff1ebbddc5Brian Wellington}
1cc90f4ab48944d49241a7ee64526bff1ebbddc5Brian Wellington
02fdafbf53f712ee72ef50c4fa537f97082d8114Michael Sawyerexit($status);