Link to home
Start Free TrialLog in
Avatar of vePortal
vePortalFlag for United Kingdom of Great Britain and Northern Ireland

asked on

PERL/SED/AWK: How do I remove a zone from named.conf in a bash script.

Hello experts:

I need some major help and will assign 300 points for the best solution to my problem.  Im working on a bash script that has a function that removes forward zones from bind configuration files. Ive attached a snippet of my source and need someone to fill in the blank.

I would like to be able to execute sh removezone.sh domain1.com and be able to remove

zone "domain1.com" {
      type master;
      file "/var/named/thevirtualserver.com.hosts";
      };
from the named.conf

I've tried using sed to search for a domain1.com string and then remove the line that has a match but I can't remove to remove the second and fourth line.

Any kind of help would be much appreciated.

---------------------
Requirements
---------------------
2.) Have to use perl / sed /awk
3.) Have fun
--------------
removezone.sh
--------------
#!/bin/bash

# Declaring Variable
DOMAIN=$1

# Checking if Zone exists; if it does we will remove it.
if [[ -f /var/named/chroot/var/named/$DOMAIN.hosts ]]; then
    echo "Removing $DOMAIN zone."
    #Zone removal code from named.conf goes here.
    rm -rf /var/named/chroot/var/named/$DOMAIN.hosts
    echo "The $DOMAIN zone has been removed."
    exit;
else
    echo "Error: The $DOMAIN zone can not be removed as it does not exist."
    exit;
fi
--------------
named.conf
--------------

zone "domain1.com" {
	type master;
	file "/var/named/thevirtualserver.com.hosts";
	};

zone "domain2.com" {
	type master;
	file "/var/named/thevirtualserver.com.hosts";
	};

Open in new window

Avatar of jb1dev
jb1dev

rem.pl

usage:
./rem.pl domainname.com


#!/usr/bin/perl

$FILENAME = "named.conf";
open INFILE, "<$FILENAME";
open OUTFILE, ">$FILENAME.mod";
while(<INFILE>) {
    if(/zone "$ARGV[0]"/) {
            print "Removing $ARGV[0]\n";
        while(<INFILE>) {
            if(/\s*};/) {
                print "Done with zone removal\n";
                last;
            }
        }
    } else {
        print OUTFILE;
    }
}
close INFILE;
close OUTFILE;
rename("$FILENAME.mod", "$FILENAME");

Open in new window

Avatar of vePortal

ASKER

Hello jb1dev:
Can you think of something that would not require me to call another file. I'd like to keep it all in one if possible.
ASKER CERTIFIED SOLUTION
Avatar of jb1dev
jb1dev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Replace $1 with $DOMAIN from your example.
Note that's the only $ in the embedded script which isn't escaped. (Appears twice)