DnsImport

From CobaltFAQs

Jump to: navigation, search

This script originally came from ftp://ftp-eng.cobalt.com/pub/users/jeffb/cmu/beta/dnsImport

It is used to import DNS records from a RaQ 4 to a RaQ 550. There are also reports of it working with a BlueQuartz system.


#!/usr/bin/perl
# $Id: dnsImport,v 1.5 2002/10/23 19:57:03 jeffb Exp $
# Copyright 2002 Sun Microsystems Inc.  All rights reserved.
# I have only tested this with RaQ4 DNS records file
# 
# for best viewing in vi use the following 
# set tabstop=4
# set shiftwidth=4

use strict;

use lib "/usr/cmu/perl";

use Global;
require cmuCCE;

my $build = Global::getBuild();
if($build ne "RaQ550") {
	print "This can only be run on a RaQ550\n";
	exit 1;
}

my $records_file;
if(@ARGV) {
	$records_file = $ARGV[0];
	if(! -f $records_file) {
		print "Cannot find records file: $records_file\n";
		print "usage: $0: [records file]\n";
		exit 1;
	}
} else {
	print "usage: $0: [records file]\n";
	exit 1;
}

use vars qw($cce);
my $cce = new cmuCCE;
$cce->connectuds();

my @records = read_records($records_file);
foreach my $rec (@records) {
	chomp $rec;
	my @line = split(/ /, $rec);
	if($line[0] eq 'a') {
		add_A(@line);
	} elsif($line[0] eq 'mx') {
		add_MX(@line);
	} elsif($line[0] eq 'cname') {
		add_CNAME(@line);
	} elsif($line[0] eq 'ptr') {
		add_PTR(@line);
	} elsif($line[0] eq 'soa') {
		mod_SOA(@line);
	} elsif($line[0] eq 'axs') {
		add_AXS(@line);
	} elsif($line[0] eq 'forwarder0') {
		add_FORWARD(@line);
	} elsif($line[0] eq 'forwarder1') {
		add_FORWARD(@line);
	} elsif($line[0] eq 'secondary') {
		add_SECONDARY(@line);
	} else {
		print "Can't import record type: ", $line[0], "\t";
		print "Skipping....\n";
	}	
}
	
commit_changes();
$cce->bye("bye-bye miss american pie");
exit 0;

sub add_A
{
	my @record = @_;
	my $hash = {};
	
	$hash->{type} = 'A';

	if($record[1] eq '-') { $hash->{hostname} = ''; } 
	else { $hash->{hostname} = $record[1]; }

	$hash->{domainname} = $record[2];
	$hash->{ipaddr} = $record[3];

	my ($ok, $bad, @info) = $cce->create('DnsRecord', $hash);
	if($ok == 0) {
		$cce->printReturn($ok, $bad, @info);
	} else { 
		print "A record ", $hash->{hostname}, " ", $hash->{domainname},
			" => ", $hash->{ipaddr}, " has been created sucessfully\n";
	}
}

sub add_MX
{
	my @record = @_;
	my $hash = {};

	$hash->{type} = 'MX';

	if($record[1] eq '-') { $hash->{hostname} = ''; } 
	else { $hash->{hostname} = $record[1]; }

	$hash->{domainname} = $record[2];
	$hash->{mail_server_priority} = lc($record[3]);
	$hash->{mail_server_name} = $record[4];

	my ($ok, $bad, @info) = $cce->create('DnsRecord', $hash);
	if($ok == 0) {
		$cce->printReturn($ok, $bad, @info);
	} else { 
		print "MX record ", $hash->{hostname}, " ", $hash->{domainname},
			" => ", $hash->{mail_server_name}, " with priority ", 
			$hash->{mail_server_priority}, " has been created sucessfully\n";
	}
}

sub add_CNAME
{
	my @record = @_;
	my $hash = {};

	$hash->{type} = 'CNAME';

	if($record[1] eq '-') { $hash->{hostname} = ''; } 
	else { $hash->{hostname} = $record[1]; }

	$hash->{domainname} = $record[2];

	if($record[3] eq '-') { $hash->{alias_hostname} = ''; } 
	else { $hash->{alias_hostname} = $record[3]; }
	$hash->{alias_domainname} = $record[4];

	my ($ok, $bad, @info) = $cce->create('DnsRecord', $hash);
	if($ok == 0) {
		$cce->printReturn($ok, $bad, @info);
	} else { 
		print "CNAME record ", $hash->{hostname}, " ", $hash->{domainname},
			" => ", $hash->{alias_hostname}, " ", $hash->{alias_domainname},
			" has been created sucessfully\n";
	}
}

sub add_PTR
{
	my @record = @_;
	my $hash = {};

	$hash->{type} = 'PTR';

	if($record[1] eq '-') { $hash->{hostname} = ''; } 
	else { $hash->{hostname} = $record[1]; }

	$hash->{domainname} = $record[2];
	$hash->{ipaddr} = $record[3];

	$hash->{netmask} = get_netmask();

	my ($ok, $bad, @info) = $cce->create('DnsRecord', $hash);
	if($ok == 0) {
		$cce->printReturn($ok, $bad, @info);
	} else { 
		print "PTR record ", $hash->{ipaddr}, " => ", $hash->{hostname}, 
			" ", $hash->{domainname}, " has been created sucessfully\n";
	}
}

sub mod_SOA
{
	my @record = @_;
	my $hash = {};

	my $base = $record[2];
	my $oid;
	if($base eq '-') {
		print "I don't know what do this record: ", @record, "\n";
		return;
	} elsif ($base =~ /(\d+\.\d+\.\d+\.\d+)(\/\d+)/) {
		$base = $1;
		($oid) = $cce->find('DnsSOA', { ipaddr => $base });
	} else {
		($oid) = $cce->find('DnsSOA', { domainname => $base });
	}
	if(!$oid) {
		print "Cannot find DNS SOA record for $base\n";
		return;
	}

	my @soa = split(/:/, $record[3]);
	$hash->{primary_dns} = $soa[0];
	if(defined $soa[1]) {
		$hash->{secondary_dns} = $cce->array_to_scalar(split(/\+/,$soa[1]));
	}
	$hash->{domain_admin} = $soa[2];
	$hash->{refresh} = $soa[3];
	$hash->{retry} = $soa[4];
	$hash->{expire} = $soa[5];
	$hash->{ttl} = $soa[6];

	my ($ok, $bad, @info) = $cce->set($oid, '', $hash);
	if($ok == 0) {
		$cce->printReturn($ok, $bad, @info);
	} else { 
		print "SOA record ", $base, " has been modified sucessfully\n";
	}
}

sub add_AXS
{
	my @record = @_;
	my $hash = {};

	$hash->{zone_xfer_ipaddr} = $cce->array_to_scalar(split(/::/, $record[3]));
	my ($oid) = $cce->find('System');
	if(!$oid) {
		print "Could not find System OID, you got some serious problems\n";
		return;
	}
	my ($ok, $bad, @info) = $cce->set($oid, 'DNS', $hash);
	if($ok == 0) {
		$cce->printReturn($ok, $bad, @info);
	} else { 
		print "AXS record has been modified sucessfully\n";
	}
}

sub add_FORWARD
{
	my @record = @_;
	my $hash = {};

	my ($oid) = $cce->find('System');
	if(!$oid) {
		print "Could not find System OID, you got some serious problems\n";
		return;
	}
	my ($ok, $obj) = $cce->get($oid, 'DNS');	
	if($ok == 0) {
 		print "Cound not get DNS namespace in system object\n";
		return;
	}
	if($obj->{forwarders}) {
		$hash->{forwarders} = $obj->{forwarders}.$record[3].'&';	
	} else {
		$hash->{forwarders} = '&'.$record[3].'&';
	}
	my ($bad, @info);
	($ok, $bad, @info) = $cce->set($oid, 'DNS', $hash);
	if($ok == 0) {
		$cce->printReturn($ok, $bad, @info);
	} else { 
		print "Forwarders record has been modified sucessfully\n";
	}
}

sub add_SECONDARY
{
	my @record = @_;
	my $hash = {};

	if($record[2] =~ /\d+\.\d+\.\d+\.\d+/) {
		$hash->{ipaddr} = $record[2];
		$hash->{netmask} = get_netmask();
	} else {
		$hash->{domain} = $record[2];
	}
	$hash->{masters} = $record[3];
 
	my ($ok, $bad, @info) = $cce->create('DnsSlaveZone', $hash);
	if($ok == 0) {
		$cce->printReturn($ok, $bad, @info);
	} else { 
		print "Secondary record ", $hash->{domain}, $hash->{ipaddr}, 
			" => ", $hash->{masters}, " has been created sucessfully\n";
	}


}

sub read_records
{   
    my $records = shift;
    if(!open(FH, "<$records")) {
        print "Could open records file: $records\n";
		exit 1;
    }
    my @data = <FH>;
    close(FH);
    return @data;
}

sub get_netmask 
{
	my $netmask = `ifconfig eth0 | grep "inet addr" | cut -f 4 -d":" | cut -f 1 -d" "`;
	chomp($netmask);
	return $netmask;
}

sub commit_changes
{

	my $time = time();
	my ($oid) = $cce->find('System');
	if(!$oid) {
		print "Could not find System OID, cannot commit changes\n";
		return;
	}
	$cce->set($oid, 'DNS', { commit => $time });
	$cce->commit();
}
Personal tools