Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1129
  • Last Modified:

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

0
vePortal
Asked:
vePortal
  • 3
1 Solution
 
jb1devCommented:
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

0
 
vePortalAuthor Commented:
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.
0
 
jb1devCommented:
You could embed the perl script in your bash script.


#!/bin/bash

perl -e "
\$FILENAME = \"named.conf\";
open INFILE, \"<\$FILENAME\";
open OUTFILE, \">\$FILENAME.mod\";
while(<INFILE>) {
    if(/zone \"$1\"/) {
            print \"Removing $1\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

0
 
jb1devCommented:
Replace $1 with $DOMAIN from your example.
Note that's the only $ in the embedded script which isn't escaped. (Appears twice)

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now