Solved

Help with my perl script

Posted on 2009-04-14
21
194 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 84

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 84

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
 
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 84

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 84

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 84

Assisted Solution

by:ozo
ozo earned 230 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 84

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 250 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 84

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 20 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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)

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now