http://www.perlmonks.org?node_id=56631

I needed to make a lot of records tonight so I just whipped this up. It's a little different from the Net::DNS module in that you don't have to know how to properly form a record. It does it for you.
#!/usr/bin/perl print "\n\n########################################\n"; print "# #\n"; print "# This script will create a basic #\n"; print "# DNS Record for use with BIND8++ #\n"; print "# #\n"; print "############################################\n\n\n"; $control = 1; while ($control > 0) { print "What is the domain you would like to create? (e.g., yah +oo.com)\n"; $domain = <STDIN>; chomp($domain); print "You typed: $domain. Is this correct? <y|n>\t"; $temp = <STDIN>; chomp($temp); if ($temp ne "y") { next; } $control = 0; } $control = 1; while ($control > 0) { print "What is the IP address for this domain? (e.g., 127.0.0. +1)\n"; $domIP = <STDIN>; chomp($domIP); print "You typed: $domIP. Is this correct? <y|n>\t"; $temp = <STDIN>; chomp($temp); if ($temp ne "y") { next; } $control = 0; } print "\nWe will begin processing $domain at $domIP.\n"; sleep(1); $control = 1; while ($control > 0) { print "\nPlease give the canonical name of the primary nameser +ver (e.g., ns1.nameserver.com)\n"; $primeNS = <STDIN>; chomp($primeNS); print "You typed: $primeNS. Is this correct? <y|n>\t"; $temp = <STDIN>; chomp($temp); if ($temp ne "y") { next; } $control = 0; } $control = 1; while ($control > 0) { print "\nPlease give the canonical name of the secondary names +erver.\n"; $secNS = <STDIN>; chomp($secNS); print "You typed: $secNS. Is this correct? <y|n>\t"; $temp = <STDIN>; chomp($temp); if ($temp ne "y") { next; } $control = 0; } $control = 1; while ($control > 0) { print "\nWhat is the contact email? (webmaster\@$domain)\n"; $contMail = <STDIN>; chomp($contMail); print "You typed: $contMail. Is this correct? <y|n>\t"; $temp = <STDIN>; chomp($temp); if ($temp ne "y") { next; } $control = 0; $contMail =~ s/[@]/./; } print "Would you like to have a serial number generated for you? <y|n> +\t"; $temp2 = <STDIN>; chomp($temp2); if ($temp2 ne "y") { $control = 1; while ($control > 0) { print "\nPlease enter your serial number.\n"; $serial = <STDIN>; chomp($serial); print "You typed: $serial. Is this correct? <y|n>\t" +; $temp = <STDIN>; chomp($temp); if ($temp ne "y") { next; } $control = 0; } } else { &genSerial; } print "The following values will be used for your dns records. You ma +y adjust them to your taste.\n\n\t10800\tRefresh\n\t3600\tRetry\n\t60 +4800\tExpire\n\t86400\tTTL\n\n"; $fileName = "db.$domain"; &printOut($domain, $domIP, $primeNS, $secNS, $contMail, $serial, $file +Name); print "\nCongratulations! Your DNS record was created in file $fileNa +me for $domain.\n"; sub genSerial { print "Generating serial number."; @breakMeUp = localtime(time); print "."; $year = (1900 + $breakMeUp[5]); print "."; $month = ($breakMeUp[4] + 1); print "."; $day = $breakMeUp[3]; print "."; $serial = "$year"."$month"."$day"."01"; print "$serial\n"; return $serial; } sub printOut($domain, $domIP, $primeNS, $secNS, $contMail, $serial, $f +ileName) { open(DNSFILE, ">$fileName"); select(DNSFILE); print <<til_the_end; $domain. IN SOA $primeNS. $contMail. ($seri +al ; Serial Number 10800; + Refresh 3600; + Retry 604800 +; Expire 86400) +; TTL $domain. IN NS $primeNS. $domain. IN NS $secNS. www.$domain. IN A $domIP ftp.$domain. IN A $domIP $domain. IN A $domIP mail.$domain. IN MX 10 www.$domain. til_the_end close(DNSFILE); }

Replies are listed 'Best First'.
Re: Basic DNS Record Creation
by merlyn (Sage) on Feb 06, 2001 at 11:33 UTC
    Having selected the link of "Comment on...", I feel obliged to comment on this program after a cursory glance as follows.

    I find your introduction of the synthetic variable $control to be very confusing to follow. I had to read that loop three times to figure out when you were getting out to verify that it was doing what the text said it was doing. I think you want this general idiom:

    { print "value for XYZ? "; chomp($xyz = <STDIN>); unless (validate $xyz) { print "$xyz is not a valid XYZ\n"; redo; } print "confirm value $xyz for XYZ? (y/n)"; redo unless <STDIN> =~ /^y/i; }
    In fact, you can bundle this up as a subroutine, and it'd make even more sense. If you ever find yourself typing the same code two or three times, except for some parameters (used in its most generic sense here), it's a good candidate for factoring out. I heard somewhere once "put the regular part as code, and the irregular part as data". And that really makes sense.

    -- Randal L. Schwartz, Perl hacker

      I agree on all points whole heartedly. I am just now getting into OOPing stuff. And I find my biggest problem is that I don't have a good grasp of things like redo and validate and the like. Which makes it difficutl for me to always see _how_ I can make something "uniquely generic" so to speak. Thanks very much for the advice and the new terms. CiceroLove