Improve company productivity with a Business Account.Sign Up

x
?
Solved

Help with my perl script

Posted on 2009-04-14
21
Medium Priority
?
207 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
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 …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

601 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