473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Find a line, and comment out the next 5 lines

HI,

I'm new to Perl. I'm trying to write a simple script which will
comment out a domain in my named.conf file for Bind.

What I would like to do, is write a script which will comment out a
domain from my named.conf if I no longer want to host that domain.

So, what I'd like to do is create a means to search for a domain
(mydomain.com) and comment out that file and all lines associated with
the file, and put a date when it was commented out. I'd also like to
take the UID of who commented it out, so if needed, we can ask that
person why it was commented out. As a hint we use sudo, so this should
be plausible, but again, not sure of the system command. If I use id,
it says I'm root, but how can I get the name of the user before I
became root?

named.conf example

zone "mydomain.com" {
type master;
file "/path/to/zone/mydomain.com";
notify yes;
}

What I'd like to do

// Domain taken out of named.conf at request of Customer by UID, date
// zone "mydomain.com" {
// type master;
// file "/path/to/zone/mydomain.com
// notify yes;
// }

Sounds simple enough, but I am not sure how to do it.

many thanks,

joe (js*****@hotmail.com)
Jul 19 '05 #1
1 4744
mdh
After providing you the obligatory warning about automating anything
to do with DNS (smile), what you're basically try to accomplish is the
following steps:

1) examine a multiline string for the following pattern:

\nzone "YOURTARGETZONE" {\n(arbitrary characters)\n(arbitrary
characters)\n(arbitrary characters);\n(arbitrary characters)}\n

2) take the string (the entire file) and chop it into three parts:
$prematch -- the characters before the match
$thematch -- the characters matching the pattern
$postmatch - the characters after the match

3) on $thematch, use the substitution operator to replace the
newlines with "\n//"

4) join $prematch, $thematch, and $postmatch back into one string

5) write the result joined string back out as your new zone file
Try the following code snippet...
mdh

#!/usr/bin/perl

my $UID = "joeblow\@somecompany.com";
my $datestring = `date -u "+%m/%d/%Y %H:%M:%S"`;
$datestring =~ tr/\n\r//d; # delete trailing newline or carriage
return
my $targetdomain = "mydomain.com";
# define a variable using the HERE construct for simulated zone file
input..
my $sampleinput = <<"EOF";

zone "dontchangethisdomain.com" {
type master;
file "/somepath/to/dontchangethisdomain.com";
notify yes;
}

zone "mydomain.com" {
type master;
file "/path/to/zone/mydomain.com";
notify yes;
}

zone "leavethisalonetoo.com" {
type master;
file "/other/path/to/leavethisalonetoo.com";
notify yes;
}

EOF

my $prematch;
my $thematch;
my $postmatch;

# use a regular expression with "non-greedy" matching to the token
# "zone" and the closing brace character (}) to anchor on the zone
# definition matching the domain you want to comment out -- you
# want "non-greedy" matching cuz you want the shortest string that
# starts with "zone", has your target, and ends with a closing
# brace -- if you use a greedy match (.*), you'll match on the last
# brace in the file, commenting out multiple domains in error

if ($sampleinput =~ m/\n(.?)zone "$targetdomain"
\{\n(.*)type(.*);\n(.*)file(.*);\n(.*)notify(.*);\ n(.?)\}/is) {
$prematch = $`;
$thematch = $&;
$postmatch = $';
$thematch =~ s/\n/\n\/\//g; # globally replace newlines with
newline and //
print "$prematch\n";
print "//domain taken out of named.conf at request of Customer by
$UID, $datestring";
print "$thematch";
print "$postmatch\n";
}

exit;
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Gdula | last post by:
Can anyone recommend a good way to get the count of the 'Number of Lines of Code' that are part of a vbProject. This should include all class and standard modules as well as the forms. If I can be...
15
by: Riko Wichmann | last post by:
Dear all, is there a way in Python to comment out blocks of code without putting a # in front of each line? Somethings like C's /* block of code here is commented out */ Thanks,
10
by: Monk | last post by:
Hi, Have a query regarding comments that extend over multiple-lines. Would like to know if the standard's view of this, so that we can create a code which doesn't run into compiler specific...
8
by: cj | last post by:
Has MS included in VB2005 any multi-line comment methods like in C? /* This is a multi-line comment in C */ It's something I'd like to have. I did read somewhere that I could use Ctrl+K,...
6
by: Markus Ernst | last post by:
Hi Searching for a possibility to display some text with preserved white space and line breaks, but with long lines being wrapped, I found this CSS declaration, which I found helpful: pre {...
18
by: vermarajeev | last post by:
Hello everybody, This is my second query in this post. Firstly thankx to Banfa, for helping me solve my first query. Here is the code which I have written. #include<iostream>...
11
by: xdevel | last post by:
Hi, I don't understand option. if I write: #line 100 "file" I change file numeration to start to line 100 but what "file" ? any example?
15
by: Spiros Bousbouras | last post by:
I'm thinking of adding a command to vim for removing white space from the end of each line of a C source file. Can anyone think of a situation where such white space might be useful ?
53
by: Gianni Mariani | last post by:
Do you have a preference on maximum line width for C++ code? I've seen the craziest debates on this most silly of topic. I have witnessed engineers spent oodles of time fiddling with line...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.