digcomp.pl revision 1.1.1.1
1#!/usr/bin/perl
2#
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# This Source Code Form is subject to the terms of the Mozilla Public
6# License, v. 2.0. If a copy of the MPL was not distributed with this
7# file, You can obtain one at http://mozilla.org/MPL/2.0/.
8#
9# See the COPYRIGHT file distributed with this work for additional
10# information regarding copyright ownership.
11
12# Compare two files, each with the output from dig, for differences.
13# Ignore "unimportant" differences, like ordering of NS lines, TTL's,
14# etc...
15
16$lc = 0;
17if ($ARGV[0] eq "--lc") {
18	$lc = 1;
19	shift;
20}
21$file1 = $ARGV[0];
22$file2 = $ARGV[1];
23
24$count = 0;
25$firstname = "";
26$status = 0;
27$rcode1 = "none";
28$rcode2 = "none";
29
30open(FILE1, $file1) || die("open: $file1: $!\n");
31while (<FILE1>) {
32	~ s/\r\n//g;
33	~ s/\n//g;
34	if (/^;.+status:\s+(\S+).+$/) {
35		$rcode1 = $1;
36	}
37	next if (/^;/);
38	if (/^(\S+)\s+\S+\s+(\S+)\s+(\S+)\s+(.+)$/) {
39		$name = $1;
40		$class = $2;
41		$type = $3;
42		$value = $4;
43		if ($lc) {
44			$name = lc($name);
45			$value = lc($value);
46		}
47		if ($type eq "SOA") {
48			$firstname = $name if ($firstname eq "");
49			if ($name eq $firstname) {
50				$name = "$name$count";
51				$count++;
52			}
53		}
54		if ($entry{"$name ; $class.$type ; $value"} ne "") {
55			$line = $entry{"$name ; $class.$type ; $value"};
56			print("Duplicate entry in $file1:\n> $_\n< $line\n");
57		} else {
58			$entry{"$name ; $class.$type ; $value"} = $_;
59		}
60	}
61}
62close(FILE1);
63
64$printed = 0;
65
66open(FILE2, $file2) || die("open: $file2: $!\n");
67while (<FILE2>) {
68	~ s/\r\n//g;
69	~ s/\n//g;
70	if (/^;.+status:\s+(\S+).+$/) {
71		$rcode2 = $1;
72	}
73	next if (/^;/);
74	if (/^(\S+)\s+\S+\s+(\S+)\s+(\S+)\s+(.+)$/) {
75		$name = $1;
76		$class = $2;
77		$type = $3;
78		$value = $4;
79		if ($lc) {
80			$name = lc($name);
81			$value = lc($value);
82		}
83		if (($name eq $firstname) && ($type eq "SOA")) {
84			$count--;
85			$name = "$name$count";
86		}
87		if ($entry{"$name ; $class.$type ; $value"} ne "") {
88			$entry{"$name ; $class.$type ; $value"} = "";
89		} else {
90			print("Only in $file2 (missing from $file1):\n")
91			    if ($printed == 0);
92			print("> $_\n");
93			$printed++;
94			$status = 1;
95		}
96	}
97}
98close(FILE2);
99
100$printed = 0;
101
102foreach $key (keys(%entry)) {
103	if ($entry{$key} ne "") {
104		print("Only in $file1 (missing from $file2):\n")
105		    if ($printed == 0);
106		print("< $entry{$key}\n");
107		$status = 1;
108		$printed++;
109	}
110}
111
112if ($rcode1 ne $rcode2) {
113	print("< status: $rcode1\n");
114	print("> status: $rcode2\n");
115	$status = 1;
116}
117
118exit($status);
119