: # use perl -w eval ' PERL=/usr/local/bin/perl; [ -x $PERL ] || PERL=/usr/bin/perl; [ -x $PERL ] || PERL=/bin/perl; exec $PERL -w -S $0 ${1+"$@"}' if 0; #!perl # just a comment now # Program : mm_addzone_restore.pl # Author : Michael Simoni # Purpose : adds a standard zone or resore to a men and mice configuraiton # Date : Thu Mar 10 21:19:44 EST 2011 ################################################################################ # CVS Information # $Source: /var/cvs/DNS/men_mice/mm_addzone_restore.pl,v $ # $Author: msimoni $ # $Date: 2012/03/25 06:46:31 $ # $Id: mm_addzone_restore.pl,v 1.3 2012/03/25 06:46:31 msimoni Exp $ # $Revision: 1.3 $ ################################################################################ # @see The GNU Public License (GPL): http://www.opensource.org/licenses/gpl-license.php # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ################################################################################ # Notes ################################################################################ $| = 1; use strict; use English; use Term::ReadKey; use Getopt::Long; use Data::Dumper; ################################################################################ # Globals ################################################################################ #my $hostname = hostname(); # always the current host my $MM_central_server = ""; my $master = ""; # the designated master server at teh command line # my $user = "Administrator"; my $comment = "initial addition of zone"; # adding a zone requires a comment my $dummyfile = "dummyzone.txt"; # need a zone file to import my $debug = 0; my $help = 0; my $showzone = 0; my $template = ""; my $zonelist_file = ""; my @zone_list = (); my @domain_list = (); ################################################################################ # print helpful information ################################################################################ sub usage() { print <; print "\n"; ReadMode 0; chomp $pass; return ($pass); } ################################################################################ # print out the data for a given zone ################################################################################ sub print_a_zone( $ $ ) { my ( $zone_name, $password ) = @_; my @args = ("mmcmd -s $MM_central_server -u $user -p $password \"print $zone_name $master; quit\""); print "Show Current Zone: $zone_name\n"; open( UNAME, "@args |" ); while ( my $line = ) { chomp($line); print "$line\n"; } } ################################################################################ # write an exported zone file with all the desired paramters # Thi sis so you cn import it. the Addzone does not have enough control # over teh zone paramters in the SOA statement # this will always overwrite the dummy zone file #@ IN A 127.0.0.1 #3www IN A 127.0.0.1 ################################################################################ sub write_dummy_zone() { if ( !$template ) { open DUMMYFILE, ">$dummyfile" or die "Could not open $dummyfile: $!\n"; print DUMMYFILE< ) { next if $data_line =~ /^#|^;/ or $data_line =~ /^\s*$/; #skip comments & blank lines next if $data_line =~ /^\s+\;|^\s+;;/; #skip comments next if $data_line =~ /\s+NS\s+/; next if $data_line =~ /\s+SOA\s+/; chomp($data_line); $data_line =~ s/^\s+|\s+$//g; # strip leading and trailing blanks $data_line = lc($data_line); push( @zone_data, $data_line ); } open DUMMYFILE, ">$dummyfile" or die "Could not open $dummyfile: $!\n"; foreach my $line (@zone_data) { print DUMMYFILE "$line\n"; } close(DUMMYFILE); return ($dummyfile); } ################################################################################ ################################################################################ # Add a zone # addZone [-DS] [* | +] [=]* # Adds a zone to a list of servers. The -DS argument specifies that the zone created should be an # Active Directory zone. Slave servers are optional more than one can be specified. An asterisk (*) can # be used instead of explicitly specifying slave servers. If the asterisk is used, a slave zone will be # created on all servers besides the master server. You can set a custom field by the name of # to the value . More than one custom field value can be defined in this manner. ################################################################################ sub add_a_zone( $ $ $ ) { my ( $zonename, $password, $comment ) = @_; print "adding $zonename\n"; my @args = ("mmcmd -s $MM_central_server -u $user -p $password \"addzone $zonename $master *; save - \"$comment\" ; quit\""); open( UNAME, "@args |" ); while ( my $line = ) { chomp($line); print "$line:\n"; } } ################################################################################ # print zone history ################################################################################ sub print_zone_history( $ $ ) { my ( $zonename, $password ) = @_; print "Zone History:\n"; my @args = ("mmcmd -s $MM_central_server -u $user -p $password \"zonehistory zone=$zonename; quit\""); open( UNAME, "@args |" ); while ( my $line = ) { chomp($line); print "$line:\n"; } } ################################################################################ # import a zone # import [-DS] [-aging] [ * | + ] # Imports a zone from an external file. The -DS argument specifies that the zone created should be an # Active Directory zone. The -aging argument specifies that if this zone is created on a Microsoft # Windows server and is a zone file that contains aging values for records, those values will # be honored when the zone is created. ################################################################################ sub import_zone( $ $ $ $ ) { my ( $zonename, $password, $comment, $zonefile ) = @_; print "importing: $zonename\n"; print " : $comment\n"; my @args = ("mmcmd -s $MM_central_server -u $user -p $password \"import $zonename $master * $zonefile; save - ; quit\""); open( UNAME, "@args |" ); while ( my $line = ) { chomp($line); print "$line:\n"; } } ################################################################################ # read in the file ################################################################################ sub get_zones( $ ) { my ($zonesfile) = @_; my $linecount = 0; open( ZONES, "<", $zonesfile ) or die "$!i : can't open $zonesfile"; while ( my $data_line = ) { $linecount++; #print STDERR "$linecount\r"; next if $data_line =~ /^#|^;/ or $data_line =~ /^\s*$/; #skip comments & blank lines next if $data_line =~ /^\s+\;|^\s+;;/; #skip comments chomp($data_line); $data_line =~ s/^\s+|\s+$//g; # strip leading and trailing blanks $data_line = lc($data_line); push( @zone_list, $data_line ); } } ################################################################################ # Main ################################################################################ #$binddn = shift @ARGV; my $length_argv = @ARGV; # check on correct number of command line paramters my $pw; my $zone_name; my $zone_data_directory; my $restore_zones = 0; my @new_nameservers = (); ################################################################################ ##################### # parse command line ##################### if ( $length_argv < 1 ) { usage; exit; } GetOptions( "central=s" => \$MM_central_server, "master=s" => \$master, "user=s" => \$user, "password=s" => \$pw, "template=s" => \$template, "filelist=s" => \$zonelist_file, "zone=s" => \$zone_name, "komment=s" => \$comment, "showzone" => \$showzone, "grab_data=s" => \$zone_data_directory, "restore_zones" => \$restore_zones, "new_nameservers=s" => \@new_nameservers, "debug" => \$debug, "help" => \$help, ); if ($help) { usage(); exit; } if ( !$pw ) { #get the password $pw = get_password("Please enter your password: "); } $master =~ s/\.$//; # get rid of trailing dot for easier comparisons if ($zonelist_file) { get_zones($zonelist_file); } if ($showzone) { if ($zonelist_file) { foreach my $zone_name (@zone_list) { print_a_zone( $zone_name, $pw ); } } else { if ( !$zone_name ) { print "ERROR: zone_name is required. Exiting.\n"; exit; } print_a_zone( $zone_name, $pw ); } exit; } # add the default zone if ($template) { $dummyfile = $template; } else { write_dummy_zone(); } if ($restore_zones) { foreach my $zone_name (@zone_list) { my $zonefile = "$zone_data_directory/$zone_name" . "-hosts"; if (@new_nameservers) { $zonefile = write_newzone( $zonefile, \@new_nameservers ); } import_zone( $zone_name, $pw, $comment, $zonefile ); print_line("="); print_a_zone( $zone_name, $pw ); print_line("="); print_zone_history( $zone_name, $pw ); } exit; } if ($zone_name) { if ( !$comment ) { print "Error: adding a zone requires a comment. Exiting.\n"; exit; } import_zone( $zone_name, $pw, $comment, $dummyfile ); print_line("="); print_a_zone( $zone_name, $pw ); print_line("="); print_zone_history( $zone_name, $pw ); exit; } else { foreach my $zone_name (@zone_list) { import_zone( $zone_name, $pw, $comment, $dummyfile ); print_line("="); print_a_zone( $zone_name, $pw ); print_line("="); print_zone_history( $zone_name, $pw ); } } __END__