User Tools

Site Tools


monitor_dns_changes_for_hostname

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

monitor_dns_changes_for_hostname [2011/05/10 14:47]
jt created
monitor_dns_changes_for_hostname [2020/08/10 02:35]
Line 1: Line 1:
-==== Monitor DNS Changes for Hostname ==== 
  
-<code perl> 
-#!/bin/env perl 
-use strict; 
-## Check to see if IP Address or CNAME has changed for a host 
-## If it has chagned, send message using mail function 
- 
-require Net::DNS; 
-require MIME::Lite; 
- 
-my %hostname = ( 
- '​2822.v.fwmrm.net'​ => [ "​ns1.fwmrm.net",​ "​ns2.fwmrm.net"​],​ 
-#​ '​cdn.djtunez.net'​ => [ "​ns1.lithiumfox.com",​ "​ns1.lithiumfox.com"​],​ 
- ); 
- 
- 
-for (keys %hostname){ 
- my $record; ​ 
- my $res = Net::​DNS::​Resolver->​new( 
- nameservers =>​ $hostname{$_},​ 
- recurse =>​ 0, 
- debug =>​ 0, 
- ); 
- 
- my $request = $res->​query("​$_"​);​ 
- 
- if ($request){ 
- foreach my $rr ($request->​answer){ 
- if ($rr-> type eq "​CNAME"​){ 
- $record = $rr->​cname;​ 
- } 
- elsif ($rr->​type eq "​A"​){ 
- $record = $rr->​address;​ 
- } 
- } 
- } 
- checkDifference($record,​$_);​ 
-} 
- 
-sub checkDifference { 
- 
- my ($record, $hostname) = @_; 
- my $statepath = "/​var/​run/​dnscheck";​ 
- my $state = "​$hostname.state";​  
- 
- open STATE, "<​$statepath/​$state"​ or die $!; 
- while (<​STATE>​){ 
- chomp; 
- my $sstate = $_; 
- close STATE; 
- if ($sstate ne $record){ 
- print "​$hostname DNS record has changed from $sstate -> $record\n";​ 
- my $msgbody = "​$hostname DNS record has changed from $sstate -> $record\n";​ 
- mail($msgbody,​ $hostname); 
- open STATE, ">​$statepath/​$state"​ or die $!; 
- print STATE $record; 
- close STATE; 
- } 
- } 
-} 
- 
- 
-sub mail {  
-  
- my ($msgbody, $hostname) = @_; 
- my $subject = "​$hostname DNS Record Changed!";​ 
- my @mailto =  [ '​jthomas@blip.tv',​ '​oogali@blip.tv'​ ]; 
- 
- my $msg = MIME::​Lite->​new( 
- Subject => $subject, 
- To => @mailto, 
- Type =>​ '​text/​html',​ 
- Data =>​ $msgbody, 
- ); 
- $msg->​send();​ 
-} 
- 
-</​code>​ 
monitor_dns_changes_for_hostname.txt ยท Last modified: 2020/08/10 02:35 (external edit)