Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with my perl script

Posted on 2009-04-14
21
Medium Priority
?
202 Views
Last Modified: 2012-05-06
hello, I have this script but it fails to run. It was orginaly set to run on a unix box( See below), so I changed the path to my my windows path wihere my input file is located.

When I run it, it fails to look at my directory where the files are located.

My input file is located at c:\named.conf-rich

Can someone fix this so it will run on my windows box.

Thanks

#!/perl
 
unless ($ARGV[0])  {
    print "******************************************************\n";
    print "*                                                    *\n";
    print "*                                                    *\n";
    print "******************************************************\n";
    exit;
}
 
$inputfile=$ARGV[0];
$zonefound = 0;
 
open(INFILE, $inputfile) or die "Unable to find $inputfile\n";
@conf = <INFILE>;
close INFILE;
 
foreach $linein (@conf)  {
    chomp($linein);
    $count++;
 
        if ($linein =~ /^zone\s+\".*in-addr.arpa\"/)  {
            $countZONE++;
            $zonefound = 1;
            ($j1, $j2, $j3, $j4, $j5) = split " ",$linein;
#print "ZONE-START: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /^\}/))  {
            $zonefound = 0;
#print "  ZONE-END: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /allow-update/))  {
print "$j2,$linein\n";
        }
        else {
                next;
        };
 
};

Open in new window

0
Comment
Question by:richsark
  • 10
  • 7
  • 3
  • +1
21 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 24141885
Do you get an error when you run it?
0
 
LVL 1

Author Comment

by:richsark
ID: 24142031

Hi,
Well, first I am unsure of the $input file in terms of what I need to set. I do know that my input file is located in c:\named.conf\-rich

In that directory I have tons of file called
named.conf.6cmghnsrt01.netdevice.rich.com

What this script will do is look at the contents of what is directed.

So, I want to make sure when I run this script, I need it to look at the directory c:\named.conf\-rich and look at all the files that begin with named.conf and pull out what the script does.

I think the script runs with script name and filename, checkconf.pl

named.conf.6cmghnsrt01.netdevice.rich.com
 
I do not want it to do that. I want it to look in my dir and do it automaticlay.
Thanks
0
 
LVL 85

Expert Comment

by:ozo
ID: 24142113
do you want to read @ARGV=<c:/named.conf/-rich/*>?
how would you have done what you are trying to do in unix?
the code snippet does not seem to match your description.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:richsark
ID: 24142221
Hi, yes, My script for unix is what I want to make work for my needs. The script I have is a baseline which I think is close to what I need.
So, yes my input file is c:\named.conf-rich which will contain all the files I need for this script to read.
Thanks Ozo
0
 
LVL 1

Author Comment

by:richsark
ID: 24142766
hi, does that help?
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24143146
The script you posted expects you to give it the name of a file as a command line argument, like so:
    script.pl c:\named.conf-rich\list.txt

The script then reads that file, and counts the number of zones (lines beginning with "zone") in them.  
0
 
LVL 1

Author Comment

by:richsark
ID: 24143175
Hi Adam, I know , I want it to read a specific directory and work from there. That is why I needed this script to be slightly re-worked.
I rather have this script work in a wau where I excute the script, it will look in my directory, in this case c:\named.conf-rich
In there it will find all the files that start with named.conf.xxxxx and spit out what its designed to do from there.
Does that sound do'able
0
 
LVL 85

Expert Comment

by:ozo
ID: 24143225
#open(INFILE, $inputfile) or die "Unable to find $inputfile\n";
#@conf = <INFILE>;
#close INFILE;
 
#foreach $linein (@conf)  {

@ARGV=<$inputfile/named.conf.*>;
while( $linein=<> ){

0
 
LVL 1

Author Comment

by:richsark
ID: 24143265
Hi Ozo, so this will go like this in my scirpt
#!/perl
 
unless ($ARGV[0])  {
    print "******************************************************\n";
    print "*                                                    *\n";
    print "*                                                    *\n";
    print "******************************************************\n";
    exit;
}
 
#open(c:\named.conf-rich, $inputfile) or die "Unable to find $inputfile\n";
#@conf = <INFILE>;
#close INFILE;
 
#foreach $linein (@conf)  {
 
@ARGV=<$c:/named.conf-rich/named.conf.*>;
while( $linein=<> ){ 
foreach $linein (@conf)  {
    chomp($linein);
    $count++;
 
        if ($linein =~ /^zone\s+\".*in-addr.arpa\"/)  {
            $countZONE++;
            $zonefound = 1;
            ($j1, $j2, $j3, $j4, $j5) = split " ",$linein;
#print "ZONE-START: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /^\}/))  {
            $zonefound = 0;
#print "  ZONE-END: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /allow-update/))  {
print "$j2,$linein\n";
        }
        else {
                next;
        };
 
};

Open in new window

0
 
LVL 85

Expert Comment

by:ozo
ID: 24143304
no,
#foreach $linein (@conf)  {
should be  commented out
and $c would need to be defined.
If you are not reading $c = $ARGV[0];
instead of $inputfile=$ARGV[0];
then you could code the path you want into the <>
0
 
LVL 1

Author Comment

by:richsark
ID: 24143326
Hi Ozo, can you please put the final code below so there is not to much back and forth.
I appericate it Ozo/Adam
Thanks
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 920 total points
ID: 24143373
#!/perl
 
#unless ($ARGV[0])  {
#    print "******************************************************\n";
#    print "*                                                    *\n";
#    print "*                                                    *\n";
#    print "******************************************************\n";
#    exit;
#}
 
#$inputfile=$ARGV[0];
$zonefound = 0;
 
#open(INFILE, $inputfile) or die "Unable to find $inputfile\n";
#@conf = <INFILE>;
#close INFILE;
 
#foreach $linein (@conf)  {

@ARGV=<c:/named.conf-rich/named.conf.*>;  #it looks like you want to ignore $inputfile=$ARGV[0];
 and always look for c:/named.conf-rich/named.conf.*
while( $linein=<> ){

    chomp($linein);
    $count++;
 
        if ($linein =~ /^zone\s+\".*in-addr.arpa\"/)  {
            $countZONE++;
            $zonefound = 1;
            ($j1, $j2, $j3, $j4, $j5) = split " ",$linein;
#print "ZONE-START: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /^\}/))  {
            $zonefound = 0;
#print "  ZONE-END: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /allow-update/))  {
print "$j2,$linein\n";
        }
        else {
                next;
        };
 
};
0
 
LVL 1

Author Comment

by:richsark
ID: 24143412
Awsome, so where it says
#@conf = <INFILE>;
I need to put:
@conf = <c:\named.conf-rich>;
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24143436
In the new code, @conf isn't used.  This line:
    @ARGV=<c:/named.conf-rich/named.conf.*>;
is the line that searches for the files named c:\named.conf-rich\named.conf.*.

Then this line:
    while( $linein=<> ){
is what reads from those files, one line at a time.
0
 
LVL 85

Expert Comment

by:ozo
ID: 24143452
No, that's not what I wrote.
But now I'm getting confused
But is c:\named.conf-rich a text file or a directory
0
 
LVL 1

Author Comment

by:richsark
ID: 24143489
ok, so we can assume this puppy will work as below
#!/perl
 
#unless ($ARGV[0])  {
#    print "******************************************************\n";
#    print "*                                                    *\n";
#    print "*                                                    *\n";
#    print "******************************************************\n";
#    exit;
#}
 
#$inputfile=$ARGV[0];
$zonefound = 0;
 
#open(INFILE, $inputfile) or die "Unable to find $inputfile\n";
#@conf = <INFILE>;
#close INFILE;
 
#foreach $linein (@conf)  {
 
@ARGV=<c:/named.conf-rich/named.conf.*>;  #i ignore $inputfile=$ARGV[0];
 and always look for c:/named.conf-rich/named.conf.*
while( $linein=<> ){
 
    chomp($linein);
    $count++;
 
        if ($linein =~ /^zone\s+\".*in-addr.arpa\"/)  {
            $countZONE++;
            $zonefound = 1;
            ($j1, $j2, $j3, $j4, $j5) = split " ",$linein;
#print "ZONE-START: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /^\}/))  {
            $zonefound = 0;
#print "  ZONE-END: $linein\n";
        }
        elsif (($zonefound == 1) and ($linein =~ /allow-update/))  {
print "$j2,$linein\n";
        }
        else {
                next;
        };
 
};

Open in new window

0
 
LVL 39

Accepted Solution

by:
Adam314 earned 1000 total points
ID: 24143538
Cleaning up your code...
#!/perl
 
$zonefound = 0;
@ARGV=<c:/named.conf-rich/named.conf.*>;
 
while( $linein=<> ){
    chomp($linein);
    $count++;
    if ($linein =~ /^zone\s+\".*in-addr.arpa\"/)  {
        $countZONE++;
        $zonefound = 1;
        ($j1, $j2, $j3, $j4, $j5) = split " ",$linein;
    }
    elsif (($zonefound == 1) and ($linein =~ /^\}/))  {
        $zonefound = 0;
    }
    elsif (($zonefound == 1) and ($linein =~ /allow-update/))  {
        print "$j2,$linein\n";
    }
    else {
        next;
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:richsark
ID: 24143617
Ok, will get back when a few
0
 
LVL 85

Expert Comment

by:ozo
ID: 24143635
#If I'm understanding what you are doing, it looks like your entire program may be

@ARGV=<c:/named.conf-rich/named.conf.*>;
(($j2)=/^zone\s+(".*in-addr.arpa")/) .. /^}/ and /allow-update/ and print "$j2,$_"  while <>;

0
 
LVL 5

Assisted Solution

by:vikaskhoria
vikaskhoria earned 80 total points
ID: 24145462
I guess the basic problem you were facing initially is like this:
When you were using arguments for giving the path, then you were entering the path with front slashes '\'
Whereas for perl script you should have done it with '/'

So the argument should have been 'c:/named.conf-rich' instead of 'c:\named.conf-rich'.
Rest of the logic changes for reading all files in the directory is well suggested by experts above.
I just added this, in case you again want to take user input for path, instead of hard coding.
0
 
LVL 1

Author Closing Comment

by:richsark
ID: 31570115
Excellent work !
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question