Listing01_DnsCmd (Perl)

This code can be found in Chapter 8 of Managing Enterprise Active Directory Services

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# From the book "Managing Enterprise Active Directory Services"
# ISBN: 0-672-32125-4

package DnsCmd;

use strict;
use vars qw( $AUTOLOAD );

# May need to fully qualify the path to DnsCmd
my $DnsCmd = 'dnscmd.exe';

# Used to instantiate a new DnsCmd object
sub new {
   my $class = shift;
   my $server = shift;
   my $self = { _server => $server };
   bless $self,$class;
}

# Stores and retrieves an error generated by DnsCmd
sub error {
my $self = shift;
   my $err = shift;
   if (defined $err) {
      $self->{_error} = $err;
   }
   $self->{_error};
}

# Stores and retrieves output generated by DnsCmd
sub output {
my $self = shift;
   my $out = shift;
   if (defined $out) {
      $self->{_output} = $out;
   }
   $self->{_output};
}

# Clears the error and output variables
sub reset {
my $self = shift;
   undef $self->{_error};
   undef $self->{_output};
}

sub AUTOLOAD {
   my $self = shift;
   my $type = ref($self)
      or die "$self is not an object";

   return  if $AUTOLOAD =~ /::DESTROY$/;

   $self->reset();

   my $name = $AUTOLOAD;
   $name =~ s/.*://;   # strip fully-qualified portion

   # Issue a DnsCmd command based on parameters passed
   my $out = `$DnsCmd $self->{_server} /$name @_ 2>&1`;

   # Check for errors in the output
   $self->error(1) if $out =~ /Command failed/s || $out =~ /USAGE:/s;

   # The regex's below remove unwanted data from the output
   # Additional regex's can be added as needed
   $out =~ s/Query result:\s+//s;
   $out =~ s/Zone query result:\s+//s;
   $out =~ s/Command completed successfully.\s+//s;
   $out =~ s/\n\s+Status =.+?\n\n/\n/s;
   $self->output("$out");
}

1;

This code has been viewed 786 times.

New from the creators of TechTasks.com: StatSheet.com