digcomp.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC")
  4. # Copyright (C) 2000, 2001 Internet Software Consortium.
  5. #
  6. # Permission to use, copy, modify, and/or distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  11. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  12. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  13. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  14. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  15. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. # PERFORMANCE OF THIS SOFTWARE.
  17. # $Id: digcomp.pl,v 1.14 2007/06/19 23:47:00 tbox Exp $
  18. # Compare two files, each with the output from dig, for differences.
  19. # Ignore "unimportant" differences, like ordering of NS lines, TTL's,
  20. # etc...
  21. $file1 = $ARGV[0];
  22. $file2 = $ARGV[1];
  23. $count = 0;
  24. $firstname = "";
  25. $status = 0;
  26. $rcode1 = "none";
  27. $rcode2 = "none";
  28. open(FILE1, $file1) || die("open: $file1: $!\n");
  29. while (<FILE1>) {
  30. chomp;
  31. if (/^;.+status:\s+(\S+).+$/) {
  32. $rcode1 = $1;
  33. }
  34. next if (/^;/);
  35. if (/^(\S+)\s+\S+\s+(\S+)\s+(\S+)\s+(.+)$/) {
  36. $name = $1;
  37. $class = $2;
  38. $type = $3;
  39. $value = $4;
  40. if ($type eq "SOA") {
  41. $firstname = $name if ($firstname eq "");
  42. if ($name eq $firstname) {
  43. $name = "$name$count";
  44. $count++;
  45. }
  46. }
  47. if ($entry{"$name ; $class.$type ; $value"} ne "") {
  48. $line = $entry{"$name ; $class.$type ; $value"};
  49. print("Duplicate entry in $file1:\n> $_\n< $line\n");
  50. } else {
  51. $entry{"$name ; $class.$type ; $value"} = $_;
  52. }
  53. }
  54. }
  55. close(FILE1);
  56. $printed = 0;
  57. open(FILE2, $file2) || die("open: $file2: $!\n");
  58. while (<FILE2>) {
  59. chomp;
  60. if (/^;.+status:\s+(\S+).+$/) {
  61. $rcode2 = $1;
  62. }
  63. next if (/^;/);
  64. if (/^(\S+)\s+\S+\s+(\S+)\s+(\S+)\s+(.+)$/) {
  65. $name = $1;
  66. $class = $2;
  67. $type = $3;
  68. $value = $4;
  69. if (($name eq $firstname) && ($type eq "SOA")) {
  70. $count--;
  71. $name = "$name$count";
  72. }
  73. if ($entry{"$name ; $class.$type ; $value"} ne "") {
  74. $entry{"$name ; $class.$type ; $value"} = "";
  75. } else {
  76. print("Only in $file2 (missing from $file1):\n")
  77. if ($printed == 0);
  78. print("> $_\n");
  79. $printed++;
  80. $status = 1;
  81. }
  82. }
  83. }
  84. close(FILE2);
  85. $printed = 0;
  86. foreach $key (keys(%entry)) {
  87. if ($entry{$key} ne "") {
  88. print("Only in $file1 (missing from $file2):\n")
  89. if ($printed == 0);
  90. print("< $entry{$key}\n");
  91. $status = 1;
  92. $printed++;
  93. }
  94. }
  95. if ($rcode1 ne $rcode2) {
  96. print("< status: $rcode1\n");
  97. print("> status: $rcode2\n");
  98. $status = 1;
  99. }
  100. exit($status);