Solved

Script not counting D-DHCP

Posted on 2009-05-06
6
283 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert grep lines to perl 6 75
AD Cleanup by EmployeeID 11 70
Authenticate using sesu from script 7 106
Script to change all permissions to my user 7 51
This article will show, step by step, how to integrate R code into a R Sweave document
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to count occurrences of each item in an array.
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 …

685 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