Solved

Script not counting D-DHCP

Posted on 2009-05-06
6
280 Views
Last Modified: 2012-05-06
Hello,
I just noticed that the value reported on the log-external.txt shows that all results from DHCP are zereo.

See a sample output:
"169.66.213.209"        "XGVSSMS"       "PC"    "D-DHCP"        "trtsq.uk.steve.com"
"169.66.213.210"        "nint-cko8rs4"        "PC"    "D-DHCP"        "trtsq.uk.sarkie.com"
"169.66.213.211"        "SRSETSFTNDQ3J"        "PC"    "D-DHCP"        "trtsq.uk.sarkie.com"
"169.66.213.212"        "Richard-nherf647"       "PC"    "D-DHCP"        "trtsq.uk.richie.com"

From log-external.txt:

Subnet,Static,DHCP,Unused

10.0.0.0/8 ,37,0,217
146.149.0.0/16 ,174,0,39314
161.16.0.0/16 ,133,0,1671


I tried to change the %counts to D-DHCP as well as

printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{DHCP}, $counts{Unused};

to

printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{D-DHCP}, $counts{Unused};

That just got me errors:

Bareword "D" not allowed while "strict subs" in use at fish3.pl line 18
Execution of fish3.pl aborted due to compilation errors.

#!perl

use strict; 

use warnings; 

use Data::Dumper; 

 

open my $log, '>', 'log-external.txt' or die "Could not open log: $!"; 

print $log "Subnet,Static,DHCP,Unused\n"; 

 

open my $dump, '>', 'dump.log' or die "failed to open 'dump.log' $!"; 
 
 

##### Step 1, read subnets 

open my $in, '<', 'm-names.txt' or die "Could not open m-names.txt: $!\n"; 

while( my $subnet = <$in>) { 

 

    print "Checking $subnet"; 

    chomp $subnet; 

     

    my %counts = (  

                   Static => 0, 

                   DHCP   => 0, 

                   Unused => 0, 

    ); 

     

    my @dnsoptions = `./getobjectlst.exe -u xx -p xx -o rich -a $subnet 2>&1`;

 

    print $dump "dumping \@dnsoptions\n";     

    print $dump Data::Dumper->Dump(\@dnsoptions); 

    print $dump '=' x 25, "\n"; 

 

    if ( @dnsoptions and $dnsoptions[0] eq '' ) {

        print "dnsoptions is null or undefined, going on to the next subnet\n"; 

        next; 

    } 

 

    # Now, at this point, we may have "Error 48" in @dnsoptions, or we may have 

    # the nicely formatted output. We'll have to check for both cases here. 

    # Let's check the unsuccessful case first. The condition below checks if 

    # the first element of @dnsoptions array has the following text in it - 

    # "Error 48: This subnet does not exist." in it. 

 

 

    if (join("",@dnsoptions) =~ /Error 48: This subnet does not exist./) { 

 

        # call "getsubnetlst.exe", passing $subnet as one of the parameters 

        my @subnetpart2 = `./getsubnetlst.exe -u xx-p xx -o rich -a $subnet -t network`; 

 

        print $dump "dumping \@subnetpart2\n";     

        print $dump Data::Dumper->Dump(\@subnetpart2); 

        print $dump '=' x 25, "\n"; 

 

         

        # now loop through each element of the array @subnetpart2, which looks like this - 

        # ########################################################################## 

        # "East" "146.149.1.0" "N" "" "146.149.0.0" " " " " "255.255.255.128" 

        # ########################################################################## 

        # pick up the 2nd field from the left (e.g. 146.149.1.0 above), and pass it as 

        # a parameter to the cli "getobjectlst.exe". 

 

 

        foreach my $line ( @subnetpart2 ) { 

 

            # get the 2nd field from the left 

            my $snetpart2 = (split/"\s+"/, $line)[1];

 

            # and now pass it to "getobjectlst.exe"; 

            my @dnsoptions2 = `./getobjectlst.exe -u xx -p xx -o rich -a $snetpart2`;

 

            print $dump "dumping \@dnsoptions2\n";     

            print $dump Data::Dumper->Dump(\@dnsoptions2); 

            print $dump '=' x 25, "\n"; 

             

            # find out counts of each subnettype (4th field from left) 

            foreach my $line (@dnsoptions2) { 

                my @subnettype = split/"\s+"/, $line; 

                $counts{$subnettype[3]}++; 

            } 

        } 

    } 

    else { # successful output from getobjectlst.exe 

 

        # find out counts of each subnettype (4th field from left) 

        foreach my $line (@dnsoptions) { 

            my @subnettype = split/"\s+"/, $line;

            $counts{$subnettype[3]}++; 

        } 

    } 

    printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{DHCP}, $counts{Unused}; 

} 

 

close($in); 

close($log);

Open in new window

0
Comment
Question by:richsark
  • 4
  • 2
6 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 24319527
Add quotes around D-DHCP
    my %counts = (  

                   Static   => 0, 

                   'D-DHCP' => 0, 

                   Unused   => 0, 

    ); 

     

Open in new window

0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 24319558
You'll also need to used the quotes in the print statement.
0
 
LVL 1

Author Comment

by:richsark
ID: 24319828
Thanks FishMonger, will let you know
0
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.

 
LVL 1

Author Comment

by:richsark
ID: 24324325
HI,

Man,... I tried everywhere to put quotes on this line, none are taking it.

printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{D-DHCP}, $counts{Unused};


0
 
LVL 1

Author Comment

by:richsark
ID: 24324439
HI, I got it!,

I thought I tried this one already,

printf $log "%s,%d,%d,%d\n", $subnet, $counts{Static}, $counts{'D-DHCP'}, $counts{Unused};

Thanks
0
 
LVL 1

Author Closing Comment

by:richsark
ID: 31578667
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

920 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

15 Experts available now in Live!

Get 1:1 Help Now