bind to nictool script

Started by shaun, April 14, 2005, 04:50:52 PM

Previous topic - Next topic

shaun

Here's a script that will do a basic export of your bind zones into nictool using the nictool API.

Few things to remember:
1. If you used the default install docs for the nictoolserver then nictools api only listens on localhost, you will need to modify your httpd.conf and either make that virtual host listen on all interfaces by changing 127.0.0.1:8082 to *:8082 or add another virtual host for the ip you want.
2. You will need to install the NicToolClient on the machine your doing the exporting from, you dont need to copy NicToolClient to /usr/local/blah... just do the standard perl Makefile.PL && make && make install
3. This script uses DNS::ZoneParse, if that perl module is not installed then you'll need to install it.
4. This script adds the zones to the sub group that this user belongs to.
5. This script does not do any kind of checking on teh nictool server so if a zone exits it will error likly.
6. script only parses zones that end in .db, that can be changed easily.

This script is really just a choped up version of one of my other scripts i use.  I didnt do much testing with this script and i'm sure it could be improved.  I decided to post this because i saw a few people ask for it and nobody wanted to post theirs.

If you have a qustion, just ask i'll attempt to awnser them.





----------------------------[SOURCE]---------------------
#!/usr/bin/perl

use strict;
use NicToolServerAPI;
use DNS::ZoneParse;
use Data::Dumper;

my %CONF;
$CONF{'named_zones_path'} = '/var/named/';
$CONF{'nictool_user'} = 'importuser';
$CONF{'nictool_pass'} = 'importpass';
$CONF{'nictool_host'} = '10.0.0.1';
$CONF{'nictool_port'} = '8082';


if(DNS::ZoneParse->VERSION < '0.95') {
       print STDERR "Warning DNS::ZoneParse Version is ".DNS::ZoneParse->VERSION." This version is known to have problems reading zones correctly, please upgrade ZoneParse to version => 0.95\n";
       exit(1);
}

my $nt = new NicToolServerAPI();
$NicToolServerAPI::data_protocol="soap";
$NicToolServerAPI::use_https_authentication = '0';
$NicToolServerAPI::server_host = $CONF{'nictool_host'};
$NicToolServerAPI::server_port = $CONF{'nictool_port'};

my $nt_user = $nt->send_request(
       action  => 'login',
       username        => $CONF{'nictool_user'},
       password        => $CONF{'nictool_pass'}
       );

if(!$nt_user->{'nt_user_session'}) {
       print STDERR $nt_user->{'error_code'}.": ".$nt_user->{'error_msg'}."\n";
       exit(1);
}

for(my $i=0;$i<9;$i++){
       if($nt_user->{'usable_ns'.$i}) {
               $nt_user->{'nameserver_list'} .= $nt_user->{'usable_ns'.$i}.",";
       }
}
my $nt_zones = $nt->send_request(
       action => "get_group_zones",
       nt_user_session => $nt_user->{'nt_user_session'},
       nt_group_id => $nt_user->{'nt_group_id'},
       limit => "9999"
       );

if($nt_zones->{'error_msg'} ne "OK") {
       print STDERR $nt_zones->{'error_code'}.": ".$nt_zones->{'error_msg'}."\n";
       exit(1);
}

opendir ZONES, $CONF{'named_zones_path'};
my @ZONES = readdir(ZONES);
closedir ZONES;

foreach(@ZONES) {
       next unless /\.db$/;
       sendzone($_);
}

sub sendzone() {
       my($zone) = @_;
       print "Importing Zone: ".$zone."\n";

       my $zp = DNS::ZoneParse->new($CONF{'named_zones_path'}."/".$zone);
       $zone =~ s/\.db//;
       my $zone_soa    = $zp->soa();

       my $cz = $nt->send_request(
                       action => 'new_zone',
                       nt_user_session => $nt_user->{'nt_user_session'},
                       nt_zone_id => '',
                       nt_group_id => $nt_user->{'nt_group_id'},,
                       zone => $zone,
                       ttl => $zone_soa->{'ttl'},
                       nameservers => $nt_user->{'nameserver_list'},
                       mailaddr => $zone_soa->{'email'},
                       refresh => $zone_soa->{'refresh'},
                       retry => $zone_soa->{'retry'},
                       expire => $zone_soa->{'expire'},
                       minimum => $zone_soa->{'minimumTTL'},
                       );
               unless($cz->{'nt_zone_id'}) {
                       warn "nt_zone_id was not returned, chances are creating the new zone failed!\n";
                       warn Dumper($cz);
                       return;
               }

       my @recordtypes = ('a', 'cname', 'ns', 'mx', 'ptr');
       foreach my $recordtype (@recordtypes) {
               foreach my $record (@{$zp->$recordtype()}) {
                       my $cr = $nt->send_request(
                               action => 'new_zone_record',
                               nt_user_session => $nt_user->{'nt_user_session'},
                               nt_zone_record_id => '',
                               nt_zone_id => $cz->{'nt_zone_id'},
                               name => $record->{'name'},
                               ttl => $record->{'ttl'},
                               type => $recordtype,
                               address => $record->{'host'},
                               weight => $record->{'priority'}
                               );
               }
       }
}

shaun

also i just realized this script does not export txt records.  I'm sure that could be added in easily but for now i dont have a need for them.  Maybe in the future when spf is used on more than 10 servers  Laughing