[techtalk] script help -- thanks!

Samuel Lavenz johngalt at io.com
Thu Sep 28 13:13:37 EST 2000


Alissa-

I don't know if you know perl at all, however, it makes doing your job a
lot easier...and prettier. ::smile::

The attatched script does precisely what you wanted.  There's one caveat,
though.  It doesn't just look for .com .net .edu .au etc...it just shaves
off whatever comes after the last . and the end of the string.

The code is heavily commented, and I mention the above caveat there.

The script, of course, doesn't address file locking and what not.  But I
don't think that file locking is a concern, as I can't feature this being
run by more than one person at once.

Cheers!
Sam


On Thu, 28 Sep 2000, alissa bader wrote:

> I would like to thank everyone who helped me with that
> DNS script the other day.  This is how I wound up
> writing it:
> 
> #!/bin/sh  
> # to fix secondary dns entries 
> 
> for DOMAIN in `cat /tmp/ns1stuff`
> do 
>        for DIR in `echo $DOMAIN |cut -c1`   
>         do
>           echo "zone" '"'"$DOMAIN"'"' "{" >>
> /etc/named.conf
>           echo "        type slave;" >>
> /etc/named.conf
>           echo '        file "'$DIR'/db.'$DOMAIN'";'
> >> /etc/named.conf
>           echo "        masters {" >> /etc/named.conf
>           echo "                204.177.32.2;" >>
> /etc/named.conf
>           echo "        };" >> /etc/named.conf  
>           echo "};" >> /etc/named.conf
>           echo "" >> /etc/named.conf
>         done
> 
> (excuse please the ratty formatting here, damned
> web-based email crap)
> 
> I opted not to drop the toplevel domain off of every
> domain name, as we might run into duplicates here.  
> 
> cut, as it turns out, really is my friend.  :>  
> 
> One thing I have noticed:  you have to be really
> careful and specific when you define each variable and
> for loop.  I have written a number of little scripts
> that only ask for one variable.  And I was agonizing
> for hours over how to define a second one.  Finally it
> hit me to just add a for loop for it.  And wouldn't
> you know it, it worked!
> 
> sed sounds like it's pretty much worth knowing,
> although I didn't have to use it in this script.  time
> to poke over the man pages!
>  
> again, thank you all so much, never could have gotten
> it working without you.  :>
> 
> --alissa
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Photos - 35mm Quality Prints, Now Get 15 Free!
> http://photos.yahoo.com/
> 
> _______________________________________________
> techtalk mailing list
> techtalk at linuxchix.org
> http://www.linux.org.uk/mailman/listinfo/techtalk
> 

-- 
-------------------------------------------------------------------------------
	               Sam Lavenz -- KC0BUH
		          johngalt at io.com

     "I recognize no obligations toward men except one: to respect their
              freedom and to take no part in a slave society"
                   -- Ayn Rand -- _The Fountainhead_ --

-------------- next part --------------
#!/usr/bin/perl

# This saves us some headaches...if there aren't two arguments, we 
# print a usage message and exit.
unless ( scalar( @ARGV ) == 2 )
{
  print "Usage: $0 [infile] [outfile]\n";
  exit;
}

# We read from the file in the first argument
# We append to the file in the second
open INFILE, "<$ARGV[0]" or die "Could not open input file: $!";
open OUTFILE, ">>$ARGV[1]" or die "Could not open output file: $!";

# As long as we're getting input from our INFILE
while( <INFILE> )
{
  next if /^$/;      # Go on to the next line if this line is blank
  chomp;             

  # This is where all the magic is done.
  # We just run a pattern match on the string we've read in.
  # The first set of parens captures anything up to the last 
  # period followed by anything, it stores it in $1
  # The second (inner) set of parens captures the first character 
  # and stores it in $2.  
  #
  # This effectively discards *anything* between the last . and the
  # end of the string.
  # mydomain.com => mydomain
  # mydomain.yourdomain => mydomain
  # so on
  m/^((\w).*)\..*$/;

  # $_ here is simply the string. 
  print OUTFILE "zone \"$_\" {\n", 
	        "\ttype slave;\n",
                "\tfile \"$2/db.$1\";\n\n",
                "\tmasters {\n\t\t204.177.32.2;\n\t};\n",
                "};\n\n";
}

close OUTFILE and close INFILE;


More information about the Techtalk mailing list